Can't get this to work.

This is a discussion on Can't get this to work. within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { char personality[20]; cout <<"Enter personality: " <<endl; cin.getline(personality,20); if (personality[20] == ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Angry Can't get this to work.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char personality[20];
    
    cout <<"Enter personality: " <<endl;
    cin.getline(personality,20);
    if (personality[20] == 'mad')
        {
        cout <<"$$$$ the world!" <<endl;
        }
        else
            {
            cout <<"Lovely" <<endl;
            }
            
    system("pause");
    }
    I know I could get this to work by doing a different way but that is not what I want. I want the user to input something wether it be, mad, sad, happy and then depending on which they TYPE display a different text. I don't want an options menu! Not looking for something like this: press M for mad or S for sad. I want to be able to feed off of the whole word.(If its possible)
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char personality[20];
    
    cout <<"Enter personality: " <<endl;
    cin.getline(personality,20);
    if (personality[20]  == 'mad')
        {
        cout <<"$$$$ the world!" <<endl;
        }
            else
            {
            cout <<"Lovely" <<endl;
            }
            
    system("pause");
    }
    should be:
    Code:
    #include <iostream>
    using namespace std;
    
    //#include <string>  //for alternate if statement below
    
    int main()
    {
        char personality[20];
    
        cout <<"Enter personality: " <<endl;
        cin.getline(personality,20,'\n');
        if (personality == "mad")   //or if(!strcmpi(personality,"mad");
            cout <<"$$$$ the world!" <<endl;
        else
            cout <<"Lovely" <<endl;
            
        system("pause");
        return 0;
    }
    Last edited by major_small; 08-18-2003 at 09:45 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I think you need to use strcmp instead of == in your comparisons.

    Also I noticed you don't explicitly have a return, so you may want to add that though I don't think it's necessary from what I have read on this board.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    forgot about the return... sometimes it is necessary, depending on what compiler your using... and I think the == will work, but strcmpi is better... or strcmp if you want it to be case-sensitive
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    1479
    Join Date
    Aug 2003
    Posts
    253
    Originally posted by major_small
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char personality[20];
    
    cout <<"Enter personality: " <<endl;
    cin.getline(personality,20);
    if (personality[20]  == 'mad')
        {
        cout <<"$$$$ the world!" <<endl;
        }
            else
            {
            cout <<"Lovely" <<endl;
            }
            
    system("pause");
    }
    should be:
    Code:
    #include <iostream>
    using namespace std;
    
    //#include <string>  //for alternate if statement below
    
    int main()
    {
        char personality[20];
    
        cout <<"Enter personality: " <<endl;
        cin.getline(personality,20,'\n');
        if (personality == "mad")   //or if(!strcmpi(personality,"mad");
            cout <<"$$$$ the world!" <<endl;
        else
            cout <<"Lovely" <<endl;
            
        system("pause");
        return 0;
    }
    The above didn't work.
    Knowledge is power and I want it all

    -0RealityFusion0-

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    char personality[20];
    
    cout <<"Enter personality: " <<endl;
    cin.getline(personality,20,'\n'); //don't think '\n' is necessary
    if(!strcmpi(personality,"mad"))
    {
        cout <<"$$$$ the world!" <<endl;
    }
    else
    {
        cout <<"Lovely" <<endl;
    }
    
    cout<<endl<<"You are "<<personality<<", in case you forgot."<<endl;
    
    return 0;
    }
    Read major_small's comments.
    Last edited by funkydude9; 08-18-2003 at 09:54 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  7. #7
    1479
    Join Date
    Aug 2003
    Posts
    253
    I was about to say something about that look at my code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char personality[20];
    char m;
    cout <<"Enter" <<endl;
    cin.getline(personality,20,'\n');
    if (strcmp("mad",personality))
        {
         cout <<"Ruck off!" <<endl;
        }
        else
            {
            cout <<"Lovely" <<endl;
            }
            
    system("pause");
    }
    I forgot the ! sign and was about to ask why it only would type Ruck off! for anything i typed besides mad. LOL.
    Thanks funkydude!
    Knowledge is power and I want it all

    -0RealityFusion0-

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    I forgot the ! sign
    Copy & past is your friend.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by major_small
    forgot about the return... sometimes it is necessary, depending on what compiler your using... and I think the == will work, but strcmpi is better... or strcmp if you want it to be case-sensitive
    Nope, the == operator only works on strings of the stl string datatype.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    thanks for clearing that up for me golfinguy4
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    When you use == as you did, it compares the pointers. For example, 'personality == "mad" ' compares the base address of 'personality' with the base address of the string literal "mad".
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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