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;
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) ".";
elseif( 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.