473,804 Members | 3,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 1793
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 Contributor
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
41272
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 difficult to do a simple thing as that. #include<string> using namespace std;
12
2493
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 Visual Studio. #include <malloc.h> #include <map>
7
2715
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
8299
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 code? many thx!
11
5361
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 (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
9
1583
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 functions due to performance issues can anybody figure out whats gone wrong with this
8
1878
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): #include <stdio.h> #include <stdlib.h> struct person { float overtime;
87
5173
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 the library, and other resources related to managed strings are available for download from the CERT web site at: http://www.cert.org/secure-coding/managedstring.html
14
2938
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 contain zero
0
1354
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: -------------------------------------------------------------------------------- public class StringArrayLibrary { public void Create(ref string names)
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10580
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.