need help with my basic c++ calculator

This is a discussion on need help with my basic c++ calculator within the C++ Programming forums, part of the General Programming Boards category; im brand new to c++, so i was wondering if someone could help me with my calculator program. it starts ...

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

    need help with my basic c++ calculator

    im brand new to c++, so i was wondering if someone could help me with my calculator program.

    it starts out by printing this on the screen:

    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division
    Choose one:

    The user chooses one of the operations by entering the number.

    It runs fine and all, except for one thing. When the user inputs a character or a string of characters, the program starts repeating itself at a very rapid rate.

    instead of getting all screwed up, i am trying to figure out how to make the program print an error message like: "Thats a letter, not a number" or something.

    the problem is, my variable for the user input is a float variable. is there some way for a variable or an array to be able to handle 2 different types, such as char and int.

    ok i hope that came across in an understandable way

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    127

    Re: need help with my basic c++ calculator

    Originally posted by iCouch_Potato



    the problem is, my variable for the user input is a float variable. is there some way for a variable or an array to be able to handle 2 different types, such as char and int.

    I dont think so

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You could something like this to make sure user enter valid input
    Code:
    int choice;
      
    cout << "Enter choice:";
    while (!(cin >> choice)) {
        cin.clear();
        cin.ignore(1000, '\n');
        cout <<"Enter choice:";
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: need help with my basic c++ calculator

    Originally posted by iCouch_Potato
    It runs fine and all, except for one thing. When the user inputs a character or a string of characters, the program starts repeating itself at a very rapid rate.
    Stop using scanf(). It has terrible error recovery.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220

    Re: need help with my basic c++ calculator

    >im brand new to c++, so i was wondering if someone could help me with my calculator program.

    Ahh, we always have to start somewhere =) Common sense and not throwing your computer out the window solves everything.


    >it starts out by printing this on the screen:

    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division
    Choose one:


    Do you have any error checking in your source documentation for the input?

    >The user chooses one of the operations by entering the number.

    After you enter in the number, where does it store the data, what type is it, and how does it deal with it?



    >It runs fine and all, except for one thing. When the user inputs a character or a string of characters, the program starts repeating itself at a very rapid rate.

    You need to setup code that will check if the number is a number If it's not then don't perform the calculations.



    >instead of getting all screwed up, i am trying to figure out how to make the program print an error message like: "Thats a letter, not a number" or something.
    Given that 'num' is the variable where the integer input value will be stored, your code might look like this:
    Code:
    do
    {
    if (num >=1 && num <= 4)
    {
         //Process data here.
    }
    else 
         std::cout << "\n\nError:Enter in a valid type.";
    }
    }while(continueCalc == true);
    >the problem is, my variable for the user input is a float variable. is there some way for a variable or an array to be able to handle 2 different types, such as char and int.
    ok i hope that came across in an understandable way


    No, but thenagain, you could make an overloaded function to receive certain types and act on whichever data-type is passed into the function.

    ----------------------------------------------------------------------------

    Without the source, this is all I could come up with.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Calculator I made check it out if you want...
    By Sshakey6791 in forum C++ Programming
    Replies: 8
    Last Post: 01-07-2009, 11:20 PM
  2. Basic Calculator
    By Surge in forum C Programming
    Replies: 18
    Last Post: 12-02-2006, 09:20 PM
  3. Basic calculator program in C++
    By linkofazeroth in forum C++ Programming
    Replies: 70
    Last Post: 08-28-2005, 04:23 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

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