Click to See Complete Forum and Search --> : Problems.... Suprised?


Typhus
06-08-2006, 12:39 AM
Ok, I tried compiling this. My goal is this:

Please enter a number:
Please enter a second number:
The sum of the two numbers is:
Would you like to add again? ( Y = Yes, N - No ):

So I tried using a boolean variable to determine weither the loop go's off. So I get a compiler problems.

CODE BELOW

#include <iostream>
#include <string>
using namespace std;

int main()
{
double add1 = 0;
double add2 = 0;
double answer = 0;
int menu_answer;
bool menu_loop = true;

do
{
bool menu_loop = true;
cout << "Please enter a number: ";
cin >> add1;
cout << "Please enter a second number: ";
cin >> add2;
cout << "The sum of the two numbers is " << ( add1 + add2 ) << "\n";
cout << "Would you like to add again? ( Y = Yes, N = No )\n";
cin >> menu_answer;
if( menu_answer = Y)
{
menu_loop = true;
}
else if(menu_answer = N)
{
menu_loop = false;
}
while( menu_loop = true ); //
}
system("PAUSE");
return EXIT_SUCCESS;
}

drkybelk
06-08-2006, 04:20 AM
Hi,

your variable menu_answer is an int but later in your if statement you try to compare it (apparently) to a char. But unfortunately you forgot the single quotes around your Y.
So declare the menu_answer as "char" and the if statement should read
if( menu_answer = 'Y')
...
else if(menu_answer = 'N')

Cheers,

D

Rune Bivrin
06-08-2006, 08:38 AM
Hello, this is C++! That means a test for equality needs two equals signs, othewise it turns into an assignment.
if (menu_answer == 'Y')

Rune

Typhus
06-08-2006, 12:24 PM
Thanks I can get it to compile now, I noticed no matter what I pressed (N or Y) the program would go into the loop and never end. SO I changed it a bit but still encounter the same problem.

Also, what could I do to send it to a specific point without looping or the goto? (Say I wanted the user to go back to the beginning of the program but not use a loop to do it.)

Code

i#include <iostream>
#include <string>
using namespace std;

int check_answer();

int main()
{
double add1 = 0;
double add2 = 0;
double answer = 0;
int menu_answer;
int menu_loop;

do
{
cout << "Please enter a number: ";
cin >> add1;
cout << "Please enter a second number: ";
cin >> add2;
cout << "The sum of the two numbers is " << ( add1 + add2 ) << "\n";
cout << "Would you like to add again? ( Y = Yes, N = No )\n";
cin >> menu_answer;
if( menu_answer == 'Y')
{
menu_loop = 2;
}
else if(menu_answer == 'N')
{
menu_loop = 1;
}
}
while( menu_loop >= 2 );

return 0;
}

Danny
06-08-2006, 01:12 PM
My advice is to read input as a string, not as a single character. So ask the user to enter Y or T (or y or t) but use strcmp:

if ( strcmp(menu_answer, "Y")==0) || strcmp(menu_answer, "y")==0)
{
//continue
}
else
{
//break
}


As for goto: you never really need to use it. Simply breal your program into indvidual functions. When the user wants to start over, simply call the initializer function, e.g., a function that accepts input.

Typhus
06-08-2006, 03:38 PM
Could you give me a link to a tutorial on that? (strcmp)

jonnin
06-08-2006, 04:45 PM
if(strcmp(a,b) == 0) //then they are equal
so

char Y[] = "Y"
char N[] = "N"
cin >> yesno;
toupper(yesno); //look up how to use this one, I may be wrong here
if(strcmp(yesno,Y) == 0 ) //they said yes
else
if(strcmp(yesno,N) == 0 ) //they said no
else
//they need to re-enter

Danny
06-08-2006, 06:06 PM
Instead of calling toupper (which has to be called in a loop, once for every character), you can use strcmpi which is now in the standard. It perfoems case-insensitive comparison of strings.