Do and while loops.

Pages: 12
Jul 8, 2010 at 2:04am
I can't seem to get my do and while loop to work. For this assignment, I have to make a calculator that will keep looping with functions + - * and /. Here's what I have so far.

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
#include <iostream>
#include <string>
using namespace std;

int main()
{

	double total = 0, number;
	string math;

	do
	{
	cout << "Current total is " << total << endl;
	cout << "Enter an operation: + - * / (or enter X to exit): " ;
	cin >> math; 
	cout << "Enter a number: " ;
	cin >> number;
	
	} while ((math != "x") && (math != "X") && (math == "+") && (math == "-")
		&& (math == "*") && (math == "/"));

	if (math == "+")
	{
		cout << total + number << endl;
	}

	if (math == "-")
	{
		cout << total - number << endl;
	}

	if (math == "*")
	{
		cout << total * number << endl;
	}

	if (math == "/")
	{
		cout << total / number << endl;
	}

}


So far, I have it asking for the mathematic symbol and then doing the equation with a number. However, it wont loop after one try! I can't figure out how to fix it. Any ideas?
Jul 8, 2010 at 2:11am
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
#include <iostream>
#include <string>
using namespace std;

int main()
{

	double total = 0, number = 0;
	string math;

	do
	{
		cout << "Current total is " << total << endl;
		cout << "Enter an operation: + - * / (or enter X to exit): ";
		cin >> math;
		cin.ignore();
		if (math == "X") break;
		cout << "Enter a number: ";
		cin >> number;
		cin.ignore();
	} while ((math != "x") || (math != "X") || (math == "+") || (math == "-")
		&& (math == "*") || (math == "/"));

	if (math == "+")
	{
		cout << (total + number) << endl;
	}

	else if (math == "-")
	{
		cout << (total - number) << endl;
	}

	else if (math == "*")
	{
		cout << (total * number) << endl;
	}

	else if (math == "/")
	{
		cout << (total / number) << endl;
	}
	
	

}


I can't get it to store the total and keep it as a running total. ARGH!
Last edited on Jul 8, 2010 at 2:52am
Jul 8, 2010 at 3:18am
Anybody? :|
Jul 8, 2010 at 3:24am
You only ever assign a value to total when you intialize it. Instead of total + number, you should say total += number. The same goes for the other operations.
Jul 8, 2010 at 3:36am
Single character constants should also be in ' 's as opposed to " ", i.e. '+', '-', '='.

Your while loop logic is also flawed - how can math be both + and * at the same time? I would make math a character as opposed to string as it should only be holding one operator as that is its intended use.

Your calculator also won't loop - once it somehow gets out of the do while loop and fulfills the if loops, that's the end of the program. I believe the if loops should be inside your do while loop.
Jul 8, 2010 at 3:41am
With some help from a friend, I have it adding up the numbers, but it's inputting odd values and formatting.
Here's the code:

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
#include <iostream>
#include <string>
using namespace std;

int main()
{

	double total = 0, number = 0;
	char math;

	do
	{
		cout << "Current total is " << total << endl;
		cout << "Enter an operation: + - * / (or enter X to exit): ";
		cin >> math;
		cin.ignore();
		if (math == 'X') break;
		cout << "Enter a number: ";
		cin >> number;
		cin.ignore();
	
	if (math == '+')
	{
		total = total + number;
		cout << total + number << endl;
	}

	else if (math == '-')
	{

		total = total - number;
		cout << (total - number) << endl;
	}

	else if (math == '*')
	{
		total = total * number;
		cout << (total * number) << endl;
	}

	else if (math == '/')
	{
		total = total / number;
		cout << (total / number) << endl;
	}
	
	} while ((math != 'x') || (math != 'X') || (math == '+') || (math == '-')
		|| (math == '*') || (math == '/'));

	

}


The input will be like:
[img] http://i27.tinypic.com/2z9il9h.jpg [/img]
Last edited on Jul 8, 2010 at 4:04am
Jul 8, 2010 at 3:46am
How about putting all those if statements inside the loop? And if you want to use character literals as bluezor suggested, you need to change variable math's type to char.
Jul 8, 2010 at 4:04am
Ok, I put them into the loop and I also changed the variable type. I don't understand why I'm getting the odd formatting between with the number though.
Jul 8, 2010 at 4:10am
Just think for a minute. You're changing total's value (you really should use the proper operator as I suggested, so you don't have to type "total" twice) and then, instead of just outputting total, you're outputting the result of performing the operation again.

So, instead of, say cout << total + number << endl; you really want to say cout << total << endl;
Jul 8, 2010 at 4:24am
I'm sorry but that makes absolutely no sense to me.

If I change total + number to total += number, I get an error that reads:
STD::basic_ostream<_Elem, _Traits<

I'm trying really hard to reread and understand all of this but isn't making sense to me.

You only ever assign a value to total when you intialize it. Instead of total + number, you should say total += number. The same goes for the other operations.


I tried changing them, and I get that error.
Jul 8, 2010 at 4:31am
Let me be clear. You should change this:

total = total + number;

to this:

total += number;

It means exactly the same thing; operator+= just makes things easier and more direct.
Jul 8, 2010 at 4:35am
Oh okay, I didn't realize I had the formula written twice. Forgive me, I've been wanting to rip my hair out for hours :/

I've made the changes, but it's still outputting in the awkward way, though displaying the right answer.

Enter sign
Enter number
x
Total is x

Also, for some reason, when I try and add a line of code that will detect when the user divides and the output gives me -1.#IND.

1
2
3
4
5
else if ((math == '/') && (number == '0'))
	{
		cout << "No." << endl;
	}
	


I'm having a bad night.
Last edited on Jul 8, 2010 at 4:36am
Jul 8, 2010 at 4:38am
For numbers, you don't need the ' 's.

' ' is for characters.
" " is for strings.
Jul 8, 2010 at 4:40am
Okie, removed them and still get the funky output.
Jul 8, 2010 at 4:40am
I would suggest just putting the number check inside of this else if():

1
2
3
4
5
6
7
8
9
else if (math == '/')
{
	if(/*number is 0*/) {
		//oh crap
	} else { //we are ok
		total = total / number;
		cout << (total / number) << endl;
	}
}
Jul 8, 2010 at 4:45am
but it's still outputting in the awkward way

What exactly do you think is awkward about it?

Also, for some reason, when I try and add a line of code that will detect when the user divides and the output gives me -1.#IND.

Notice this:

number == '0'

number is a double, not a char. So we test for the double 0.0, not for the character literal '0':

number == 0.0

Remember to test before you perform the division.
Jul 8, 2010 at 4:49am
Jul 8, 2010 at 4:53am
If you don't want that output, then remove the corresponding line from the if statements. In case you're in doubt, it's this line:

cout << total << endl;
Jul 8, 2010 at 4:56am
Wow, I feel so stupid. I made such a mistake taking this class online.. my instructor seriously sent out an e-mail saying we're only allowed to ask 3 questions the entire term.

Thank you so much filipe.. I really should pay you for this! Haha.

Here's what I'm getting, along with my code, with the 0.0

http://i32.tinypic.com/30ag7ba.jpg

This is before I took out the repeated input I was trying to get rid of too. So don't mind that part :P
Last edited on Jul 8, 2010 at 4:59am
Jul 8, 2010 at 4:59am
I'm glad to help.

To fix that you need to test for that 0.0 before performing division. The statement you just added will never be executed if math == '/'. You should do what firedraco suggested.
Pages: 12