473,394 Members | 1,640 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,394 software developers and data experts.

C++ String With Spaces

cjbrx3115
Hello everyone! I am kind of new to C++ and am curently making a program that needs the user to type a line of text (with spaces) and have the program store that in a "string" variable. I tried using the "cin" method, but my program crashes when I do. I also tried using the getline feature too but for some reason the program always skips over it. Heres my code...

(the real code is too long, so I made a smaller version of the scenario)

-------------------------------------------------------------------------------------------------------------

#include <iostream>
#include <string>

using namespace std;

void main()

{

string sentence;
char s;

cout << "Type 'S' to type a sentence: ";
cin >> s;


if (s == 's')
{
cout << "Type a sentence: ";
getline(cin, sentence);

cout << "You typed: " << sentence;
cout << endl;
}

system("pause");

}



-------------------------------------------------------------------------------------------------------------


The code seems to work without the "if" statement. I have no clue what I'm doing wrong so please help!

Thanks in advance,

-Cody-
Jan 26 '07 #1
2 2329
horace1
1,510 Expert 1GB
consider
Expand|Select|Wrap|Line Numbers
  1. cout << "Type 'S' to type a sentence: ";
  2. cin >> s;
  3.  
you hit a key followed by the newline key, the character is returned into variable s and the newline is left in the input stream.

when you execute
Expand|Select|Wrap|Line Numbers
  1. cout << "Type a sentence: ";
  2. getline(cin, sentence);
  3.  
the getline() reads the newline that is in the input stream and returns immediatly with an empty string

you need to extract and discard the newline character, e.g.
Expand|Select|Wrap|Line Numbers
  1. cout << "Type 'S' to type a sentence: ";
  2. cin >> s;
  3. cin.get(); // read newline
  4.  
Jan 26 '07 #2
Thank you! The code worked!
Jan 26 '07 #3

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

Similar topics

9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
16
by: Steve | last post by:
Hi Guys, I have a string which contains data elements separated by spaces. I also have a function which returns the number of characters from the beginning of the string for a given number of...
18
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
20
by: Chris LaJoie | last post by:
I'm looking for some kind of simple string compression code I can use. I'm not looking for SharpZipLib. Their implimentation spans several classes and is very complex. I'm just looking for...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
11
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
121
by: swengineer001 | last post by:
Just looking for a few eyes on this code other than my own. void TrimCString(char *str) { // Trim whitespace from beginning: size_t i = 0; size_t j; while(isspace(str)) {
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.