Thread: A Bit Of A Problem Here...

  1. #1
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86

    Question A Bit Of A Problem Here...

    I have a function in which the user can type in a number:

    int Input()
    {
    int b;
    cin >> b;
    return b;
    }

    If the user inputs 1, this function is run:

    void RunStats()
    {
    cout << "\nYour HP: ";
    cout << mos.GetHP() << endl;
    cout << "Your Money: ";
    cout << sat.GetMoney() << " GP " << endl;
    cout << "Tired Level: ";
    cout << sat.GetTired() << " Points" << endl;
    int Input(); //This returns to the function where you input a number
    }

    I want it to show your stats, and then return to the input function, where you can type your commands once again. However, the program ends once you return to the input function. It doesn't allow you to input your selection again. How can I fix this problem?
    - Dean

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    'int Input();' is a declaration and should be used only once
    to tell the computer that a function with this name exists,
    if you want to call the function use 'Input();'.

  3. #3
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    That sorta helps, but after I go back to the Input() function and type in something again, the program ends.
    - Dean

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    so why not encase the program in a while loop?


    maybe like while(input != 999)

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    How about the entire code?

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    In your main function, you should be using a loop like so:
    Code:
    while (choice!=2)
    {
       cout<<"1. Run stats"<<endl;
       cout<<"2. Exit"<<endl;
       cin>>choice;
       if (choice==1)
         RunStats();
    }
    The way you have it, it calls input somewhere else (I assume main) runs the RunStats function which calls input again, but after it does that, then RunStats is done and it goes back to where it left off in main - which is probably at the end of the file. Make sense?

  7. #7
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    Normally, it would, but my code is a bit strange. I'm getting a run-time error with that code, and no matter what I type in, it runs your stats. It might take some time for me to locate the source of the problem
    - Dean

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    If you post your code whe can help too

  9. #9
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    HEADER:
    // This class sets Sebba's stats.

    class Sebba
    {
    public:
    int GetHP() ;
    void SetHP(int x);
    private:
    int HP;
    };
    void Sebba::SetHP(int x)
    {
    HP = x;
    }
    int Sebba::GetHP ()
    {
    return HP;
    }

    //This class sets the Stats of your money, and how tired you are.

    class Stats
    {
    public:
    void SetMoney(int y);
    int GetMoney();
    void SetTired(int z);
    int GetTired();
    private:
    int Money;
    int Tired;
    };
    void Stats::SetMoney(int y)
    {
    Money = y;
    }
    int Stats::GetMoney()
    {
    return Money;
    }
    void Stats::SetTired(int z)
    {
    Tired = z;
    }
    int Stats::GetTired()
    {
    return Tired;
    }
    Sebba mos;
    Stats sat;


    AND THE C++ SOURCE CODE:

    #include <iostream.h>
    #include "header.h"
    int GAExp();
    void RunStats()
    {
    cout << "\nYour HP: ";
    cout << mos.GetHP() << endl;
    cout << "Your Money: ";
    cout << sat.GetMoney() << " GP " << endl;
    cout << "Tired Level: ";
    cout << sat.GetTired() << " Points" << endl;
    GAExp();
    }

    //Run Exp is the function that runs the 'Exploration' Function

    void RunExp()
    {
    cout << "\nWow. What a beautiful place!";
    //This is another exploration function:
    GAExp();
    }

    //Main Code:

    int main()
    {
    cout << "\n\nSetting Statistics...\n";
    //These are Hit Point, Money, and Tired Values:
    int x = 5;
    int y = 100;
    int z = 10;
    //These set Hit Point, Money, and Tired Values:
    sat.SetMoney(y);
    mos.SetHP(x);
    sat.SetTired(z);

    //Information:

    cout << "\nYour HP has been set to " << mos.GetHP();
    cout << "\nYour money has been set to " << sat.GetMoney() << " GP";
    cout << "\nYour tired level has been set to " << sat.GetTired();
    cout << "\n\nDone Loading Stats";

    //These is your choices. Enjoy

    cout << "\nWhat do you want to do?\n1.)ViewStats\n2.)Explore\n3.)Use Item\n";

    //This is your imput command, and tells the program where to go:

    int a = GAExp();
    while (a == 1)
    {
    cout<<"1. Run stats"<<endl;
    cout<<"2. Exit"<<endl;
    RunStats();
    }

    //If you type in 1, You'll open up your menu.

    return 0;
    }

    //This is currently a sample function:

    int GAExp()
    {
    int b;
    cin >> b;
    return b;
    }
    - Dean

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    Sebba.h
    
    class Sebba
    {
     public:
        Sebba() : HP(0) {}
        Sebba(int x) : HP(x) {}
        int GetHP() const {return HP;}
        void SetHP(int x) { HP = x;}
     private:
        int HP;
    };
    Code:
    Stats.h
    
    class Stats
    {
     public:
        Stats() : Money(0),Tired(0) {}
        Stats(int money,int tired) : Money(money),Tired(tired) {}
        void SetMoney(int y) { Money = y;}
        int GetMoney()const { return Money;}
        void SetTired(int z) { Tired=z;}
        int GetTired()const { return Tired;}
     private:
        int Money;
        int Tired;
    };
    Code:
    main.cpp
    
    #include<iostream>
    #include "Stats.h"
    #include "Sebba.h"
    using namespace std;
    
    int GAExp()
    {
        int b;
        cin >> b;
        return b;
    }
    
    void RunStats()
    {
        cout << "\nYour HP: ";
        cout << mos.GetHP() << endl;
        cout << "Your Money: ";
        cout << sat.GetMoney() << " GP " << endl;
        cout << "Tired Level: ";
        cout << sat.GetTired() << " Points" << endl;
        GAExp();
    }
    
    
    
    int main()
    {
        cout << "\n\nSetting Statistics...\n";
        Sebba mos(5);
        Stats sat(100,10);
        cout << "\nYour HP has been set to " << mos.GetHP();
        cout << "\nYour money has been set to " << sat.GetMoney() << " GP";
        cout << "\nYour tired level has been set to " << sat.GetTired();
        cout << "\n\nDone Loading Stats";
        bool exit = false;
        while (1) //infinite loop
        {
            cout << "\nWhat do you want to do?\n1.)ViewStats\n2.)Explore\n3.)Use Item\n4)Exit\n";
            int input = GAExp();
            switch (input)
            {
                case 1:   RunStats();
                               break;
                case 2:   // do whatever to explore
                               break;
                case 3:   // do whatever to use item
                               break;
                case 4:   exit = true;
                               break;
                default : cout << "Not valid option"<<endl;
            }
             if (exit) break;
        }
    return 0;
    }
    Heres one way of doing what you wanted. Code cleaned up a little too.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by LordVirusXXP
    //These are Hit Point, Money, and Tired Values:
    int x = 5;
    int y = 100;
    int z = 10;
    Then why don't you call them hp, money, and tired? You want to get into the habit of using descriptive identifiers. x, y, and z generally aren't too descriptive.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Replies: 7
    Last Post: 12-10-2004, 08:18 AM
  4. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM