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

String with spaces in C++

cjbrx3115
Hey everyone! I'm kind of new to C++, but know most of the basic and some advanced stuff. I am currently making a program that requires users to type in a letter, and if that letter is written then the user has to input a string. I use an IF statement in my program, but for some reason the program crashes everytime a user types the sentence. Here is the program...

(The original code is too long, so I made a smaller scenario.)



NOTE: There is a SWITCH statement that encases the if statement, too.
-----------------------------------------------------------------------------------------------------------


#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <string>


using namespace std;

int main()

{

char s;
string sentence;

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

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

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

system("pause");

}
Mar 5 '07 #1
7 2527
Hey everyone! I'm kind of new to C++, but know most of the basic and some advanced stuff. I am currently making a program that requires users to type in a letter, and if that letter is written then the user has to input a string. I use an IF statement in my program, but for some reason the program crashes everytime a user types the sentence. Here is the program...

(The original code is too long, so I made a smaller scenario.)



NOTE: There is a SWITCH statement that encases the if statement, too.
-----------------------------------------------------------------------------------------------------------


#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <string>


using namespace std;

int main()

{

char s;
string sentence;

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

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

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

system("pause");

}
I checked and the problem must be in your function system("pause"); because without it it works perfectly in my computer
Mar 5 '07 #2
willakawill
1,646 1GB
No crash with this version:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2.  
  3. {
  4.  
  5.     char s;
  6.     string sentence;
  7.  
  8.     cout << "Type 's' to type a sentence: ";
  9.     cin >> s;
  10.  
  11.     if (s == 's')
  12.     {
  13.         s = getchar();
  14.         cout << endl << "Type a sentence: " << endl;
  15.         getline(cin, sentence);
  16.  
  17.         cout << endl << "You typed: " << sentence << endl;
  18.     }
  19.  
  20.     system("pause"); 
  21.     return 0;
  22.  
  23. }
Mar 5 '07 #3
Let me just say and sorry if I didn't before, I am using Dev-C++ 4.
Mar 5 '07 #4
Let me just say and sorry if I didn't before, I am using Dev-C++ 4.
No problem, Just ask if you've got any more doubts. Good Luck with your program!
Mar 5 '07 #5
Nope the program still doesn't work. I have heard about "cin.get();", and I think it has to do with my situation. But I'll still keep trying!
Mar 5 '07 #6
Nope the program still doesn't work. I have heard about "cin.get();", and I think it has to do with my situation. But I'll still keep trying!
If you want to load a string use gets(string name) from conio.h
Mar 5 '07 #7
If you want to load a string use gets(string name) from conio.h
Except for the function system(), it works perfectly in dev c++ 4.9
Mar 5 '07 #8

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
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,...

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.