Thread: Printing text with functions in this practice problem

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    6

    Printing text with functions in this practice problem

    Hi guys, i was wondering about something here in this practice problem that i semi-completed i believe. it's from the book Jumping into c++ chapter 6 functions, question 1.

    in the code below, i was wondering if there is a way to make a function that prints something. i wanted to make a function for
    Code:
     cout << " PAUSE MENU " << endl;    cout << " 0. Resume Game " << endl;
        cout << " 1. Commands " << endl;
        cout << " 2. Controller Setup " << endl;
        cout << " 3. Bottles of Beer " << endl;
        cout << " 4. Quit " << endl;
    
    
        cout << " What is your Choice - ";
        cin >> userinput;
    so i wouldn't have to retype it again in the loop, i would just call the function. but it seems whatever i try it doesn't display the text. i've tried making a function with no return time like this " Void Pausemenu(); " but that just goes blank.

    thanks for any help or advice thanks.


    Code:
    #include <iostream>
    
    using namespace std;
    
    
    //This function displays different results based on the users input
    int menu(int menuchoice);
    
    
    int main()
    {
        //Main variable that accepts the userinput
        int userinput;
    
    
        cout << " PAUSE MENU " << endl;
        cout << " 0. Resume Game " << endl;
        cout << " 1. Commands " << endl;
        cout << " 2. Controller Setup " << endl;
        cout << " 3. Bottles of Beer " << endl;
        cout << " 4. Quit " << endl;
    
    
        cout << " What is your Choice - ";
        cin >> userinput;
    
    
        menu(userinput);
    
    
        /*This checks whether the user entered a number
        Greater than 4 or less than 0 and loops */
        while( userinput < 0 || userinput > 4 )
            {
    
    
                cout << " \nPAUSE MENU " << endl;
                cout << " 0. Resume Game " << endl;
                cout << " 1. Commands " << endl;
                cout << " 2. Controller Setup " << endl;
                cout << " 3. Bottles of Beer " << endl;
                cout << " 4. Quit " << endl;
    
    
                cout << " What is your Choice - ";
                cin >> userinput;
                menu(userinput);
    
    
             }
    }
    
    
    int menu(int menuchoice)
    {
        if ( menuchoice == 0 )
            {
                cout << " Game resumes and you win ";
            }
        else if ( menuchoice == 1 )
            {
                cout << " x = shoot, y = jump ";
            }
        else if ( menuchoice == 2 )
            {
                cout << " Don't know what to put here ";
            }
        else if ( menuchoice == 3 )
        {
            int beercount = 99;
            while ( beercount > 0 )
                {
                    cout << "\n" << beercount << " bottles of beer on the wall,\n" << beercount << " bottles of beer. " << endl;
                    beercount--;
                    if ( beercount > 0)
                    {
                        cout << "Take one down, pass it around, \n" << beercount << " bottles of beer on the wall... " << endl;
                    }
                    else
                    {
                        cout << "Take one down, pass it around, \n";
                        cout << "\nNo more bottles of beer on the wall, no more bottles of beer " << endl;
                        cout << "Go to the store and buy some more, 99 bottles of beer on the wall... " << endl;
                    }
                }
        }
        else if ( menuchoice == 4 )
            {
                cout << " Game Over!!!! ";
            }
            return menuchoice;
    }
    Thanks in advance.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    i've tried making a function with no return time like this " Void Pausemenu(); " but that just goes blank.
    O_o

    You should post that attempt.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    Oh I'm sorry, At the moment I'm on my phone so the code won't be formatted well.
    The prototype was:

    Code:
    Void pausemenu();
    
    Void pausemenu()
    {
                Cout << " \nPAUSE MENU " <<  endl;
                Cout << " 0. Resume Game " << endl;
                Cout << " 1. Commands " << endl;
                Cout << " 2. Controller Setup " << endl; 
                Cout << " 3. 99 Bottles of beer" << endl;
                Cout << " 4. Quit " << endl;
               
    }
    I believed this would just display this piece of info whenever I called it. But it doesn't sadly.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Presumably, you realise that both Void and Cout should be written in lowercase?

    > Oh I'm sorry, At the moment I'm on my phone so the code won't be formatted well.
    Then don't bother posting until you're back at your PC.

    If you've got code to post, then you NEED to be copy/pasting it directly from your IDE/Editor, not typing in some half remembered crap with naff autocorrect getting in the way.
    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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sorry, but to answer that question, we really need to see the "real" faulty code that you've tried and which doesn't work.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Practice Problem Help!
    By PCBeginner in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2012, 10:24 AM
  2. an practice problem on C and OS
    By oldercoder in forum C Programming
    Replies: 4
    Last Post: 02-07-2012, 10:59 AM
  3. Even or Odd (practice problem)
    By Amphibian in forum C Programming
    Replies: 16
    Last Post: 09-22-2010, 01:32 PM
  4. Best practice: string manipulation functions
    By samadhi in forum C Programming
    Replies: 4
    Last Post: 09-23-2004, 05:16 PM
  5. Problem when printing to text file
    By sissoko in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2002, 06:26 AM