Newbie C++ Question

This is a discussion on Newbie C++ Question within the C++ Programming forums, part of the General Programming Boards category; This is the first time I've ever touched C++ (More then basic Hello World anyways). For my first program I ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    3

    Newbie C++ Question

    This is the first time I've ever touched C++ (More then basic Hello World anyways). For my first program I decided to make a very basic calculator that can add, divide, multiply, and subtract. I got it to run fine, but I'm looking for a way to restart the program from the "Input first number" line. I'm working with a C++ book six years old, and web tutorials, and both said the goto command should be avoided. But so far that's all I've found. Thanks in advance. (Also if you could point out any glaring beginner mistakes that I may have made, I would appreciate it. Every little bit of advice helps.)


    Code:
    #include <iostream>
    using namespace std;
    //Functions for operation
    int addition (int a, int b)
    {
      int r;
      r=a+b;
      return (r);
    }
    
    int subtract (int a, int b)
    {
      int r;
      r=a-b;
      return (r);
    }
    
    int multiply (int a, int b)
    {
      int r;
      r=a*b;
      return (r);
    }
    
    int divide (int a, int b)
    {
      int r;
      r=a/b;
      return (r);
    }
    
    
    int main(int argc, char *argv[])
    {
        int a, b, sum, op; //Declares user input number, one, two, sum, and the operator.
        
        restart: //Used to restart program
    cout << "Enter first number: ";
    cin >> a;
    cout << "Enter your operator:(1 adds, 2 subtracts, 3 multiplies, 4 divides)\n";
    cin >> op;
    cout << "Enter second number:";
    cin >> b;
    
    if (op == 1) {
      sum = addition(a,b);  
      cout << "The Sum Is: " << sum << "\n";
      goto restart;
      }
    else if (op == 2) {
      sum = subtract(a,b);
      cout << "The Sum Is: " << sum << "\n";
      goto restart;
      }
    else if (op == 3) {
      sum = multiply(a,b);
      cout << "The Sum Is: " << sum << "\n";
      goto restart;
      }
    else if (op == 4) {
      sum = divide(a,b);
      cout << "The Sum Is: " << sum << "\n";
      goto restart;
      }
      else {
      cout << "Invalid Operator";
    }
          system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    put the while thing in a big loop that brings it back to that point

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Use a do...while loop with a Boolean variable. Check the tutorials on this site.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    3
    Ah thanks, wouldn't have thought of a loop for that purpose. It works fine using a do...while boolean loop.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    just a small recommendation. why dont you use a char as your operator instead of a number, so that you have + = addition - = subtraction * = multiplication / = division
    be a little more like a calculator.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    3
    I actually tried that, and I got a compiler error. I assumed you couldn't have something like

    Code:
    x == +

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    you would want to declare a char variable,
    and use that for your operator.
    then do something like this

    Code:
    char operator;
    
    if(operator == '+')
    {
              ...
    }
    
    else if(operator == '-')
    {
             ...
    }
    something like that

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    1
    little Tip:

    NEVER use GOTO!!! NEVER EVER!!!

  9. #9
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    big tip:

    Never listen to VillageIdiot. Never ever.



    Seriously, banning goto without thinking is stupid. It's like never using a hammer because you accidentally hit your thumb once.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Hammers are often the right tool for the job, gotos are rarely the right tool for the job, especially in C++. I won't say "never" use them, but I would say that if you find yourself using them then it might be a good time to learn the better alternatives (like a while loop in this case).

  11. #11
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    See? That's a good way to look at it, that's how I look at it too. And it's pretty different in meaning than "NEVER use GOTO!!! NEVER EVER!!!", right?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> And it's pretty different in meaning than "NEVER use GOTO!!! NEVER EVER!!!", right?
    Yup. That was part of my point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 03:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21