Program that adds two numbers entered

Hi I keeping getting errors on my code i wanted to know if anyone could help me figure out what im doing wrong

#include <iostream>
Using namespace std;
int main()
{
int n1, n2;
char again;
while ( again ==’y) || again == ‘Y’)
cout<< “enter a number :”;
cin << n1;
cout<< “enter another number :”;
cin << n2;
cout<< “The sum is << ( n1+n2)”<<endl;
cin << again;

}
You're missing a single quote (') after again ==’y. Also, you need to enclose all this inside curly brackets for them to be part of your while loop.

1
2
3
4
5
6
cout<< “enter a number :”;
cin << n1;
cout<< “enter another number :”;
cin << n2;
cout<< “The sum is << ( n1+n2)”<<endl;
cin << again;


And you need to fix the placement of the quotes in this line: cout<< “The sum is << ( n1+n2)”<<endl;
Last edited on
#include <iostream>
using namespace std;
int main()
{
int n1, n2;
char again;
while(again =='y') || again == ‘Y’) --------> || This is still giving me an error
{
cout<< “enter a number :”;
cin << n1;
cout<< “enter another number :”;
cin << n2;
cout<< "The sum is" << ( n1+n2)<<endl;
cin << again;

}
return;
}
What is the error it's giving you? Also, what do you think is the value of again when you run the program? And you need to look up how to do input operations, specifically the formatted input operator.
Last edited on
Thank you for the help i figured out what was wrong with the code
I re-wrote your code so that it would work. In the comments are what you did wrong.
Also, you used the wrong kind of quotes in your program. You have to use " " instead of ” ”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>                                 //all good here
using namespace std;                                //lowercase the u
int main(void)                                      //preference for me
{                                                   //all good here
    int n1=0, n2=0;                                 //always set variables to 0
    char again='Y';                                 //always set chars to '\0'
    do {                                            //you forgot a bracket, and it is better to use a do while here.
        cout << "Enter a number: ";                 //good here
        cin >> n1;                                  //wrong way brackets
        cout<< "Enter another number: ";            //good here
        cin >> n2;                                  //wrong way brackets
        cout << "The sum is "<< ( n1+n2) << endl;   //extra parenthesis here
        cout << "Try again? [Y/N] ";                //You forgot to ask the user if they wanted to try again
        cin >> again;                               //wrong way brackets
    }while (again == 'y' || again == 'Y');          //forgot a quotation mark and an extra parenthesis was there
    return (0);                                     //Return 0 always
}                                                   //Program end 

Topic archived. No new replies allowed.