C++ newbie problem!

This is a discussion on C++ newbie problem! within the C++ Programming forums, part of the General Programming Boards category; Hello guys! First I'd like to say that I started studying C++ just a few days ago with the HUGE ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    C++ newbie problem!

    Hello guys!

    First I'd like to say that I started studying C++ just a few days ago with the HUGE help of Alex Allain's guide.

    Now, I've learned some basics (I'm currently at lesson 16 of Alex's guide) and I started to write a small text game of my own. I got through the beginning well, but then a problem arose.

    I'll link the code first and point out the part I have problem with.
    So here's the code (scroll below the code to see my problem):

    Code:
    #include <iostream>#include <cstring>
    using namespace std;
    int input;
    char string[256];
    void line()
    {
        cout<<"-------------------";
    }
    void game1()
    {
        cout<<"Welcome to the world of whatever (this is just and example for the forums)"<<string<<"!"<<endl;
    }
    void game0()
    {
        char string[256];
        cin.getline(string, 256, '\n');
        cout<<"Please enter your name: ";
        cin.getline(string, 256, '\n');
        cout<<endl;
        cout<<"Confirm name: "<<string<<"? (yes/no): ";
        cin.getline(string, 256, '\n');
        cout<<endl;
        if (strcmp(string, "yes")==0)
        {
            game1();
        }
        else if(strcmp(string, "no")==0)
        {
        cout<<"Please re-enter your name correctly: ";
        cin.getline(string, 256, '\n');
        game1();
        }
    
    
    
    
    }
    void instructions()
    {
        cout<<endl;
        cout<<"Instructions:"<<endl;
        cout<<"-------------"<<endl;
        cout<<endl;
        cout<<"Always press ENTER to progress."<<endl;
        cout<<endl;
        cout<<"If you are prompted to answer either yes or no"<<endl;
        cout<<"make sure your grammar is correct. Only use lower-"<<endl;
        cout<<"case letters in these cases or the game will crash."<<endl;
        cout<<endl;
        cout<<"Everything else is pretty self-explanatory."<<endl;
        cout<<endl;
        cout<<"Press ENTER to move back to Main Menu."<<endl;
        cout<<endl;
        cin.ignore();
        cin.get();
    
    
        cout<<"Test game!"<<endl;
        cout<<endl;
        int input;
        cout<<"1.Start game"<<endl;
        cout<<"2.Exit"<<endl;
        cout<<"3.Instructions"<<endl;
        cout<<endl;
        cout<<"---------"<<endl;
        cout<<"Selection: ";
        cin>>input;
        cout<<"---------"<<endl;
        cout<<endl;
        switch (input)
        {
            case 1:
            game0();
            break;
            case 2:
            cout<<"Exiting program."<<endl;
            cin.ignore();
            cin.get();
            break;
            case 3:
            instructions();
            break;
        }
    
    
    }
    int main()
    {
        int input;
        cout<<"Test game."<<endl;
        cout<<endl;
        cout<<"1.Start game"<<endl;
        cout<<"2.Exit"<<endl;
        cout<<"3.Instructions"<<endl;
        cout<<"---------"<<endl;
        cout<<"Selection: ";
        cin>>input;
        cout<<"---------"<<endl;
        cout<<endl;
        switch (input)
        {
            case 1:
            game0();
            break;
            case 2:
            cout<<"Exiting program."<<endl;
            cin.ignore();
            cin.get();
            break;
            case 3:
            instructions();
            break;
        }
    
    
    }
    First of all, I know I'm using a lot of useless stuff in there but this is my first project so all that matters to me is that it works as intented.

    Anyway the problem is that I want to use the name defined in void game0() later on in the game by using the string's definition but I can't because it's defined in another void.

    So my question is: is there any way I could use the string defined in void game0() in other voids through some coding?

    Thanks in advance. And I'm sorry if this question has already been answered in another thread. I tried looking through the forums, but couldn't find an answer.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your naming is bad. "string" is a particularly bad name, as it potentially conflicts with the string class. Also, function names like game0 are meaningless. What does the function do? Give it a name that reflects that.

    You seem to be using the word "void" as we would normally use the word "function". I.e., name is not defined in another void, but in another function (that happens to ahve a void return type).

    To retrieve the name, you could make it a non-void function that returns the name:
    Code:
    string getName() {
        string name;
        cout << "What's yer name? ";
        getline(cin, name);
        return name;
    }
    
    // in main
        string name = getName();
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Thank you for your effort oogabooga!
    As expected I can't get that code to work due to my lack of knowledge.
    I will continue my studies and come back to this project when I've learned some more.
    Nevertheless I appreciate you answering, and I'm sorry I couldn't put your advice to use.
    -OP

    (Edit: Case closed.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie problem
    By redsfan in forum C Programming
    Replies: 6
    Last Post: 01-26-2010, 02:10 PM
  2. Newbie problem
    By brunovl83 in forum C Programming
    Replies: 9
    Last Post: 10-29-2009, 04:16 PM
  3. Newbie problem
    By -Od|n- in forum C# Programming
    Replies: 6
    Last Post: 07-28-2009, 11:49 AM
  4. newbie problem
    By face_master in forum C++ Programming
    Replies: 7
    Last Post: 05-15-2002, 09:56 AM
  5. newbie problem... help!!
    By ygfperson in forum C Programming
    Replies: 14
    Last Post: 04-23-2002, 06:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21