Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Classes problem...

Think you can spy out that little nasty bug? Try debugging some code in here

Classes problem...

Postby DevGeek++ on Wed Aug 05, 2009 6:56 am

Guys please help me,it run but doesnt do what i want,i want to show the type menu,i am class beginner :

Code: Select all
//======================================================================\\
// Author       : DevGeek++                                             \\
// Date         : 05.08.2009                                            \\
// Description  : Text based game in C++ (Combat Simulator).            \\
//======================================================================\\

#include <iostream>

using namespace std;

class character
{
private:
    char name[10];
    int health;
    int strength;
    int armor;
    int experience;
    int level;
public:
    void newCharacter();
};

character general;
character major;
character captain;
character lieutenant;
character officerCadet;

int main()
{
    int choice;

do {
    cout <<"==========================" << endl;
    cout <<"|        MAIN MENU       |" << endl;
    cout <<"==========================" << endl;
    cout <<"| 1 - Character          |" << endl;
    cout <<"| 2 - Inventory          |" << endl;
    cout <<"| 3 - Shop               |" << endl;
    cout <<"| 4 - Stats              |" << endl;
    cout <<"| 5 - Combat Zone        |" << endl;
    cout <<"| 6 - Quit               |" << endl;
    cout <<"=========================" << endl;
    cout << "Choice: ";
    cin >> choice;
}
while(choice < 1 || choice > 7);
    switch (choice)
    {
        case 1:
        void newCharacter();
        break;
            case 2:
            cout <<"asdfasdf" << endl;
            break;
                case 3:
                cout <<"asdfasdf" << endl;
                break;
                    case 4:
                    cout <<"asdfasdf" << endl;
                    break;
                        case 5:
                        cout <<"asdfasdf" << endl;
                        break;
                            case 6:
                            return 0;
                            break;
    }

    return 0;
}

void character::newCharacter()
{

    cout <<"==========================" << endl;
    cout <<"|          TYPE          |" << endl;
    cout <<"==========================" << endl;
    cout <<"| 1 - General            |" << endl;
    cout <<"| 2 - Major              |" << endl;
    cout <<"| 3 - Captain            |" << endl;
    cout <<"| 4 - Lieutenant         |" << endl;
    cout <<"| 5 - Officer Cadet      |" << endl;
    cout <<"| 6 - Back               |" << endl;
    cout <<"=========================" << endl;

}

DevGeek++
 
Posts: 28
Joined: Tue Aug 04, 2009 5:54 am

Re: Classes problem...

Postby learning in progress on Wed Aug 05, 2009 10:04 am

u mean this :roll:
Code: Select all
//======================================================================\\
// Author       : DevGeek++                                             \\
// Date         : 05.08.2009                                            \\
// Description  : Text based game in C++ (Combat Simulator).            \\
//======================================================================\\

#include "stdafx.h"
#include <iostream>

using namespace std;

class character
{
private:
    char name[10];
    int health;
    int strength;
    int armor;
    int experience;
    int level;
public:
    void static newCharacter();
};

character general;
character major;
character captain;
character lieutenant;
character officerCadet;

int main()
{
    int choice;

do {
    cout <<"==========================" << endl;
    cout <<"|        MAIN MENU       |" << endl;
    cout <<"==========================" << endl;
    cout <<"| 1 - Character          |" << endl;
    cout <<"| 2 - Inventory          |" << endl;
    cout <<"| 3 - Shop               |" << endl;
    cout <<"| 4 - Stats              |" << endl;
    cout <<"| 5 - Combat Zone        |" << endl;
    cout <<"| 6 - Quit               |" << endl;
    cout <<"=========================" << endl;
    cout << "Choice: ";
    cin >> choice;
}
while(choice < 1 || choice > 7);
    switch (choice)
    {
        case 1:
         character::newCharacter();
        break;
            case 2:
            cout <<"asdfasdf" << endl;
            break;
                case 3:
                cout <<"asdfasdf" << endl;
                break;
                    case 4:
                    cout <<"asdfasdf" << endl;
                    break;
                        case 5:
                        cout <<"asdfasdf" << endl;
                        break;
                            case 6:
                            return 0;
                            break;
    }
   char f;
   cin>>f;

    return 0;
}

  void character::newCharacter()
{

    cout <<"==========================" << endl;
    cout <<"|          TYPE          |" << endl;
    cout <<"==========================" << endl;
    cout <<"| 1 - General            |" << endl;
    cout <<"| 2 - Major              |" << endl;
    cout <<"| 3 - Captain            |" << endl;
    cout <<"| 4 - Lieutenant         |" << endl;
    cout <<"| 5 - Officer Cadet      |" << endl;
    cout <<"| 6 - Back               |" << endl;
    cout <<"=========================" << endl;

}

learning in progress
 
Posts: 11
Joined: Thu Jul 09, 2009 4:15 pm

Re: Classes problem...

Postby DevGeek++ on Wed Aug 05, 2009 11:56 am

Yup,it worked,as far as i see,the problem was because in class character i used void newCharacter(); insted of void static newCharacter();,can you tell me why that STATIC keyword make the problem,why do i need to put it ? Where can i learn more about those keywords ? :) Thanks again :D
DevGeek++
 
Posts: 28
Joined: Tue Aug 04, 2009 5:54 am

Re: Classes problem...

Postby learning in progress on Wed Aug 05, 2009 12:11 pm

DevGeek++ wrote:Yup,it worked,as far as i see,the problem was because in class character i used void newCharacter(); insted of void static newCharacter();,can you tell me why that STATIC keyword make the problem,why do i need to put it ? Where can i learn more about those keywords ? :) Thanks again :D

no problem
http://www.learncpp.com/cpp-tutorial/43 ... c-keyword/

static keyword retains its value after the scope has been existed :D
learning in progress
 
Posts: 11
Joined: Thu Jul 09, 2009 4:15 pm

Re: Classes problem...

Postby Knight313 on Sun Aug 09, 2009 12:50 pm

Interesting, I was having trouble calling my constructor class within an if statement. It limited the scope I could call the functions. So if I just include the static keyword, it will give it file scope in main.cpp? So I would not have to worry weather I called it in a loop, or an if, i can still use its class methods right after I have constructed an instance?
Knight313
 
Posts: 64
Joined: Mon Aug 03, 2009 7:20 pm

Re: Classes problem...

Postby C++ on Sun Aug 09, 2009 2:27 pm

Static has three meanings in C++

1) Retain the value of a variable within the scope it was declared.
The actual value is stored in a special memory section on the heap.
The value on the heap gets the value of the definition of the declared variable. But only once.
Any operations on the variable will be reflected on to the reserved space on the heap.
That's why a static variable never seems to go out of scope.

2) Make the variable only visible within the file scope (code unit)
It's sort of the same as anonymous namespaces in C++

3) Declare a method as static within a class.
Normally non static methods of a class are called by passing the pointer this of an instance to the method.
Static methods are called by appending the class name and ::
This is because static methods don't get a this pointer passed to them.
Therefore a static method can only access static data.

If you only want to have the user select a rank, then the method can be static.
However if in the method you want to assign attributes according to the rank, then the method has to be non static.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests