well as i said im realy realy new and i just wondered if there was a way to make a if command saying
if input char is y run if input char is n close so could someone plz help me
well as i said im realy realy new and i just wondered if there was a way to make a if command saying
if input char is y run if input char is n close so could someone plz help me
You need to know the basics of I/O using cin/cout.
1) Declare a character variable.
2) Prompt user for input using cout.
3) Input into the above character variable using cin.
4) Test the character entered by user via an if statement and perform the appropriate action based on result of the test.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
next time read a tutorialCode:char a_char; //program does some stuff - read input etc. if (a_char == 'y') { //do stuff } else if (a_char == 'n') { //do stuff }
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
thanks its helped me a lot and thanks richie t for the link to those tutorials i hadnt seen them
neway thanks again for the help
do you no what is rong with this code because i cant work it outCode:#include <iostream> using namespace std; int main() { char a_char; char answer; cout<<"Would you like to run this program y = yes n = no: "; cin>> answer; if (a_char == 'y') { int age; cout<<"Please input your age: "; cin>> age; cin.ignore(); if ( age < 100 ) { cout<<"You are pretty young!\n"; } else if ( age == 100 ) { cout<<"You are old\n"; } else { cout<<"You are really old\n"; } cin.get(); } else if (a_char == 'n') { return EXIT_SUCCESS; }
Notice anything wrong with that code, in particular the variables you are using?Code:cin>> answer; if (a_char == 'y')
Your storing the yes / no ( y / n ) in the variable: answer, but your checking if the variable: a_char equals y / n. If I'm not mistaken, a_char will always equal nothing in your program. And also, add another } at the end, as your'e not closing main().
I think this is what you want:
It's always a good idea to indent your code, as it makes it much easier to read. Also, it would be quite helpful to handle any unexpected input: e.g. If the user enteres a letter when asked for his/her age, or if they don't enter either y or n at the start. If you have any questions, please do not hesitate to contact me at: School.Code:#include <iostream> using namespace std; int main() { char a_char; cout<<"Would you like to run this program y = yes n = no: "; cin>> a_char; if (a_char == 'y') { int age; cout<<"Please input your age: "; cin>> age; cin.ignore(); if ( age < 100 ) { cout<<"You are pretty young!\n"; } else if ( age == 100 ) { cout<<"You are old\n"; } else { cout<<"You are really old\n"; } cin.get(); } else if (a_char == 'n') { return EXIT_SUCCESS; } }
Cya.
P.S. I've probably made some sort of fatal mistake in the code above, so if any of the other members have the time, please alert me of any errors so I can edit them for him. Thanks
Last edited by Necrofear; 06-30-2006 at 09:14 AM.
What happens if a_char is neither 'y' or 'n'? Also, answer is being declared but not used. What happens if the user inserts an invalid age?
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
hi sorry to ask realy perthetic questions but how do i tell it to go back to a set place in the code instead of just starting the if loop again
The tutorials explain about loops, conditions, and functions, shadowless. The main elements in controling program flow.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
ok so ive checked my cod a bit and yet still it doesnt seem to be quite right
Code:#include <iostream> using namespace std; int main() { char a_char; cout<<"Would you like to run this program y = yes n = no \n"; cin>> a_char; if (a_char == 'y') { int age; cout<<"Please input your age: "; cin>> age; cin.ignore(); if ( age < 100 ) { cout<<"You are pretty young!\n"; } else if ( age == 100 ) { cout<<"You are old\n"; } else if ( age > 100 ) { cout<<"You are really old\n"; } else { cout<<"that is an invalid charector, application will now close\n"; // this is the bit that doesnt work no matter what i do it just doesnt run that section } cin.get(); } else if (a_char == 'n') { return EXIT_SUCCESS; } else { cout<<"that is an invalid charector, application will now close\n"; // this is the bit that doesnt work no matter what i do it just doesnt run that section } }
were i put this is the i meant
this is the bit that doesnt work no matter what i do it just doesnt run that section
WTF ?
What was that last post ? Any one ?
And what was that about going back to a set place in the code ? Where exactly do you want it to 'go back,' and where do you want it to 'go back' to. I'll try to make a loop for you, and go through it line by line.
Last edited by Necrofear; 06-30-2006 at 11:38 AM.
You are using too many if statements, and some on places where they shouldn't exist.
Code:char a_char; //removed 'n'. Note really necessary, don't you agree? cout<<"Would you like to run this program y = yes\n"; cin>> a_char; if (a_char == 'y') //any other char will exit the program { int age; cout<<"Please input your age: "; cin>> age; //it makes sure cin is valid. It could be invalid if for instance //i tried to input something other than an integer if (cin) { if ( age < 100 ) cout<<"You are pretty young!\n"; else if ( age == 100 ) cout<<"You are old\n"; else if ( age > 100 ) cout<<"You are really old\n"; } else // if cin is not valid then... cout<<"that is an invalid character, application will now close\n"; cin.ignore(); cin.get(); } //main should always return something. //the way you had it before, it wasn't all the time. return EXIT_SUCCESS;
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
>> this is the bit that doesnt work no matter what i do it just doesnt run that section
When the prompt is "Would you like to run this program y = yes n = no", if I type g and hit enter it says, "that is an invalid charector, application will now close".
The first time you have that error message will never be run, though, because it is the else of if age < 100, age == 100, and age > 100. No matter what age is, it will always be one of those three, so the final else will never be run. You just have to look at the logic of the code to see why it is never called.