strings.. help please

This is a discussion on strings.. help please within the C++ Programming forums, part of the General Programming Boards category; At school I am taking a c++ class, and in school we use codewarrior for the mac. At home tho ...

  1. #1
    help_me
    Guest

    Angry strings.. help please

    At school I am taking a c++ class, and in school we use codewarrior for the mac. At home tho I use Dev c++ (much better than codewarrior).
    My problem occurs when I try to input to strings right after each other:

    cout << "\nPlayer 1, please enter your name: ";
    gets(player_1);

    cout << "\nPlayer 2, please enter your name: ";
    gets(player_2);

    cout << endl << player_1 << " you are X\n";
    cout << endl << player_2 << " you are O\n\n";

    In codewarrior this works fine, but in dev c++ the output looks like this:

    Player 1, please enter your name:
    Player 2, please enter your name: joe

    you are X

    joe you are O

    Press any key to continue . . .

    it skips inputting the first players name, any body know why? I really appreciate any help... got to finish this by monday

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Are you allowed to use the STRING type?

    Code:
    #include <string>
    
    string abc="hello!";
    
    cin >> abc;

  3. #3
    help_me
    Guest
    i'm using the following headers:

    #include <iostream>
    #include <iomanip>
    #include <stdlib.h>
    #include <windows.h>

    I tried adding <string>, but it didnt do a thing

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    If Dev-C++ doesn't do what you want, why is it better than codewarrior?

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Please post your whole entire code so we can see if there is a problem with it.





    YES MY 100TH POST
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: strings.. help please

    Originally posted by help_me
    At school I am taking a c++ class, and in school we use codewarrior for the mac. At home tho I use Dev c++ (much better than codewarrior).
    My problem occurs when I try to input to strings right after each other:

    cout << "\nPlayer 1, please enter your name: ";
    gets(player_1);

    cout << "\nPlayer 2, please enter your name: ";
    gets(player_2);

    cout << endl << player_1 << " you are X\n";
    cout << endl << player_2 << " you are O\n\n";

    In codewarrior this works fine, but in dev c++ the output looks like this:

    Player 1, please enter your name:
    Player 2, please enter your name: joe

    you are X

    joe you are O

    Press any key to continue . . .

    it skips inputting the first players name, any body know why? I really appreciate any help... got to finish this by m
    onday
    If you're studying C++, why not try using some functions from the C++ standard library?.
    Code:
    #include <string>
    
    // ...
    
    std::string player[2];
    cout << "Player 1, enter your name: ";
    std::getline(std::cin, player[0]);
    cout << "\nPlayer 2, enter your name: ";
    std::getline(std::cin, player[1]);

  7. #7
    CIS and business major
    Join Date
    Aug 2002
    Posts
    194

    Re: strings.. help please

    Originally posted by help_me
    At school I am taking a c++ class, and in school we use codewarrior for the mac. At home tho I use Dev c++ (much better than codewarrior).
    My problem occurs when I try to input to strings right after each other:

    cout << "\nPlayer 1, please enter your name: ";
    gets(player_1);

    cout << "\nPlayer 2, please enter your name: ";
    gets(player_2);

    cout << endl << player_1 << " you are X\n";
    cout << endl << player_2 << " you are O\n\n";

    In codewarrior this works fine, but in dev c++ the output looks like this:

    Player 1, please enter your name:
    Player 2, please enter your name: joe

    you are X

    joe you are O

    Press any key to continue . . .

    it skips inputting the first players name, any body know why? I really appreciate any help... got to finish this by monday
    use getline(cin, stringVariable);

    rather than gets

  8. #8
    help_me
    Guest
    thanx i figured it out
    if i use

    cin.getline(player_1, 25, '\n');

    then it works

    thanx for hte help,
    Achilles

    p.s - the reason i didnt post my entire code is because it is part of a 6 page program ; )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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