Thread: someone help

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    someone help

    Anyone want to tell me why this portion of my code wont work. After it asks for the string, it doenst let you enter it goes straight into "you entered"

    Here is all of my code. Ive commented the problem.

    Code:
    #ifndef GUARD_Character_Info
    #define GUARD_Character_Info
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Character_Info
    {   
        private:
        char player_name[20];  //players name
        
        public:
        void name() //get players name 
        {    
            cout <<"Enter your players name."<<endl; //problem lies in this section
            cin.getline(player_name, 20);
            cout<<"You have entered"<<player_name<<endl;
        }
        void infOut()
        {
            ofstream charout;
                
            charout.open("CHARACFILE.log", ios::app | ios::binary);
            charout.write( (char*)this, sizeof(*this));
        }
    };
    #endif

    or maybe theres a problem here with the function call.
    Code:
    #ifndef GUARD_Intro
    #define GUARD_Intro
    #include <iostream>
    #include "Character_Info.h"
    
    
    
    using namespace std;
    
    class Intro: public Character_Info
    {   
        private:
        int ch; //users choice on switch
        public:
        void Display_Intro() // display and get name
        {
    
            cout <<" You have just entered the world of Lost Dungeons"<<endl;
            cout <<" You will be taken on a long journey of trouble and danger."<<endl;
            cout <<" If you are up to the task then select your choice (1 or 2):"<<endl;
            
            cin >> ch;
            
            switch (ch)
            {
                    case 1:
                    Character_Info::name(); //this is where i call the function with the problem.
                    infOut();
                    break;
                    case 2:
                    cout <<"Quitting program";
                    break;
                    default:
                    cout <<"ERROR";
                    
            }
        }
    };
    #endif
    ANY HELP IS APPRECIATED
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #2
    is that c++ ? #endif? wheres main() ? im comusededed

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Im sorry i thought main wasnt needed here ya go.

    Code:
    #include <iostream>
    #include <conio.h>
    #include "Intro.h"
    #include "Character_Info.h"
    using namespace std;
    
    int main()
    {
        Intro I1;
        I1.Display_Intro();
        getch();
        return 0;
    }
    NOTE: #endif is c++ i believe
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Just ignore Cgawd, he doesn't know that you can have more than one source file
    he showed all but....whatever...blah

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Hey thanks for the help but its not working. maybe i entered it wrong in my code. heres how i did it.

    Code:
    #ifndef GUARD_Intro
    #define GUARD_Intro
    #include <iostream>
    #include "Character_Info.h"
    
    
    
    using namespace std;
    
    class Intro: public Character_Info
    {   
        private:
        int ch = 0; // here
        public:
        void Display_Intro() // display and get name
        {
    
            cout <<" You have just entered the world of Lost Dungeons"<<endl;
            cout <<" You will be taken on a long journey of trouble and danger."<<endl;
            cout <<" If you are up to the task then select your choice (1 or 2):"<<endl;
            cin >> ch; // here
    
            if( !cin.good( ) ) {// here
            cout << "Error?!" << endl;// here
            cout << ch << endl;// here
            }
            
            switch (ch)
            {
                    case 1:
                    Character_Info::name();
                    infOut();
                    break;
                    case 2:
                    cout <<"Quitting program";
                    break;
                    default:
                    cout <<"ERROR";
                    
            }
        }
    };
    #endif
    PLEASE HELP!!!
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    !cin.good( )
    is that correct?

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Cgawd
    is that correct?
    Yes. I thought you were a professional programmer?
    Although I think while (!(cin >> ch)) would be a better choice.

    > int ch = 0; // here
    That needs to be moved inside your function Display_Intro()... for one you can't initialize class members like that (read: constructor) and another... there's no reason for ch to be a class member.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    thx so much guys i got it working now. i feel stupid because i had tried something close to that. heres what i put and it worked in case you want to know
    Code:
    #ifndef GUARD_Intro
    #define GUARD_Intro
    #include <iostream>
    #include "Character_Info.h"
    
    
    
    using namespace std;
    
    class Intro: public Character_Info
    {   
        private:
        public:
        void Display_Intro() 
        {
            int ch = 0;//added
            cout <<" You have just entered the world of Lost Dungeons"<<endl;
            cout <<" You will be taken on a long journey of trouble and danger."<<endl;
            cout <<" If you are up to the task then select your choice (1 or 2):"<<endl;
            cin >> ch; // here
            cin.ignore( 10, '\n' ); //added
            if( !cin.good( ) ) {// added
            cout << "Error?!" << endl;// added
            cout << ch << endl;// added
            }
            
            switch (ch)
            {
                    case 1:
                    Character_Info::name();
                    infOut();
                    break;
                    case 2:
                    cout <<"Quitting program";
                    break;
                    default:
                    cout <<"ERROR";
                    
            }
        }
    };
    #endif
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  9. #9
    Although I think while (!(cin >> ch)) would be a better choice.
    thats what i meant...

Popular pages Recent additions subscribe to a feed