Calculator in while loop

May 12, 2020 at 12:50pm
Hello There, im trying to create a calculator in while for an assignment but im stuck on something, i have done the addition and sub and multiplication but im stuck on divsion, so basically what im trying to do is to let the user input numbers to divide on, until he input a specific number to exit the loop but i always get the output incorrect

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
#include <iostream>

using namespace std;

int main()
{
    
    bool loop = true;;
    int value;
    float divsion;
    
    while (loop) {
        cout << "Please Enter a Value, 1 to calculate: ";
        cin >> value;
        if (value != 1) {
            divsion = divsion / value;
        }
        else if (value == 1) {
            loop = false;
            cout << "Your Calculation is: " << divsion << endl;
        }
    }

    return 0;
}
Last edited on May 12, 2020 at 12:50pm
May 12, 2020 at 1:04pm
You only ever “initialize” value. Division is just set to some garbage value.
May 12, 2020 at 1:10pm
extra ; (harmless) line 8
use double, not float, unless you have a reason to use float.
what is the value of division? You don't read it in or set it anywhere, its random garbage. Don't know what answer you expected but this is why your answer does not match what is in your mind.
1 to calculate makes no sense in the printout. 1 exits; its 1 to exit and anything else calculates.
a do-while loop gives full controls over 'loop' variable to the loop rather than rely on it being set and managed outside the loop. That is minor here, but important in complex programs where the setting and consumption of 'loop' could be farther apart.

anyway, all that to say fixing the value of division to be read in from the user is what you needed to do to get it working. The rest is just minor cleanup stuff.
May 12, 2020 at 2:33pm
im not sure what garbage value is, but i set value to 1 still nothing, also i still didnt take do while loop in the course, im still a beginner
May 12, 2020 at 2:36pm
Your division variable is never set to anything specific, so it is just a random number. To fix that, set division equal to something before you divide it by value.
May 12, 2020 at 2:48pm
i set it to 1, still the same error
Last edited on May 12, 2020 at 2:49pm
May 12, 2020 at 2:49pm
Could you send us the new code?
May 12, 2020 at 2:52pm
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
#include <iostream>

using namespace std;

int main()
{
    
    bool loop = true;;
    int value = 0;
    float divsion = 1;
    
    while (loop) {
        cout << "Please Enter a Value, 1 to calculate: ";
        cin >> value;
        if (value != 1) {
            divsion = divsion / value;
        }
        else if (value == 1) {
            loop = false;
            cout << "Your Calculation is: " << divsion << endl;
        }
    }

    return 0;
}
May 12, 2020 at 2:57pm
Hum.. maybe the functionality your looking for is more like 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
#include <iostream>

using namespace std;

int main()
{
    
    bool loop = true;;
    int value = 0;
    float divsion = 1;
    
    while (loop) {
        cout << "Please Enter a Value, 1 to calculate: ";
        cin >> value;
        if (value != 1) {
            if(division == 1) {
                division = value;
                continue;
            }
            divsion = divsion / value;
        }
        else if (value == 1) {
            loop = false;
            cout << "Your Calculation is: " << divsion << endl;
        }
    }

    return 0;
}
May 12, 2020 at 3:12pm
its working mate, thank you very much, was struggling with it all week, searching the whole web without any solution, i appreciate it
May 12, 2020 at 3:22pm
Yw 👍
May 12, 2020 at 3:41pm
Hello RaiN3772,

Just another idea for you to look at.

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
#include <iostream>
#include <iomanip>
#include <limits>

//using namespace std;  // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
using std::cin;
using std::cout;
using std::endl;
using std::string;


int main()
{

	bool loop = true;
	double demonator{};
	double numerator{};
	double result{};

	std::cout << std::fixed << std::setprecision(6);

	while (loop)
	{
		cout << "\n Please Enter a numerator, 1 to exit: ";
		
		if (cin >> numerator && numerator != 1.0)
		{
			std::cout << " Please enter the denomoter: ";
			cin >> demonator;
		}

		if (numerator != 1.0 && demonator != 0.0)  // <--- Can not divide by (0).
		{
			result = numerator / demonator;
		}
		else if (numerator == 1.0)
		{
			loop = false;
			cout << "\n Your Calculation result is: " << result << endl;
		}
		else
			std::cout << "\n     Can not divide by (0)! Try again.\n";
	}

	// A fair C++ replacement for "system("pause")". Or a way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
}


Andy
May 12, 2020 at 5:56pm
Hello Andy, thanks for your reply, really helpful, but unfortunately i'm required to use "using namespace std" in my code because this is an assignment for my uni, and my professor somehow doesn't accept another method for a reason i don't know, i coded my first assignment in a better and optimized way, but he didn't give me the mark i wanted it and he said "that's not how to code a c++ program", then i started to questioning his knowledge.
Anyway thank you
Last edited on May 12, 2020 at 6:31pm
May 12, 2020 at 8:38pm
Hello RaiN3772,

somehow doesn't accept another method for a reason i don't know
Because he is lazy and does not teach what you should be learning from the start. Also the classes may be designed in a way that puts off teaching some parts of the language.

"that's not how to code a c++ program", then i started to questioning his knowledge.
Good idea. Question everything. There is more than one way to code a C++ program and this forum is a good place for ideas from seasoned people who have been doing this for many years. Your professor may have been in the class room so long that he is stuck on what the text book says and not willing to go beyond. As an example:
1
2
3
4
5
6
7
8
bool cont{ true };

if (cont == true)

and

if (cont)

Are exactly the same. The second version is a shorter way to make use of the bool variable. If "cont" is set to "false" (0) zero the if statement will be bypassed. Otherwise it is "true" and you enter the if statement.

I can see where the code I presented may be going beyond what has been taught so far, but the concept of checking for invalid input is good to start learning early. In the future up to half of your code will be checking for something that may go wrong. This may take the second class or beyond, (3rd or 4th), before you get to validating your input. Any practice now will help you in the future.

You may have to learn what you can and remove anything that does not go along with what he is teaching to receive the best marks.

Andy
Last edited on May 12, 2020 at 8:39pm
May 12, 2020 at 9:31pm
i agree with what you said, like everything, i'm trying to improve my skills as much as i can in this site, also i'm required to use his method as much as i can, anyway i'll use my method to do my personal programs, and you guys are really helpful, thank you very much.
Topic archived. No new replies allowed.