C++ noob

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;
}
The program seems ok to me.

A couple of comments.
1
2
3
4
5
string mystr;
float a;
cout << "Enter value for x: ";
getline (cin,mystr);
stringstream(mystr) >> a;

is equivalent to:
1
2
3
float a;
cout << "Enter value for x: ";
cin >> a;

You only need the stringstream stuff if you need to do further parsing.

It's not a good idea to have your program call it's user stupid.
Last edited on
cin >> d; leaves a \n in the stream. remove it with a cin.ignore();
@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.
Topic archived. No new replies allowed.