Hi I'm new to programming and just wanted to see if someone could tell me what I'm doing wrong in this program. When the program loads back to the loop,it skips the first:
"cout << "Enter value for x: ";
getline (cin,mystr);"
and jumps right to:
"cout << "Enter value for y: ";
getline (cin,mystr);"
I know this isn't the prettiest or most efficient program, but as I stated above, I'm new to programming.
Hopefully someone can analyze the program and tell me what I did wrong, and maybe give me some pointers.
Thanks beforehand.
//Alle mulige ting til programmet
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//Her kan der, hvis det er nødvændigt defineres nogle globale variabler
int main()
{
int d;
do {
string mystr;
float a,b,c;
cout << "Enter value for x: ";
getline (cin,mystr);
stringstream(mystr) >> a;
cout << "Enter value for y: ";
getline (cin,mystr);
stringstream(mystr) >> b;
cout << "What is: a) x+y?\n";
getline (cin,mystr);
stringstream(mystr) >> c;
while (c!=a+b) {
cout << "Are you stupid? Try again!\nWhat is: a) x+y?" << endl;
getline (cin,mystr);
stringstream(mystr)>>c;
}
if (c=a+b)
cout << "YOU ARE CORRECT!" << endl;
cout << "What is: b) x-y?\n";
getline (cin,mystr);
stringstream(mystr) >> c;
while (c!=a-b) {
cout << "Are you stupid? Try again!\nWhat is: a) x-y?" << endl;
getline (cin,mystr);
stringstream(mystr)>>c;
}
if (c=a-b)
cout << "YOU ARE CORRECT!" << endl;
cout << "What is: c) x*y?\n";
getline (cin,mystr);
stringstream(mystr) >> c;
while (c!=a*b) {
cout << "Are you stupid? Try again!\nWhat is: a) x*y?" << endl;
getline (cin,mystr);
stringstream(mystr)>>c;
}
if (c=a*b)
cout << "YOU ARE CORRECT!" << endl;
cout << "What is: d) x/y=\n";
getline (cin,mystr);
stringstream(mystr) >> c;
while (c!=a/b) {
cout << "Are you stupid? Try again!\nWhat is: a) x/y?" << endl;
getline (cin,mystr);
stringstream(mystr)>>c;
}
if (c=a/b)
cout << "YOU ARE CORRECT!" << endl;
cout << "Want to try again?\n1) Yes\n2) No" << endl;
cin >> d;
}while (d==1);
return 0;
}
@kbw
Thanks for the pointers and the quick reply.
btw, I know it isn't a good idea, but I've got black humor, so this works best on myself. But i'll certaintly remember not to use hatefull comments when / if I get so far as to make programs for other people.
Again thanks alot.
@hamsterman
Your sollution worked perfectly.
Thanks alot for your quick reply.