Thread: Jumping into C++ Functions Problem...

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    30

    Jumping into C++ Functions Problem...

    Take the "menu program" you wrote earlier and break it out into a series of calls to functions for each of the menu items. Add the calculator and "100 bottles of beer" as two different functionsthat can be called.

    I tried this and I don't understand why it isn't working. I even replaced
    "int menu()", "int song()", and "int calc()" with their raw script and it worked fine. If someone could help me with this and help me understand a little bit more about functions I would really appreciate it.

    Note that I have tested each of the other individual functions and they all work perfectly.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int song();
    int calc();
    int menu();
    
    
    int main()
    {
    string choice;
        cout << "Choose: 1. Song, 2. Calculator. 3. Menu.\n";
        cin >> choice;
        if(choice=="1")
            {
                int song();
            }
        else if(choice=="2")
            {
                int calc();
            }
        else if(choice=="3")
            {
                int menu();
            }
        else
            {
                cout << "Invalid entry.";
            }
    }
    
    
    int song()
    {
        for(int i=99;i>0;i--)
            {
                cout << i << " bottles of beer on the wall. " << i << " bottles of beer.\n";
                cout << "Take one down and pass it around. " << i-1 << " bottles of beer on the wall.\n";
            }
    }
    
    
    int calc()
    {
    double x;
    double y;
    string input;
        cout << "Enter your first number.\n";
            cin >> x;
        cout << "Enter your arithmetic operator (arithmetic operators are +, -, *, or /).\n";
            cin >> input;
        cout << "Enter your second number.\n";
            cin >> y;
        if(input=="+")
            {
                cout << x << " + " << y << " = " << x+y << ".";
            }
        else if(input=="-")
            {
                cout << x << " - " << y << " = " << x-y << ".";
            }
        else if (input=="*")
            {
                cout << x << " * " << y << " = " << x*y << ".";
            }
        else if (input=="/")
            {
                cout << x << " / " << y << " = " << x/y << ".";
            }
        else
            {
                cout << "Invalid data.\n";
            }
    }
    
    
    int menu()
    {
        string choice;
        while(true)
            {
        cout << "Choose a color: Red, Blue, or Green.\n";
        cin >> choice;
            if(choice=="Red")
                {
                    cout << "You chose red.\n";
                    break;
                }
            else if(choice=="Blue")
                {
                    cout << "You chose blue.\n";
                    break;
                }
            else if(choice=="Green")
                {
                    cout << "You chose green.\n";
                    break;
                }
            else
                {
                    cout << "Invalid input.\n";
                }
            }
        cout << "Thank you for using my program.\n";
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Aenn View Post

    I tried this and I don't understand why it isn't working. I even replaced
    "int menu()", "int song()", and "int calc()" with their raw script and it worked fine.

    Code:
    int song();
    
    /* ... /*
    
    if(choice=="1")
            {
                int song();
            }
    I don't know what you mean by "raw script" in your code. Notice in your code that you declare a function near the top and then later when you want to call it, you are currently using the declaration. To call a function you write, for example

    Code:
    song();
    Also, if something doesn't work, please also say what error or warning messages occured. This is important for learning how to debug code.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>int song();
    This is a function declaration. To call a function, type the function name with a paranthesis and the name of the variables you want to pass in a comma separated list:
    song(arg1, arg2, ...);
    In your case it is song().

    Also be aware that functions that return nothing should have a return type of void. Therefore,
    int song()
    should be
    void song()

    Same for the rest.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jan 2015
    Posts
    30
    Thank you guys so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping Into C++ Chapter 14 Problem 4
    By mbartholomew in forum C++ Programming
    Replies: 3
    Last Post: 12-28-2014, 03:10 PM
  2. Jumping into C++ Ch 13 Problem 3 - Pointers
    By bkushigian in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2013, 12:12 AM
  3. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  4. jumping between functions without a function call
    By WindEdge in forum C Programming
    Replies: 43
    Last Post: 09-12-2009, 12:19 PM
  5. Jumping between functions
    By Nexus-ZERO in forum C Programming
    Replies: 8
    Last Post: 01-14-2008, 03:47 AM

Tags for this Thread