My string crashes when i have a space- NEED HELP!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Onlynewsopleasebenice
    New Member
    • Mar 2008
    • 1

    My string crashes when i have a space- NEED HELP!

    Hi,

    I am trying to input a message into a string within my program.... however each time i input a space the program crashes.

    Here is the code, please help me.

    #include <iostream>
    #include <string>
    using namespace std;

    struct messages
    {
    string message;
    int hours;
    int minutes;
    int seconds;

    };

    int main()
    {
    int size;
    string text;
    int hrs;
    int mins;
    int secs;


    struct messages* array;



    cout << "How many messages do you want to enter? ";
    cin >> size;
    cout << endl;

    array = new messages[size];

    for (int i = 0; i < size; i++)
    {
    cout << "Message " << i+1 << ": ";
    cin >> text;
    cout << endl;
    array[i].message = text;
    cout << endl;

    cout << "Enter current time in Hours, Minutes & Seconds : " ;
    cin >> hrs >> mins >> secs;
    cout << endl << endl;

    array[i].hours = hrs;
    array[i].minutes = mins;
    array[i].seconds = secs;


    }

    cout << "---------------------------" << endl;
    cout << "Messages Recieved were: "<<endl;
    cout << "---------------------------" << endl;
    cout << endl;

    for (int j=0; j < size; j++)
    {
    if( array[j].hours > 24 || array[j].minutes > 60 ||array[j].seconds > 60)
    {
    cout << "-----------------------------------------------------------------------" << endl;
    cout << endl;
    cout << "Message: " << array[j].message << " "<<endl;
    cout << endl;
    cout << " *** The above message was posted at an invalid time *** " << endl;
    cout << endl;
    }
    else

    {
    cout << "-----------------------------------------------------------------------" << endl;
    cout << endl;
    cout << "Message: " << array[j].message << " "<<endl;
    cout << endl;
    cout << " This message was posted at : ";
    cout << array[j].hours << ":" << array[j].minutes << ":"<<array[j].seconds <<endl;
    cout << endl;
    }
    }

    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It's your cin >> text.

    When you enter a space the >> stops. Then you get to cin >> hrs >> mins >> secs and if the character after the space it not an int, you fail on cin >>hrs. That terminates the cin leaving garbage in your variables.

    If your string has multiple words you need to use cin.getline().

    Remember, the >> operator is for formatted input. That is, you know the type of data being entered before it is entered.

    Lastly, you should check your >> operations for success by test the goodbit after the >>. And that means you probably shouldn't have multiple >> operations in one statement.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      weaknessforcats
      Could you elaborate on how to test (typically) for success after >> using the 'goodbit' (this latter unheard of before now).

      Comment

      Working...