Thread: Help would be appreciated

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    31

    Help would be appreciated

    I am very new to C++. I have been reading some tutorial mainly on the cprogramming tutorial page. I wanted to but the very basic things I have learned to use and see if I could create just a little program. I ran into a problem and I looked for the answer but didn't get the answer I need. Here is my code and if you could tell me what I was doing wrong I would be very grateful. I am thinking it may be very off the wall. I just started learning C++ yesterday and I wrote all this without referencing back to anything. When I was done I tried to run it but it didn't work, so I went back and tried to find out what I had wrong but like I said it didn't work. So anways here it is.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
      int enteredname;
      int sexofchar;
    
      cout<<"Welcome to Lords of the Land.  This is a text-based Role-Playing game that will immerse you into a world with countless people to meet and activities to do.  You will be able to create your character, hunt with your character, build your character anyway you fancy, join guilds and form alliances, and any other thing your imagination can conjure.  Prepare to enter a world of pure imagination!!";
      cout<<"First off I need your to pick your Characters name.  So if you would please enter your characters name.";
      cin>> enteredname;
      cin.ignore();
      cin.get();
      cout<<"From now okay I shall call you "<<enteredname<<"\n";
      cout<<"Next I need to know the gender of your character.  Please type in either M or F.  Letters are case sensitive.";
      cin>> sexofchar;
      cin.ignore();
      cin.get();
    
      if ( sexofchar = M )
      {
         cout<<"Sir: "<<enteredname<<",I'm glad you could come join us on this Journey.";
      }
    
      else if ( sexofchar = F )
      {
         cout<<"Madam: "<<enteredname<<",I'm glad you could come join us on this Journey.";
      }
    
      cin.get();
      return 0;
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    its cause you are using a int where you should be using a char variable.
    It should be char sexofchar.
    Also when you are comparing variables for equality you use "==" not "=". "=" assigns values to variables
    Woop?

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    okay that's what it is huh? Oh the == I had it there but I tried the = just to see if that was the problem but it wasn't so I guess I had forgot to change it back. So near the top where I have int sexofchar, I should change to char sexofchar? Thanks for the help.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    I changed it to look like.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
      int enteredname;
      char sexofchar;
    
      cout<<"Welcome to Lords of the Land.  This is a text-based Role-Playing game that will immerse you into a world with countless people to meet and activities to do.  You will be able to create your character, hunt with your character, build your character anyway you fancy, join guilds and form alliances, and any other thing your imagination can conjure.  Prepare to enter a world of pure imagination!!";
      cout<<"First off I need your to pick your Characters name.  So if you would please enter your characters name.";
      cin>> enteredname;
      cin.ignore();
      cin.get();
      cout<<"From now okay I shall call you "<<enteredname<<"\n";
      cout<<"Next I need to know the gender of your character.  Please type in either M or F.  Letters are case sensitive.";
      cin>> sexofchar;
      cin.ignore();
      cin.get();
    
      if ( sexofchar == M )
      {
         cout<<"Sir: "<<enteredname<<",I'm glad you could come join us on this Journey.";
      }
    
      else if ( sexofchar == F )
      {
         cout<<"Madam: "<<enteredname<<",I'm glad you could come join us on this Journey.";
      }
    
      cin.get();
      return 0;
    }
    But it still didn't run properly. I got 4 error messages say.

    Line 22 'M' undeclared (first use this function)
    Line 22 (Each undeclared identifier is reported only once
    Line 22 for each fuction it appears in.)
    Line 27 'F' undeclared (first use this function)

  5. #5
    Banned
    Join Date
    Oct 2004
    Posts
    250
    you have to declare M & F like
    Code:
    char m[50];
    Code:
    char f [50];

  6. #6
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by Emotions
    I changed it to look like.

    Code:
      int enteredname;
    the name should probably be a string unless you want your characters to be called by numbers

    Code:
    std::string enteredname;
    (this should work with the rest of your code but I'm not 100% sure, also you probably will have to include the string header)

    [QOUTE]
    Line 22 'M' undeclared (first use this function)
    Line 22 (Each undeclared identifier is reported only once
    Line 22 for each fuction it appears in.)
    Line 27 'F' undeclared (first use this function)[/QUOTE]

    A character in c/c++ is always between single quotes so your if should be:

    Code:
    if ( sexofchar == 'M' )
    a single letter on its own (in fact any non reserved keyword not surronded by quotes) is assumed to be an identifier of some kind nad therefore generates compiler errors

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
      int enteredname;
      char sexofchar;
    
      cout<<"Welcome to Lords of the Land.  This is a text-based Role-Playing game that will immerse you into a world with countless people to meet and activities to do.  You will be able to create your character, hunt with your character, build your character anyway you fancy, join guilds and form alliances, and any other thing your imagination can conjure.  Prepare to enter a world of pure imagination!!";
      cout<<"First off I need your to pick your Characters name.  So if you would please enter your characters name.";
      cin>> enteredname;
      cin.ignore();
      cin.get();
      cout<<"From now okay I shall call you "<<enteredname<<"\n";
      cout<<"Next I need to know the gender of your character.  Please type in either M or F.  Letters are case sensitive.";
      cin>> sexofchar;
      cin.ignore();
      cin.get();
    
      if (sexofchar == 'M')
      {
         cout<<"Sir: "<<enteredname<<",I'm glad you could come join us on this Journey.";
      }
    
      else if (sexofchar == 'F')
      {
         cout<<"Madam: "<<enteredname<<",I'm glad you could come join us on this Journey.";
      }
    
      cin.get();
      return 0;
    }
    That runs I believe. But after I input the name it just closes out and I don't get to see anymore. What do I do here to get it where it will stay up until I hit the exit button?
    Last edited by Emotions; 12-06-2004 at 08:39 PM.

  8. #8
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Here's what I did to the original code:

    First add #include <string> as I did in the code below. Then make enteredname of type 'string' and sexofchar type 'char'. Also in the if statements, you need to add single quotes around the 'M' and the 'F'. You don't need an array, you just need a char variable to store the input in. All of the fixes are bolded.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    
    {
      string enteredname;
      char sexofchar;
    
      cout<<"Welcome to Lords of the Land.  This is a text-based Role-Playing game that will immerse you into a world with countless people to meet and activities to do.  You will be able to create your character, hunt with your character, build your character anyway you fancy, join guilds and form alliances, and any other thing your imagination can conjure.  Prepare to enter a world of pure imagination!!";
      cout<<"First off I need your to pick your Characters name.  So if you would please enter your characters name.";
      cin>> enteredname;
      cin.ignore();
      cin.get();
      cout<<"From now okay I shall call you "<<enteredname<<"\n";
      cout<<"Next I need to know the gender of your character.  Please type in either M or F.  Letters are case sensitive.";
      cin>> sexofchar;
      cin.ignore();
      cin.get();
    
      if ( sexofchar == 'M' )
      {
         cout<<"Sir: "<<enteredname<<",I'm glad you could come join us on this Journey.";
      }
    
      else if ( sexofchar == 'F' )
      {
         cout<<"Madam: "<<enteredname<<",I'm glad you could come join us on this Journey.";
      }
    
      cin.get();
      return 0;
    }
    Last edited by homeyg; 12-06-2004 at 08:41 PM.

  9. #9
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Hi Emotions. Let's take things one step at a time here. For one thing, if you expect a user to enter his sex as either male or female (M or F), you need to add single quotes around M and F like this: 'M' 'F' as in:
    Code:
    if ( sexofchar == 'M' )
      {
         cout<<"Sir: "<<enteredname<<",I'm ...";
      }
    
      else if ( sexofchar == 'F' )
      {
         cout<<"Madam: "<<enteredname<<",I'm ...";
      }
    I personally don't think you need:
    Code:
        char M[50];
        char F[50];
    ...for this.

    Best of luck,

    Steve

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    Thank you for the help but for some reason I don't know of it is still closing after I enter the name of the character. It outputs the introduction and says Please enter your characters name. I enter in a name and then it closes. I just don't know what to do. Any help would be appreciated.

  11. #11
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Hi Emotions. Do the following:
    Add this to your list of includes:
    Code:
        #include<string>
    Then change the type of "username" to string:
    Code:
    string enteredname;
    ...see what that does.

    The next step is to comment out:
    Code:
    cin>> enteredname;
    // cin.ignore();
    // cin.get()
    ...you don't need these here.

    Best,

    Steve
    Last edited by smitsky; 12-06-2004 at 09:01 PM. Reason: Further instructions given

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    Thanks for all the help. It is working thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  2. Program Control, HELP appreciated.
    By Astra in forum Windows Programming
    Replies: 7
    Last Post: 01-01-2007, 06:59 AM
  3. Simple question. Help will be appreciated.
    By wickedclownz in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2003, 02:18 AM
  4. C++ Programs -- Any input appreciated
    By Kyoto Oshiro in forum Game Programming
    Replies: 0
    Last Post: 02-27-2002, 11:22 PM
  5. I need help..anything will be appreciated
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2001, 10:55 AM