Hi. I'm having trouble making a simple calculator.
The code I have so far compiles, but it does not run.
Right now I only have addition in because I wanted to make sure I was going in the right direction before I added anything else.
I have to use a Do...while statement in this code.
Can anyone tell me when the program doesn't run???
Here is what I have so far:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
	char sign;
	string input;
	string total=0;
	cout << "Current total is 0" << endl;
	do
	{	cout << "Enter an operation: + - * / (or enter X to exit):";
		cin >> sign;
		if (sign = '+')
			cout <<"Enter a number: ";
			cin >> input;
			cout <<"Current total is " << input + total;
	}
		while (input != "X");
}

Thanks in advance.

#include <iostream>
#include <string>
using namespace std;
 
int main ()
{
	char sign;
	string input;
	int total=0; // No need for a string, is there?
	cout << "Current total is 0" << endl;
	do
	{	cout << "Enter an operation: + - * / (or enter X to exit):";
		cin >> sign;
		if (sign == '+')
                {
 	                cout <<"Enter a number: ";
			cin >> input;
			cout <<"Current total is " << input + total;
                 }
	}while (input != "X");
}

I wouldn't use string unless you know how to use it. Use char arrays. :)

Some starters: Sort out your confusion about strings and numbers. Assignment is = , comparison is == . Multiline blocks are not determined by indentation in this curly-brace language.

I don't know how to use arrarys yet :(

Some starters: Sort out your confusion about strings and numbers. Assignment is = , comparison is == . Multiline blocks are not determined by indentation in this curly-brace language.

I was trying to use double to identify the input..but I can't use "X" because it is not a number.

#include <iostream>
#include <string>
using namespace std;
 
int main ()
{
	char sign;
	int input;
	int total=0; // No need for a string, is there?
	cout << "Current total is 0" << endl;
	do
	{	cout << "Enter an operation: + - * / (or enter X to exit):";
		cin >> sign;
		if (sign == '+')
                {
 	                cout <<"Enter a number: ";
			cin >> input;
			cout <<"Current total is " << input + total;
                 }
	}while (input != "X");
}

Actually, there is no need to use string since you're only inputting a number. Char array:

char name[255];

Strings:

string sentence = "Hello my name is Phil and I hate C++";

where as using single char, would just print out "H"
:)

I was trying to use double to identify the input..but I can't use "X" because it is not a number.

So take input as a string, and if it's not "X", then convert it to a number.

So take input as a string, and if it's not "X", then convert it to a number.

I don't know how to convert a string to a number.. I tried looking it up but it looks complicated.

Getting 10 errors in the build current. I think it's because of the input problem with the while statement.

What errors?

I don't know how to convert a string to a number.. I tried looking it up but it looks complicated.

Getting 10 errors in the build current. I think it's because of the input problem with the while statement.

Converting a string to a number is just about the most frequently asked question. Surely even a rudimentary search would give you a hint. (Part of learning to code is learning how to find answers. This is a good time to start, I'd say.)

What errors?

(26) : error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<

etc, etc, I don't know what to make of it.

I have the program running now with all the operators working. I still cannot get the program to end on "X" and I am having trouble making it display a cout "Cannot divide by zero".

I have the program running now with all the operators working. I still cannot get the program to end on "X" and I am having trouble making it display a cout "Cannot divide by zero".

Hm. If only there were some way for the rest of us to see the code you have as currently modified. Perhaps you might consider, I dunno, posting it?

Why can't you just use char for sign like you are and if that is x then stop and for input and total I would just use integers.

#include <iostream>

using namespace std;

int main()
{
	char sign;
	bool end = false;
	int total = 0, input;
	
	cout << "Current total is: " << total << endl;
	do
	{
		cout << "Enter an operator ( + - * / or 'x' to quit ): ";
		cin >> sign;
		if( sign == 'x' || sign == 'X' )
		{
			end = true;
		}
		else if( sign == '+' )
		{
			cout << "Enter a number: ";
			cin >> input;
			total += input;
		}
		else if( sign == '-' )
		{
			cout << "Enter a number: ";
			cin >> input;
			total -= input;
		}
		else if( sign == '*' )
		{
			cout << "Enter a number: ";
			cin >> input;
			total *= input;
		}
		else if( sign == '/' )
		{
			cout << "Enter a number: ";
			cin >> input;
			if( input == 0 )
			{
				cout << "Cannot divide by zero!" << endl;
			}
			else
			{
				total /= input;
			}
		}
		else
		{
			cout << "Invalid operator!" << endl;
		}
		cout << "Total is: " << total << endl;
	}while( !end );
	
	system("PAUSE");
	return 0;
}

I was eating and working on the program. I already finished the program earlier. I was mixing up the input and the signs in the program...didn't need to switch from an int to a char after all :P It was simple after that. Thanks guys.

Yeah I changed something in my post before testing it and it actually doesn't work the way it should. Good job getting it done.

Yeah I changed something in my post before testing it and it actually doesn't work the way it should. Good job getting it done.

Thanks. I tend to make things more complicated then they actually are...

Why not use a switch statement instead of if statements?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.