Yes/No programming question

Alright so i have this code i made, and i want to ask if you are sure that is what you want but i dont know how. here is the code. ill bold the part i need help in

// Michael DeGray Wednsday September 23, 2009
#include <iostream>

using namespace std;
using std::cout;
using std::cin;
using std::endl;

int main()
{
int numb1;
int numb2;
int numb3;
int numb4;
int sum1;
int sum2;
char Y;
char N;


cout << "This program will take two numbers and add them, then it will take two seperate number add them and compaire the two. " << endl << "\nPlease input your first number.\n"; // promts for user input
cin >> numb1; // stores inputed number as numb1

cout << "Please input the number to be added to the previous number.\n"; // promts for user input
cin >> numb2; // stores inputed number as numb2

cout << "The first equation is " << numb1 << " + " << numb2 << endl << "Is this correct? Y/N\n"; // shows the equation for the first set of inputed numbers
cin >> Y;
if Y

sum1 = ((numb1 + numb2));

cout << "The answer to the first equation is " << sum1 << endl; // shows answer to first equation

cout << endl << "Please input the first number of the second equation.\n"; // asks for user input
cin >> numb3; // stores inputed number as numb3

cout << "Please input the number to be added to the previous number.\n"; // asks for user input
cin >> numb4; // stores inputed number as numb4

cout << "The second equation is " << numb3 << " + " << numb4 << endl; // shows equation for the second set of inputed numbers

sum2 = ((numb3 + numb4));

cout << "The answer to the second equation is " << sum2 << endl << endl; // shows answer to second quation

if ( sum1 == sum2 ) // Is the two numbers are equal display the following
cout << sum1 << " == " << sum2 << endl;

if ( sum1 != sum2 ) // if the two numbers are not equal display the following
cout << sum1 << " != " << sum2 << endl;

if ( sum1 < sum2 )
cout << sum1 << " < " << sum2 << endl;

if ( sum1 > sum2 )
cout << sum1 << " > " << sum2 << endl;

if ( sum1 <= sum2 )
cout << sum1 << " <= " << sum2 << endl;

if ( sum1 >= sum2 )
cout << sum1 << " >= " << sum2 << endl;

return 0;
}

Now if the user types Y then it contuines with the program but if they press N it will ask them for 2 numbers again. how would i do this? thanks in advance

EDIT: for some spelling errors (there are probably more i missed but i think it's readable)
Last edited on
didn't quite get you when you said continue with program ...how should the program look?
btw the if statement should have something like if (y) ...parenthesis are required..
You would need to use a loop of some sort.
Hi,

1) You don't need to include the whole std namespace when you're including some parts of it.
2) A variable name has nothing to do with user input. You're storing the user input in a variable of type char with the name "Y" and assumes the user really typed Y. But the user can also type in other things.

You could use a char variable, then check what the user did input. You will need to use a while loop to ask for 2 numbers again. To use yes/no, you will need a boolean variable.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int num1 = 0;
int num2 = 0;
char input = "";
bool correct = false;

while (!correct)
{
     cout << endl << "Enter number 1: ";
     cin >> num1;
     cout << endl << "Enter number 2: ";
     cin >> num2;
     cout << endl << "Did did enter " << num1 << " and " << num2 << ". Is this correct? (Y/N)";
     cin >> input;

     switch(input)
     {
          case 'Y':
             correct = true; // Now the loop will stop
          case 'N':
             correct = false; // As correct is still false, it will repeat the whole while-loop
     }
}


3) You don't need parenthesis here:
sum2 = ((numb3 + numb4));
It will compile, but it's useless.

4) Use code-tags to post your code please; that's much more readable =)
Last edited on
Thanks so much! I'm new to programming so im sorry if its hard to read :( but i reall appricate all the help! Thanks again!

How do i use code-tags?
Last edited on
Just put your code in [code ][/ code] (without spaces) to use code-tags.
Last edited on
Topic archived. No new replies allowed.