My First Program

This is a discussion on My First Program within the C++ Programming forums, part of the General Programming Boards category; I've finally been able to write my first half usefull C++ program there are a few thing I would like ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    21

    My First Program

    I've finally been able to write my first half usefull C++ program there are a few thing I would like to add but at the moment I don't know enough to be able to add them .

    I have some problems with the code below, when you choose to add and enter the first number etc. then when the answer is displayed the else statement which I added to the program is executed even though I don't want it to be, any ideas?

    Code:
    #include <iostream>
    
    int main()
    {
        
        int unsigned short DoAddOrMinus;
        int long FirstNumber;
        int long SecondNumber;
        
        std::cout << "What would you like to do?\n";
        
        //This is the code for the menu
        std::cout << "1 - Add\n2 - Subtract\n\n";
        
        std::cout << "Please make your selection: ";
        std::cin >>  DoAddOrMinus;
        
        //This is the if statement which performs addition
        if (DoAddOrMinus == 1)
        {
           std::cout << "\n\nWhat is the first number you want to add: ";
           std::cin >> FirstNumber;
           
           std::cout << "What is the second number you want to add: ";
           std::cin >> SecondNumber;
           std::cin.ignore(80,'\n');
           std::cout << "\n\n";
           
           std::cout << "The answer is: " << FirstNumber + SecondNumber << "\n";
        }   
        
        //This is the code which preforms subtraction
        if (DoAddOrMinus == 2)
        {
           std::cout << "\n\nWhat is the first number you want to subtract: ";
           std::cin >> FirstNumber;
           
           std::cout << "What is the second number you want to subtract: ";
           std::cin >> SecondNumber;
           std::cin.ignore(80,'\n');
           std::cout << "\n\n";
           
           std::cout << "The answer is: " << FirstNumber - SecondNumber << "\n";
        }
        
        //This is the code which is activated when someone does not enter a number from the menu
        else
        {
            std::cout << "You should have entered a number from the menu.\n";
        }
        
        std::cin.get();
        return 0;
        
    }

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Read the tutorial about switches they allow you to make long if statements.
    Here is an example of my code:

    Code:
    #include <iostream>
    
        int multiply(int x,int y)
        {
        return x*y;
        }
        
        int divide(int x, int y)
        {
        return x/y;
        }
        
        int add(int x, int y)
        {
        return x+y;
        }
        
        int subtract(int x, int y)
        {
        return x-y;
        }
    
    using namespace std;
    
    int main()
    {
        char op='c';
        int x,y;
        while (op!='e')
        {
        cout<<"What operation would you like to perform add(+), subtract(-),divide(/),multiply(*), (e)exit?";
        cin>> op;
        switch(op)
        {
        case '+':
        cout<<"Enter the numbers you would like to add."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
        break;
        
        case'-':
        cout<<"Enter the numbers you would like to subtract."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
        break;
        
        case'/':
        cout<<"Enter the numbers you would like to divide."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"/"<<y<<divide(x,y)<<endl;
        break;
        
        case'*':
        cout<<"Enter the numbers you would like to multiply."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"*"<<y<<multiply(x,y)<<endl;
        break;
        case'e':
        return 0;
        default:
        cout<<"sorry, try again."<<endl;
        }
        }
        
        return 0;
    }
    case replaces if which makes everything a lot easier.
    As for the else loop I don't know why it won't work.
    My computer is awesome.

  3. #3
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Your else statement will only operate properly if DoAddOrMinus == 2 because that is the only if statement it is attached to. If DoAddOrMinus == 1 the applicable if statement will execute it's code. But the next if statement only evaluates DoAddOrMinus == 2, and anything else will trigger the else statement.

    Hint...lookup the else if evaluation statement.

    Code:
        if (DoAddOrMinus == 1)
        {
            // some code
        }   
        .
        .
        if (DoAddOrMinus == 2)  // See Hint above
        {
             // some code
        }
        
        //This is the code which is activated when someone does not enter a number from the menu
        else
        {
            // This else is a logic error
        }
    Last edited by Scribbler; 02-06-2005 at 03:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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