Quantcast

Jump to content


Photo

Looking for C++ Help!


  • Please log in to reply
3 replies to this topic

#1 Supernature

Supernature
  • 87 posts

Posted 07 July 2010 - 06:45 PM

Hey guys, I'm currently taking a c++ class at school and I'm working on an assignment that has me really confused. I can't decide what to do in order to execute my program.

I have to create a program that acts as a computer. You start with a total of zero, then ask for an arithmetic function with an additional number. So, let's say I am going to add 7. It would then keep a running total using a do..while loop of the transactions, and then cancel if the user inputs X.

Here is my code:
#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 the total to work. It keeps giving me a 0 total. Any ideas?

Edited by Supernature, 07 July 2010 - 06:53 PM.


#2 aweroij

aweroij
  • 44 posts

Posted 07 July 2010 - 08:01 PM

The variable total is never assigned a new value.

Something like this would be needed for each operation.
	if (math == "+")
	{
		total = total + number;
		cout << total << endl;
	}

Checking for division by zero would be wise.

#3 Faval

Faval
  • 637 posts

Posted 10 July 2010 - 11:03 AM

while ((math != "x") || (math != "X") || (math == "+") || (math == "-") && (math == "*") || (math == "/"))
Also, the && is probably a typo and should be an || statement instead if you want it to loop more than once.

Edited by Faval, 10 July 2010 - 11:04 AM.


#4 Supernature

Supernature
  • 87 posts

Posted 10 July 2010 - 02:52 PM

Thanks guys! Totally makes sense to me now!


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users