Welcome Guest [Log In] [Register]
Add Reply
How do I get this to not close automatically; have this code that i can't use cin.get
Topic Started: Apr 8 2009, 09:19 AM (351 Views)
SheWasAlmost18
Member Avatar

This code uses cin >> value
so when i put cin.get() somewhere else, it closes automatically, without me typing in a value

Code:
 
#include <iostream>
void age(int);

int main()
{
using namespace std;
age(4);
cout << " Right now you are 4 years old, how old do you want to be: ";
int new_age;
cin >> new_age;
age(new_age);
cout << " Nice, processing... " << endl;
return 0;
}

void age(int n)
{
using namespace std;
cout <<"You will be " << n <<" soon!" << endl;

}


how do I change this code, so i can see what happens if i put a value
Edited by SheWasAlmost18, Apr 8 2009, 09:19 AM.
We are made kind by being kind
Offline Profile Quote Post Goto Top
 
Samael88
Member Avatar

First of all I just want to say that you don't even need the "using namespace std;" here, if you are going to use it, why don't you just put it right after the includes? That way you are using it for the whole thing.

I would put a "cin" right before the return, that way you would cause the program to halt there.
Else you can include "conio.h" and use a "getch();" instead, that works also ^_^

The easiest would be this: "cin>>a;" and just declare "a" as a integer that you never use or something.
Offline Profile Quote Post Goto Top
 
MMZJSFMMX10MM
Member Avatar

Depending on what you are trying to do you may have to put the cin.get() twice. Once to flush an extra return key and then the second to do what it was you originally wanted it to do.
Posted Image
Posted Image
Posted Image
Offline Profile Quote Post Goto Top
 
SheWasAlmost18
Member Avatar

hmm, instead of using cin.get()
i used system("PAUSE")
It works, but the it says press any key to continue

also
how do I make a number random
like: random int f between 1 and 10
cout<< "You lost " << f << " dollars" << endl;
and if i want to say, pick a random number 1-10
they put 3, ( i want 7)
how do i say no, and yes
Edited by SheWasAlmost18, Apr 8 2009, 07:44 PM.
We are made kind by being kind
Offline Profile Quote Post Goto Top
 
MMZJSFMMX10MM
Member Avatar

Code:
 
srand( (unsigned) time (NULL ) );

for( int i=0 ; i < 10 ; i++ )
cout << rand() % 11 << endl;// Random number 0 - 10

system("pause");


You link the random generator with the system clock. I even used "system("pause");" but it is important to note that "system("pause");" is not a good programming technique in C++ under most conditions.
Edited by MMZJSFMMX10MM, Apr 9 2009, 12:27 AM.
Posted Image
Posted Image
Posted Image
Offline Profile Quote Post Goto Top
 
SheWasAlmost18
Member Avatar

cool!, how do I get it to display yes when it's the right int, and no if it's not

also, *off topic*
I have a converter ( oz to lbs)
and it works, but how do i get it to not close right after the first number I give it
Code:
 
#include <iostream>

int oz(int);
int main()
{
using namespace std;
cout<<"hello! how many pounds do you weigh: ";
int lbs;
cin >> lbs;
int ounces;
ounces = oz(lbs);
cout << "You weigh " << lbs <<" which is " << ounces << " ounces" << endl;
system("PAUSE");
}

int oz(int oz2lbs)
{
return 16 * oz2lbs;
}
yeah, I didn't think system pause was very good, are there alternate ways?
I prefer not to include more libraries, unless you think it's easier
Edited by SheWasAlmost18, Apr 10 2009, 07:40 PM.
We are made kind by being kind
Offline Profile Quote Post Goto Top
 
Darksorrow131

Wrap the question/answer code in an infinite loop ( while(true) { ... } ) and it will keep asking for more numbers. You probably want to break the loop if the user types "quit" or something though.

No idea about making the program pause, I've always been running those programs in Command Prompt so this issue never came up :P
(let loop () (loop))
((lambda (x) (x x)) (lambda (x) (x x)))
(let ((k #f)) (call/cc (lambda (cc) (set! k cc)) (k))
((call/cc call/cc) (call/cc call/cc))

-- Infinite loops are awesome! --
Tell me if you have other awesome infinite loops!
Offline Profile Quote Post Goto Top
 
MMZJSFMMX10MM
Member Avatar

To make it pause you can use a random variable like a char and then insert cin >> char; where you want it to pause. The person just has to push enter to continue then. You can also you can use a variable to hold your random number like this:

Code:
 
srand( (unsigned) time (NULL ) );

int num;
int randomN;

cin >> num;

randomN = rand() % 11; // Random number 1 through 10

if( num == randomN )
cout << "Yay!";
Posted Image
Posted Image
Posted Image
Offline Profile Quote Post Goto Top
 
Cosmic_AC

To make something repeat until the user wants to quit, use a loop:

char cont;
do{
/**/// Actions
/**/cout << "Continue? (Y/N) : ";
/**/cin >> cont;
}while(cont != 'y' && cont != 'Y');

If you want to be even more elaborate/precise, you can use another loop to make sure the user enters either 'Y' or 'N':

char cont = 'n';
do{
/**/// Actions
/**/do{
/******/cout << "Continue? (Y/N) : ";
/******/cin >> cont;
/**/}while(cont != 'Y' && cont != 'y' && cont != 'N' && cont != 'n');
}while(cont != 'y' && cont != 'Y');
Edited by Cosmic_AC, Apr 11 2009, 01:02 PM.
++++++++++[>+++++++>+++++++++++<<-]>---.>+.++++.------.----.------.<<++++[>>+++++++<<-]>>.<---.+++.
Offline Profile Quote Post Goto Top
 
SheWasAlmost18
Member Avatar

I don't quite understand how to apply the loop thing to my converter, but the random guessing thing i'll definitely work on!
thanks
We are made kind by being kind
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
Go to Next Page
« Previous Topic · Application Programming Support · Next Topic »
Add Reply

Banner and Logo by TheKeith