Charas-Project
Off-Topic => Really Old Stuff => Archive => General programming => Topic started by: maxine on October 21, 2005, 03:04:51 PM
-
Ok here is a c++ simple made if else test
// Just a test math simple
#include
using namespace std;
main()
{
double answer;
cout << "1. what is 5 x 5?: ";
cin >> answer;
if (answer == 25){
cout << "Ur right mate";
}
else {
cout << "Ur wrong";
}
cin.ignore(1000,'\n');
getchar ();
return 0;
}
I want too know how too put a loop so if you answer the wrong answer it jumps too the beginning so it starts over you know?. I think it should have something with "while" loop too do but i dont get "while" so good plz help (A)
-
If I remeber correctly:
"What is 5 x 5"
get input (with cin??)
While (input != right answer (25) )
{
"Wrong! Try again."
get input (with cin??)
}
"You are Right!! Have a cookie."
.. yeah, but thats pseudocode. I've not written in C++ in forever. But if you know what I'm trying to do, you can replace the pseudocode. Hope that helps.
EDIT: I'll just edit your code. :p Now With 33% more Comments!
main()
{
double answer;
cout << "1. what is 5 x 5?: "; // Output
cin >> answer; //Get input
while (answer != 25) //Loop while answer is wrong
{
cout << "Ur wrong"; // Output
cout << "1. what is 5 x 5?: "; // As Q again
cin >> answer; // Get input again
}
cout << "Ur right mate"; // Output
cin.ignore(1000,'\n'); // WTH is this!!!???
getchar (); // yeah.
return 0; // should it be int main() for this??
}
-
int main is gonna be up where main is i didnt put it there. Cin ignore ignores all cin pieces so you can watch the program in a black box. Thanks i get the idea
Edit: or actually i dunno.. But the teacher said it was gonna be there so i guess it has something with the test too do. (when you are gonna test ur program)
-
Hey drighton ive got another question for you PRO ;). You can still answer 25,1 25,3 etc and its right as long as it got with 25 too do. I know the problem and thats cuz i made a "double" but whats it gonna be there instead? thanks mate you are really helping me =)
-
If you mean what type of variable it needs to be, then probably integer.
CODE
--------------
main()
{
int answer;
--------------
like that
-
no, i meant like this. I am using this
double answer <--- double. But with double you can still answer 70,1 or 70,2 etc.. But the answer is gonna be 70. You know there is like double, float etc.. But i dont know which one to use so the program only reads integer numbers and not the decimals. You know what i mean?
-
So you want the user to be able to input decimal numbers.
like 5x5 = 25.5 ?
if so, then use float. I think... :p
I might have slept through that class. I also slept through or missed the class on Classes :| which I regret.
If this don't work, ask Osmose. He may be of better help.
-
hehe ok yeah you didnt quite get it, or maybe i am so bad at explaining =). But you've been to help, thank you. I might come back with more to you but this topics now closed =) thanks again