help?

i cant figure out how to exit the function when i input "no". any solutions? heres the function.

#include <iostream>


using namespace std;

int yes=1;
int no=2;

void exit()
{

cin >> no >> endl;
return;
}


int main()
{

cout << "Enter a number: "; // ask user for a number
int x;
int y;

cin >> x; // read number from console and store it in x
cout << "You entered " << x << endl;
cout << "Do you wish to know what " << x << " squared is equal to?" << endl;
cout << "For yes, type: " << yes << " For no, type: " << 2 << endl;
cin >> yes;
exit();

y=x*x;
cout << x << " squared is equal to " << y << endl;

return 0;
}
Last edited on
First, you should surround code in code tags( the <> button under Format: ).

You are doing way to much for such a simple program. Heres some pseudocode for you to figure out how to translate it to real code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
variable num;
Prompt user to enter a number;
Store that number in num;

Ask user if he would like to square the number;
variable response;
cin >> response;

if( response == yes )
   tell user, num "squared is equal to " (num*num) ".";
else if( response == no )
   return 0; // ends the program

while(true){} // keeps the window open

return 0;


The window will close immediately though, so i added a line to keep it open.
how would you code "if (response ==yes/no)?"
When you prompt say something like "Would you like to square the number? For Yes, enter 1. For No, enter 2.

if( response == 1)//yes
etc.

Or you could do "Would you like...? Y/N"

if( response == Y)//yes
etc.
ok
Last edited on
Topic archived. No new replies allowed.