473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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;
}
Mar 6 '08 #1
2 1771
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 6 '08 #2
whodgson
542 512MB
weaknessforcats
Could you elaborate on how to test (typically) for success after >> using the 'goodbit' (this latter unheard of before now).
Mar 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Developwebsites | last post by:
I want to enter sentences with blanks between words, names, cities, new york, pam anderson, etc. as in: Input"enter your name";name$ print"your name is: ";name$ C++ seems to make it rather...
12
by: Paul | last post by:
Hi, Global operator new and delete are overloaded and I am using stl map to store pointers, but this code crashes, can some one shed some light??? Compiler: MS VC++ 6.0 STL: Shipped with...
7
by: Dev | last post by:
Hello, In the following class definition, the ZString destructor is invoked two times. This crashes the code. class ZString { public: ZString(char* p)
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
9
by: shyam | last post by:
Hi All Here is a program which basically tokenizes a string based on space seperation. But it does not run bcoz i am directly trying to manuplate the string. I cannot use standard string...
8
by: gk245 | last post by:
This is part of a bigger program, but i made it simple (basically the OT_hours function is supposed to determine if the number entered is over 40 hours or not and return the appropriate answer): ...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
14
by: nishit.gupta | last post by:
Is their any single fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" , It can also...
0
by: Rik Moed | last post by:
Hi all, I encounter a problem with a library method that should create a multi dimensional string array. Below is the method:...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.