Results 1 to 2 of 2

Thread: simple Calculator script, easy fix hopefully?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1
    Thanks
    0
    Thanked
    0 times in 0 posts

    Wink simple Calculator script, easy fix hopefully?

    Alright, so I just started learning c++ a few hours ago with no prior programming experience, and i'm working on a somewhat simple (probably more complicated than it needs to be) calculator script.
    Here's the code I have so far (assuming i get the code tags right lol):
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        int a, b, c, d, e, f;
        int resultadd, resultsub, resultmul, resultdiv;
        int add, subtract, multiply, divide;
        
        resultadd = a + c;
        resultsub = a - d;
        resultmul = a * e;
        resultdiv = a / f;
      
        cout<<"Welcome to my first calculator script. Please enter a number: ";
        cin>>a;
        cin.ignore();
        cout<<"You entered the number "<<a<<".\n";
        cout<<"Would you like to add, subtract, multiply, or divide? \n";
        cin>>b;
        cin.ignore();
        if (b==add)
        {
                   cout<<"What number would you like to add to "<<a<<"?\n";
                   cin>>c;
                   cin.ignore();
                   cout<<"Your answer is "<<resultadd<<".\n";               
                   }
        else if (b==subtract)
        {
             cout<<"What number would you like to subtract from "<<a<<"?\n";
             cin>>d;
             cin.ignore();
             cout<<"Your answer is "<<resultsub<<".\n";
             }
       else if (b==multiply)
       {
            cout<<"What number would you like to multiply "<<a<<" with?\n";
            cin>>e;
            cin.ignore();
            cout<<"Your answer is "<<resultmul<<".\n";
            }
       else if (b==divide)
       {
            cout<<"What number would you like to divide "<<a<<" by?\n";
            cin>>f;
            cin.ignore();
            cout<<"Your answer is "<<resultdiv<<".\n";
            }
       else
       {
           cout<<"I'm sorry, what? Please try again.\n";
           cin>>b;
           cin.ignore();
           }
           
           cout<<"Thank you for using my calculator script!";
         cin.get();
    }
    Now, my problem is that my compiler doesn't show that there are any problems, yet when i run the program, it quits after I get to the part where it says "Would you like to add, subtract, multiply, or divide?" like it can't recognize what I am saying. Any suggestions here guys? I've been doing my research and i'm still stumped ;|

  2. #2
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,494
    Thanks
    335
    Thanked
    313 times in 249 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre

    Re: simple Calculator script, easy fix hopefully?

    A quick look through shows that you are using the wrong data type for some things, and are also not initialising some data types.

    at the top you are declaring your variables - a,b,c,d,e,f, resultadd, add etc etc.

    The first thing in each of those three lines, is the type of that variable. You have declared the type of all of your variables to be of type "int", which stands for "Integer" (a whole, positive number). So that int could be 1, 10, 1000 and so on.

    Your first question to the user is to request a number, eg 1. Since "cin" recognises that 1 is an integer, it will convert the input to an int, and your program will store 1 in variable a, and move on - thats fine.
    Your second question is asking the user to type "add", "subtract", "multiply" or "divide". The user will type one of these in and then your program will try and put "add", which is of type string, into b, which is of type integer. You can't do this - so you get an exception, and your program will crash as you have no error handling.

    So to fix that problem you have two options. The lazy way that lots of people do when learning a language is to provide numbered options for the user - for example "press 1 for add, 2 for multiply" and so on. Then, where you are testing "if b == add" you would change that to "if b == 1" etc. That should fix that problem.

    The better way to do it is to use different data types - strings.

    change variable b to become a string first of all, so move it to its own line, and declare it as so:

    Code:
     string b = "";
    This creates an empty string, with the name b. so now your program will not crash when a user enters the word "add" etc. It will however fail when you try to check what the user has typed.

    This is because of two things - 1) You have declared add, subtract etc as integers when they should really be strings, and 2) you have not initialised them to a value. So, change the "int add" etc line to be :

    Code:
     
    string add = "add";
    string subtract = "subtract";
    string multiply = "multiply";
    string divide = "divide";
    That should solve that problem

    Your final problem will be with your "resultadd" sections. You are doing this at the top, which is possible to do if you use pointers and some fun tricks, but the way you have it at the moment will not work. It is simple to fix though - I would personally get rid of those variables all together, and calculate the answer in each of your if statement blocks. As an example, for adding I would do something like:

    Code:
    count << "Your answer is: " << (a+c) <<". \n";
    Note however that you have no error testing at all there, so if you are expecting a number and someone types in "BOOBIES" then it will crash. The same problem doesn't exist for when you are expecting a string (as pretty much anything can be converted to a string), however I notice that you might have a problem with your "I'm sorry, what?" aspect, as you are not using a loop. Therefore, if someone types in an unrecognised word, they will get asked one more time, and then your program will exit without calculating the answer.

    The way around this problem is to use a while loop and a boolean flag. The flag indicates if the user has input rubbish or a recognised word. If they have input rubbish then it will display your "sorry" message, and then ask them to try again, and will keep doing so until they input some recognised text. For example:

    Code:
    bool bFlag = true;
    
    while(bFlag)
    {
    <.. your code here..>
    if (b == add)
    {
                   cout<<"What number would you like to add to "<<a<<"?\n";
                   cin>>c;
                   cin.ignore();
                   cout<<"Your answer is "<<resultadd<<".\n";               
                   bFlag = false;
    }
    <.. more code like above..>
       else
       {
           cout<<"I'm sorry, what? Please try again.\n";
           cin>>b;
           cin.ignore();
           bFlag = true;
     }       
    
    }
    What the above code does, is run your script while the boolean flag, bFlag, is set to true. So, if the user enters some text that your program recognises, the bFlag is set to false, and the program will work out the answer and then exit, as you expect. If they enter an incorrect answer, the bFlag is set to true, and the user is shown your error message and then given the chance to go through the whole process again.

    Hopefully that will help to fix your problems - i've not tried putting that all together and compiling but I think that should work
    Last edited by Spud1; 16-03-2008 at 01:06 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help with a simple online database
    By 5cupa in forum Software
    Replies: 6
    Last Post: 24-02-2006, 05:30 PM
  2. Simple way to fix a broken HDD pin?
    By arthurleung in forum Help! Quick Relief From Tech Headaches
    Replies: 4
    Last Post: 11-02-2006, 06:51 PM
  3. Simple, reliable, wireless ADSL router ?
    By Marcos in forum Networking and Broadband
    Replies: 17
    Last Post: 14-08-2005, 09:22 PM
  4. Replies: 3
    Last Post: 13-05-2005, 01:01 PM
  5. Simple Question...
    By ajbrun in forum PC Hardware and Components
    Replies: 1
    Last Post: 07-06-2004, 10:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •