Please HELP.I'm new to programming...

This is a discussion on Please HELP.I'm new to programming... within the C++ Programming forums, part of the General Programming Boards category; I have written this program in cpp to perform basic calculations on two variables using functions and switch.The program does ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Unhappy Please HELP.I'm new to programming...

    I have written this program in cpp to perform basic calculations on two variables using functions and switch.The program does compile with zero errors,but the answer it gives is false.Please tell me if its a glitch with the code or the program itself is untrue.
    Code:
    #include<iostream>
    using namespace std;
    int menu(int x, int y,char choice)
    {
        switch(choice){
            case'A':
                cout<<"The sum of "<<x<<" and "<<y<<" is "<<x + y;
                break;
            case'S':
                cout<<"The difference between "<<x<<" and "<<y<<" is "<<x - y;
                break;    
            case'M':
                cout<<"The product of "<<x<<" and "<<y<<" is "<< x * y;
                break;    
            case'D':
                cout<<"The ratio of "<<x<<" and "<<y<<" is "<<x / y;
                break;    
            default:
                cout<<"Invalid Input";
            }
        }
        int main()
        {
            int a,b;
            char choose;
            cout<<"Enter the first number:";
            cin>>a;
            cout<<"Enter the second number:";
            cin>>b;
            cout<<"Enter your choice:";
            cin>>choose;
            cin.ignore();    
            cout<<menu(a,b,choose);
            cin.get();
            return 0;
        }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > int menu(int x, int y,char choice)
    Change this to
    void menu(int x, int y,char choice)

    > cout<<menu(a,b,choose);
    And this to just
    menu(a,b,choose);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I second Salem.
    If you define a function as "int blabla(...)" it is supposed to return an integer. Considering it does not return an int, boolean or any other thing, then it is a void function. (Returns nothing).

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 03:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 10:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread


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