This is very beginner, but...

I'm attempting to make a a simple program, very basic, and I've come across a problem.
Whenever I try to get a string value, it only gets the string up to the first space.
For example: I type 'Hello World.'
It writes 'Hello.'
Please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Hooray.

#include <iostream>
#include <string>

using namespace std;

int msg(string text)
{
    cout << text << '\n';
}

int main ()
{
 int a, b, Age;
 string tex;
  int result, test;
  cout << "I'm Bobobob12's first C++ program!" << '\n' << "Need to figure out some math? Enter the first number to be added: ";
  cin >> a;
  cout << "Now enter the secound number: ";
  cin >> b;
  result = a + b;
  cout << "The answer to " << a << " + " << b << " is " << result << ". Ha, that was random." << '\n';
  cout << "Please enter your age to continue: ";
  cin >> Age;
  if (Age<=10)
  {
  cout << "You are too young to continue!" << '\n';
}
  else
  {
  cout << "Age " << Age << " is accepted. You may now continue. " << '\n' << "Now enter a message to be displayed: ";
  cin >> tex;
  msg(tex);
  cout << "Why would you say a silly thing like that?" << '\n';
}
  system("PAUSE");
  return 0;
}
Use getline( cin, tex ) instead.

cin only reads up to whitespace (e.g. a space or an "enter"). To read a full string, you have to type:

getline(cin, tex);

But, if you're using cin and getline in the same program, before the getline, you'll wanna clear the stream:
cin.ignore(numeric_limits<streamsize>::max(), '\n');

But you'll need to
#include <limits>
Hope that helps a bit. Sorry for the complexity. :D

Oh, and system() is evil. Read this to see why:
http://cplusplus.com/forum/articles/11153/
Last edited on
If system is evil, what do I do instead?
It normally closes automaticly on me..
why do you need to get so in depth on such a simple task?
just use cin.get();
I don't see why everyone gets worked up over using system(). I know its bad for your program - I've read Duoas article which is a very interesting read to those who haven't(QWERTYman's post). But apart from giving yourself bad habbits whilst your learning c++ whats the harm? These console programs aren't exactly at industry level and, correct me if i'm wrong because this may be down to my own ignorarnce, but the chances of you needing to write an industry level console application doesn't sound high, unless maybe its for a *nix system.

I think we should be less concerned about the use of it, and more concerned with the books/tutors/etc who are teaching its use.
Last edited on
There isn't a problem with using on random code like this, but the habit is what the problem is. Most people won't bother to learn the "right" way to do it if the evil way is easier.
The odd thing is though, i've never seen an example in a book or any prior online tutorial that has used system(). So where is it coming from?
' ' Apart from all the horrors and failure that comes from using system(), what's the harm? ' '

' ' Excluding Unices, console programs aren't industry level? ' '

I sure hope this is just some sick joke...



I think we ought to be more concerned about (un)teaching people dangerous and foolish habits.

I cannot go and personally beat-up every bad tutor/book author/etc, nor can I personally dispose of every bad piece of information. Trying to do so is a total waste and utterly fruitless -- and would show me to be an absolute moron.

A better use of my time, which I offer you freely, is to teach correct habits and principles so that you can use your own brain to do some actual, informed, intelligent computing, instead of getting stupid about stupid stuff. If that occasionally means saying, "oh by the way..."


So, please, don't get so worked up about some passing advice intended to increase a programmer's worth. Seriously, anyone who has no respect for system integrity is a danger to his employer and his own self.

As for me, I'm bored right now...

[edit] Get distracted spraying the kids with anti-mosquito byteLOL bite spray...

I've seen it all over the net. Even on these forums people post saying 'use system()' to do stupid things like clear the screen and pause (the two most popular offenders) ... but even to do complicated things with processes and the like when a couple simple system calls would have been easier -- and infinitely safer.
Last edited on
Topic archived. No new replies allowed.