Hi Guys/ girls.

I have written a small program for uni, and it involves the user inputting a message that is saved into a string. The only problem is that whenever i use a space within the message the whole program crashes.

Can anyone help, i have given the code below.

Many Thanks

#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;

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 << "Messages Recieved were: "<<endl;
cout << endl;

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

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

return 0;
}

Hi Guys/ girls.

I have written a small program for uni, and it involves the user inputting a message that is saved into a string. The only problem is that whenever i use a space within the message the whole program crashes.

Can anyone help, i have given the code below.

Many Thanks


#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;

for (int i = 0; i < size; i++)
{
cout << "Message " << i+1 << ": ";
cin >> text;

array.message = text;
cout << endl;

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

array.hours = hrs;
array.minutes = mins;
array.seconds = secs;


}


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

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

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

return 0;
}

See my other post for an explanation.
http://www.daniweb.com/forums/thread112419.html
In the example from this link:
http://www.cplusplus.com/reference/string/getline.html

// getline with strings
#include <iostream>
#include <string>
using namespace std;

int main () {
  string str;
  cout << "Please enter full name: ";
  getline (cin,str);   // this is the relevant line
  cout << "Thank you, " << str << ".\n";
}

line 9 above corresponds to line 2 in your code below:

cout << "Message " << i+1 << ": ";
cin >> text;  // corresponds to line 9 in example above

array[i].message = text;
cout << endl;

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

Try changing line 2 in your code to:

getline (cin, text);

One more thing. It's not relevant in the example from cplusplus.com since there was only one entry. With your code, there is more than one entry and it needs the getline command, so the input needs to flushed. Narue has a good writeup of how to flush the input stream pinned to the top of the forum.


http://www.daniweb.com/forums/thread90228.html

Also check out this thread, particularly post 3.
http://www.daniweb.com/forums/post540758.html#post540758

int main()
{
  // ...
  cout << "How many messages do you want to enter? ";
  cin >> size;
  array = new messages[size];

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

    cout << "Enter current time in Hours, Minutes & Seconds : " ;
    cin >> ws >> hrs >> mins >> secs;
    array[i].hours = hrs;
    // ...
   }
   // ...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.