do-while problems

Yesterday I decided to compose my own calculator just for fun and right now I'm trying to make it able to repeat operations via do-while loop. Algorithm looks like right, but it doesn't work properly. Could someone say what's wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include <iostream>
#include <math.h>
using namespace std;

int main()
{
	char sign, sign2;
	double a, b, c;
	do
	{
		cin >> a;
		c = a;
		do
		{
			a = c;
			cin >> sign;

			switch (sign)
			{
			case '+': cin >> b; c = a + b;
				cout << "The result is:" << c << endl;
				break;
			case'-': cin >> b; c = a - b;
				cout << "The result is:" << c << endl;
				break;
			case'*': cin >> b; c = a*b;
				cout << "The result is:" << c << endl;
				break;
			case '/': cin >> b; c = a / b; if (b == 0)
			{
						  cout << "Forbidden action" << endl;
			}
					  else
					  {
						  cout << "The result result is:" << c << endl;
					  }
					  break;
			}

			cout << "C-- Continue calculations,\nN-- New calculation,\nE-- Exit" << endl;
			cin >> sign2;
		} while (sign2 == 'N');
	} while (sign2 == 'E');
	
	system("pause");
	return 0;
}
Last edited on
What you are doing doesn't make sense. You need to get 2 numbers as input and 1 sign from the user. In the beginning of the code give the user some guide, something like "Enter two numbers followed by a space: " or "Enter first number: ". You seem to take one number and then set the second to the first (c = a).

You are doing this:
1
2
cin >> a;
c = a;


Then:
 
a = c;


That doesn't make sense: c is already a, so why do you set a to c?
Also, you don't need two do-while loops.

I think you probably meant this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

int main()
{
    double n1, n2, result; //n1 and n2 will be the two numbers, result will hold the result
    char sign, sign2; //sign will be the Math operator, sign 2 will be option to continue or exit
    
    do
    {
        //Get the 1st number
        std::cout << "Enter a number: ";
        std::cin >> n1;
        //Get the 2nd number
        std::cout << "Enter another number: ";
        std::cin >> n2;
        //Get the operator
        std::cout << "Enter an operator (+, -, * or /): ";
        std::cin >> sign;
        
        //Check the operator
        switch(sign)
        {
            //If its '+' then add
            case '+':
                result = n1+n2;
                break;
            case '-': //If its '-' then subtract
                result = n1-n2;
                break;
            case '*': //If its '*' then multiply
                result = n1*n2;
                break;
            case '/': //If its '/' then divide
                if(n2 != 0) //Divide if 2nd number isn't 0
                    result = n1/n2;
                else //If 2nd number is 0, give a Math error
                {
                    std::cout << "Math error: Div by 0!" << std::endl;
                    result = 0;
                }
                break;
            default: //If the Math operator isn't applicable, give an error message
                std::cout << "Invalid operation!" << std::endl;
                result = 0;
                break;
        }
        
        //Display the result
        std::cout << "Result: " << result << std::endl;
        
        //Get the option to continue or exit. Repeat the menu until the user enters a correct option
        do
        {
            std::cout << "\nEnter an option:\nN - New calculation\nE - Exit" << std::endl;
            std::cout << "Option: ";
            std::cin >> sign2;
            std::cout << "\n";
            if(!(sign2 == 'E' || sign2 == 'N'))
                std::cout << "Invalid option!" << std::endl;
        } while(!(sign2 == 'E' || sign2 == 'N'));
        
    } while(!(sign2 == 'E'));
    
    //The following will wait until the user presses ENTER. This is better than system("pause").
    std::cin.get();
    std::cin.ignore();
    
    return 0;
}
The point of
1
2
3
4
5
6
7
8
do
	{
		cin >> a;
		c = a;
		do
		{
			a = c;
			cin >> sign;

is attempt to compel my program to work again with previous result. As you could see, I want to have three options after obtaining first result: continue calculations with result that program have already yielded, start new calculation and end of program.
If user would choose 'Continue' - program assign previous result as 'a' and then do further calculations, if he would choose 'New', program offer to write new number and then assign this new number to result that we obtained before.
So, in this way we could repeat inner loop and avoid conflict with previous results, that could occur if user would choose new calculation. I hope now it's clear. Maybe another(more simple and descriptive) algorithm could exist.
Anyway, thanks a lot! I found a lot of clues in your code which could be useful!
Thanks for clarifying your question. To do what you want, you just need to modify my code by a bit:
1) Add an if statement before getting the first number to only get the first number if the user wants to do a new calculation.

2) After getting the options, if the option is 'C', set n1 equal to result.

Here is the modified one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>

int main()
{
    double n1, n2, result; //n1 and n2 will be the two numbers, result will hold the result
    char sign, sign2 = 'N'; //sign will be the Math operator, sign 2 will be option to continue or exit
    
    do
    {
        //Get the 1st number only if user wants to do a new calculation
        if(sign2 == 'N')
        {
            std::cout << "Enter a number: ";
            std::cin >> n1;
        }
        //Get the 2nd number
        std::cout << "Enter another number: ";
        std::cin >> n2;
        //Get the operator
        std::cout << "Enter an operator (+, -, * or /): ";
        std::cin >> sign;
        
        //Check the operator
        switch(sign)
        {
            //If its '+' then add
            case '+':
                result = n1+n2;
                break;
            case '-': //If its '-' then subtract
                result = n1-n2;
                break;
            case '*': //If its '*' then multiply
                result = n1*n2;
                break;
            case '/': //If its '/' then divide
                if(n2 != 0) //Divide if 2nd number isn't 0
                    result = n1/n2;
                else //If 2nd number is 0, give a Math error
                {
                    std::cout << "Math error: Div by 0!" << std::endl;
                    result = 0;
                }
                break;
            default: //If the Math operator isn't applicable, give an error message
                std::cout << "Invalid operation!" << std::endl;
                result = 0;
                break;
        }
        
        //Display the result
        std::cout << "Result: " << result << std::endl;
        
        //Get the option to continue or exit. Repeat the menu until the user enters a correct option
        do
        {
            std::cout << "\nEnter an option:\nN - New calculation\nC - Continue calculations\nE - Exit" << std::endl;
            std::cout << "Option: ";
            std::cin >> sign2;
            std::cout << "\n";
            if(!(sign2 == 'E' || sign2 == 'N' || sign2 == 'C'))
                std::cout << "Invalid option!" << std::endl;
        } while(!(sign2 == 'E' || sign2 == 'N' || sign2 == 'C'));
        
        //If user wants to continue
        if(sign2 == 'C')
            n1 = result;
        
    } while(!(sign2 == 'E'));
    
    //The following will wait until the user presses ENTER. This is better than system("pause").
    std::cin.get();
    std::cin.ignore();
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.