do while loop

so im starting to learn some loops... its a rocky start but in theory i think this should work im trying to get a repetitive addition program going.
basically its a simple this + that program but i am focusing on making it continue instead of exiting every single time i finish calculating
so i tried giving it a choice to exit or not to exit...but it doesnt seem to be working

2) also this is off topic but i was surprised when result=a+b is not the same thing as a+b=result.... i think i understand that is like that because we are trying to define result first... but i was still a little surprised by that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
    int a,b,result;
    char cont;

    do{
        cout<<" Enter number 1: ";
        cin>> a;
        cout<<" enter number 2: ";
        cin>>b;
        result=a+b;
        cout<<" Your answer is: "<<result;
        cout<<" Continue? (y/n)";
        cin>>cont;
    } while (cont==y);

return 0;
}
Last edited on
This will make it compile.
while(cont = 'y');
(cont==y); y should be 'y' (character literal )

2) also this is off topic but i was surprised when result=a+b is not the same thing as a+b=result.... i think i understand that is like that because we are trying to define result first... but i was still a little surprised by that

when you type in C++ A = B; you are telling give to A the value of B

result = a+b means get the value of a+b and assign it to result,
a+b = result wouldn't make much sense ( although it may be right on some weird circumstances )
Last edited on
hmm thank you it worked...however it does not recognise the "n"...i thought that by the logic of my code anyhting other than "y" entered will stop the program
Here is a riddle for you. Try entering a character instead of a number and see what happens.
Entering anything other than 'y' did stop the program for me.
1
2
kempofighter (671)  	 Mar 20, 2010 at 5:09pm
Here is a riddle for you. Try entering a character instead of a number and see what happens.


you mean enter a character into the addition process of the program? itll go all crazy
ah i see yes it does work i accidentally erased one of my == and made it =...thanks alot everyone
Here you go :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
    int a,b,result;
    char cont;

    do
    {
	cin.clear();
        cout << "Enter number 1: ";
	cin >> a;
        cout <<"Enter number 2: ";
        cin >> b;
        result = (a + b);
        cout << "Your answer is: " << result << endl;
        cout << "Continue? (Y/N)";
        cin >> cont;
    } while (cont == 'Y' || cont == 'y');
return 0;
}
Last edited on
ooohh thats a nice use of or.... can one use that as many times in the same parenthesis? of an action? also im not sure what cin.clear does? im assuming it clears the screen of my previous calculations after i have used the program a few times?

EDIT: hmm i tested your code and cin.clear() does not do what i thought it did...
Last edited on
you mean enter a character into the addition process of the program? itll go all crazy


How do I fix that? have been making programs to make while and do loops, but whenever I press a letter, instead of the numbers, my program loops and loops until I kill the process. How can I make the program only choose integers, and not letters?
thanks..
Topic archived. No new replies allowed.