Thread: Newbie Needs Help on Calculator Program

1. Newbie Needs Help on Calculator Program

I was working on a calculator type program in which you type the operation you want, along with two numbers to perform the operation on. The problem is that I put this line:
Code:
```else
{
goto cout<<"Enter operation type:\t";
}```
at the end to make sure that the user types in a correct operation. It is giving me 2 syntax errors in the goto line:

syntax error : '('
syntax error : missing ';' before ')'

If anyone can help me, I would be very appreciative. I don't think it matters but I'm using MSVC++ 6.0

2. remove 'goto' from the second line. Why did you put that over there ?

3. I put the goto there so that if the user inputs anything besides "add", "subtract", "multiply", or "divide" for the operation type that it will ask them to input it correctly, i.e. if they put "addition" instead of "add"

4. Can you explain your reasoning behind that. You lost me.

5. an if-else statement llooks like this:

Code:
```if(condition)
{ // if the condition is true
statement
} else { // if the condition is false
statement
}```

6. Can you explain your reasoning behind that. You lost me.
I am making a program that calculates 2 numbers. You input the operator by typing "add", "subtract", "multiply", or "divide". If the user inputs something besides one of these words, the program tells the user what to input, and goes back to the operator entry point.
an if-else statement llooks like this:
I am aware of that, Okiesmokie. I only put the part of the code that was defective. But, to make you happy, here is the entire source code.
Code:
```#include <iostream.h>

int main()
{
char op[9]   = "";
int  firstNo = 0;
int  secNo   = 0;

cout<<"Enter first number:\t";
cin>>firstNo;
cout<<"Enter second number:\t";
cin>>secNo;
cout<<"Enter operation type:\t";
cin>>op;
cout<<endl;

{
cout<<firstNo<<" + "<<secNo<<" = "<<firstNo + secNo;
}
else if(op=="subtract")
{
cout<<firstNo<<" - "<<secNo<<" = "<<firstNo - secNo;
}
else if(op=="multiply")
{
cout<<firstNo<<" * "<<secNo<<" = "<<firstNo * secNo;
}
else if(op=="divide")
{
cout<<firstNo<<" / "<<secNo<<" = "<<firstNo / secNo;
}
else
{
goto cout<<"Enter operation type:\t";
}

return 0;
}```

Should be
if ( strcmp(op,"add") == 0 )

And
> goto cout<<"Enter operation type:\t";

Have you learnt about while loops yet?
This is just begging for a while loop.
I strongly suggest you learn about while loops.

If you're absolutely set on using goto (did I mention while loops?), then its
Code:
```hell:
cout<<"Enter operation type:\t";
cin>>op;
cout<<endl;

else
{
goto hell; // follow the dark side of the source
}```

8. hell... haha..

9. Thank you, Salem. I have this great idea all of the sudden to use a while loop. It just kinda came to me while i was reading your reply.

10. Can I offer some advice on how to make the program as a whole better? Have it input a string, and then seperate that string into "tokens" at each space. Then determine the operation from what symbol is contained in the second token, and perform it on the other two.

11. That sounds like a good idea Sean, but I'm not sure how to do that.

12. Some ideas

Originally posted by CompWiz84

a program that calculates 2 numbers. using one operator.
Code:
```#include <iostream.h>

int main()
{
char op[9]   = "";
int  firstNo = 0;
int  secNo   = 0;

cout<<"Enter first number:\t";
cin>>firstNo;
cout<<"Enter second number:\t";
cin>>secNo;
cout<<"Enter operation type:\t";
cin>>op;
cout<<endl;

if(op=="add") // Why don't you use swich
{
cout<<firstNo<<" + "<<secNo<<" = "<<firstNo + secNo;
}
else if(op=="subtract")
{
cout<<firstNo<<" - "<<secNo<<" = "<<firstNo - secNo;
}
else if(op=="multiply")
{
cout<<firstNo<<" * "<<secNo<<" = "<<firstNo * secNo;
}
else if(op=="divide")
{
cout<<firstNo<<" / "<<secNo<<" = "<<firstNo / secNo;
}
else
{
goto cout<<"Enter operation type:\t";
}

return 0;
}```
OK ... If you want to be a good programmer you should do this:
1. make function that calculate the resulte :
Double mult ( float, float);
etc.
2. use swith loop, insted of " if else statements";
3. use a loop to make the user enter a choise to make another calculation or not...
4. if you can ... make a specific class for the calculation functions.

5. have fun with C++.

13. I have recently started to study how computers work when adding bits and get the sum successfully, by using so called "half-adders" and "full-adders", very interesting, I think.

Maybe this could be used to advantage, although, I don't know how to work with bits. Here I have copied the model for a half adder:

Code:
```bit x=0,y=1;
bit result[1];

result[0] = x&y;
result[1] = x^y;

cout << result[0] << result[1];```

14. Originally posted by Zewu
[B]I have recently started to study how computers work when adding bits and get the sum successfully, by using so called "half-adders" and "full-adders", very interesting, I think.
Well Good for you...