Thread: Newbie Needs Help on Calculator Program

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    12

    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
    {
    	cout<<"Please enter 'add', 'subtract', 'multiply',  or 'divide' as operation type";
    	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. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    remove 'goto' from the second line. Why did you put that over there ?
    -

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    12
    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. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Can you explain your reasoning behind that. You lost me.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    an if-else statement llooks like this:

    Code:
    if(condition)
    { // if the condition is true
        statement
    } else { // if the condition is false
        statement
    }
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    12
    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;
    
    	if(op=="add")
    	{
    		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
    	{
    		cout<<"Please enter 'add', 'subtract', 'multiply', or 'divide' as operation type";
    		goto cout<<"Enter operation type:\t";
    	}
    
    	return 0;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(op=="add")
    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;
    
    // more of your code
    
    else
    {
        cout<<"Please enter 'add', 'subtract', 'multiply', or 'divide' as operation type";
        goto hell; // follow the dark side of the source
    }

  8. #8
    hell... haha..

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    12
    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. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    12
    That sounds like a good idea Sean, but I'm not sure how to do that.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Lightbulb 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
    	{
    		cout<<"Please enter 'add', 'subtract', 'multiply', or 'divide' as operation type";
    		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 :
    float Add( float, float );
    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++.

    C++
    The best

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    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. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    249
    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...
    C++
    The best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM