GetOperator() and GetOperand() functions

My question is how can i implement code into this that uses a GetOperator() and a GetOperand() function to call into my main function to see if its bad input or not? This is 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
#include<iostream>
#include<string>
using namespace std;

int main()
{
float total, num;
char operat;
	total=0;
	do
	{
		cout<<"Please input your expression starting with an operand and type in '=' when completed ";
		cin>>num;
		if(operat=='-')
		{
			total=total-num;
		}
		else
		{
			total=total+num;
		}
		cout<<"Please insert a minus or addition operator ";
		cin>>operat;
	}while(operat!='=');
		cout<<total;
		return 0;
}


Last edited on
Line 8: operat is an uninitialized variable.

Line 14: You're comparing an uninitialized variable.

Here's a simple GetOperand() function:
1
2
3
4
5
6
7
float GetOperand ()
{   float num;

    cout << "Enter operand: ";
    cin >> num;
    return num;
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Topic archived. No new replies allowed.