catlover2
May 5th, 2012, 01:17 AM
Hello world!
If I compile the following code,
#include <iostream>
#include <string>
using namespace std;
#define NEWLINE "\n"
int main ()
{
string w_string;
cout << "Please enter some combination of characters that DOES include some whitespace: ";
getline (cin,w_string);
cout << NEWLINE << "You just entered the string '" << w_string << "'" << NEWLINE;
return 0;
}
it runs as expected:
$ ./a.out
Please enter some combination of characters that DOES include some whitespace: Hi there!
You just entered the string 'Hi there!'
BUT, if I add more to it as follows,
include <iostream>
#include <string>
using namespace std;
#define NEWLINE "\n"
#define TWOLINE "\n \n"
int main ()
{
cout << "Please enter a number: ";
int a_number;
cin >> a_number;
cout << NEWLINE;
cout << "You just entered the number " << a_number << ", and you get " << a_number+1 << " if you add 1 to it." << TWOLINE;
string w_string;
cout << "Please enter some combination of characters that DOES include some whitespace: ";
getline (cin,w_string);
cout << NEWLINE << "You just entered the string '" << w_string << "'" << NEWLINE;
return 0;
}
then it still compiles but outputs this
$ ./a.out
Please enter a number: 7
You just entered the number 7, and you get 8 if you add 1 to it.
Please enter some combination of characters that DOES include some whitespace:
You just entered the string ''
without waiting for my input.
What am I missing?
-catlover
If I compile the following code,
#include <iostream>
#include <string>
using namespace std;
#define NEWLINE "\n"
int main ()
{
string w_string;
cout << "Please enter some combination of characters that DOES include some whitespace: ";
getline (cin,w_string);
cout << NEWLINE << "You just entered the string '" << w_string << "'" << NEWLINE;
return 0;
}
it runs as expected:
$ ./a.out
Please enter some combination of characters that DOES include some whitespace: Hi there!
You just entered the string 'Hi there!'
BUT, if I add more to it as follows,
include <iostream>
#include <string>
using namespace std;
#define NEWLINE "\n"
#define TWOLINE "\n \n"
int main ()
{
cout << "Please enter a number: ";
int a_number;
cin >> a_number;
cout << NEWLINE;
cout << "You just entered the number " << a_number << ", and you get " << a_number+1 << " if you add 1 to it." << TWOLINE;
string w_string;
cout << "Please enter some combination of characters that DOES include some whitespace: ";
getline (cin,w_string);
cout << NEWLINE << "You just entered the string '" << w_string << "'" << NEWLINE;
return 0;
}
then it still compiles but outputs this
$ ./a.out
Please enter a number: 7
You just entered the number 7, and you get 8 if you add 1 to it.
Please enter some combination of characters that DOES include some whitespace:
You just entered the string ''
without waiting for my input.
What am I missing?
-catlover