THE BORE
General => The Superdeep Borehole => Topic started by: Madrun Badrun on November 07, 2008, 11:51:44 PM
-
#include <iostream>
#include <string>
int getPositiveInt()
{
int number;
do
{
cout << "Please enter a positive number" << endl;
cin >> number;
}
while(number < 0);
return number;
}
void printText(int number, string textMessage)
{
for(int i = 0; i < number; i++)
{
cout << textMessage << endl;
}
}
using namespace std;
int main()
{
int number = getPositiveInt();
cout << "The positive value that was entered is " << number << endl;
string textMessage;
cout << "Please enter a message." << endl;
getline(cin, textMessage, '\n');
printText(number, textMessage);
return 0;
}
So this program asks the user for a number, then a message then prints the message that number of times. The problem is here getline(cin, textMessage, '\n'); I want the message to include the whitespace in between words (so a whole sentence), and only end when the user hits enter but the way I have it results in not even being able to enter a message because it sees the initial whitespace and thinks the message input is done.
-
What do you mean by the white space?
Have you tried instead of getLine doing cin >> textMessage >> "\n";
-
A space. like the thing in between the the quotes ' '. What the spacebar makes.
-
Well \n doesn't do a space, so do
getline(cin, textMessage, " ", '\n');
Try that. Or try inserting " " someweres.
-
the third parameter is when the the string stops getting input so getline(cin, textMessage, " "); is exactly what I don't want.
-
I just registered my copy of C++ so i'll work on it rightn ow incase someone doesn't come in and answers right away.
-
what I don't get is just getline(cin, textMessage) should be exactly what I want but it still only gives me the first word in the line. grrr I hate c++
-
If you do this:
string textMessage;
cout << "Please enter a message." << endl;
cin >> textMessage;
It lets you enter a messsage in. I'll try and get it to display, I have an idea whats not doing it (your prototype is void), but I haven't figured it out yet.
-
that just takes the first word in the sentence into the string. Which is my problem.
int main()
{
int number = getPositiveInt();
cout << "The positive value that was entered is " << number << endl;
string textMessage;
cout << "Please enter a message." << endl;
getline (cin, textMessage);
printText(number, textMessage);
return 0;
}
This should be it but now it won't even let me enter anything. It just prints blank lines.
-
Well then it's a different operator than a string I think then.
-
int main()
{
int number = getPositiveInt();
cout << "The positive value that was entered is " << number << endl;
string textMessage;
cout << "Please enter a message." << endl;
getline(cin, textMessage); // doesn't ask for imput
printText(number, textMessage); // only prints blanks
getline(cin, textMessage);
printText(number, textMessage);
return 0;
}
OK this works except it prints blank lines in the first set and then works perfectly fine in the second set. WTF? IT'S THE SAME STATEMENT TWICE DOING TWO DIFFERENT THINGS!!!
-
C++ sucks dick. Everything looks right, just can't figure it out.
-
int number = getPositiveInt();
cout << "The positive value that was entered is " << number << endl;
string textMessage;
cout << "Please enter a message." << endl;
cin >> textMessage;
//getline(cin, textMessage); // doesn't ask for imput
printText(number, textMessage); // only prints blanks
//getline(cin, textMessage);
printText(number, textMessage);
Like you said, only prints the first word if you get rid of getline....wonder why that operator is not taking more than the first word
-
That's normal c++
what I don't get is why
getline(cin, textMessage); // doesn't ask for imput
printText(number, textMessage); // only prints blanks
getline(cin, textMessage);
printText(number, textMessage);
doesn't work for the first two lines, but works right for the other two.
-
Well it's apparent, atleast from what I can see, that getLine is passing in blanks. So in theory it works, but it's just printing blanks.
What is getLine used for anyways?
-
but then the second getline should also pass in blanks but it doesn't.
-
Got it.
#include <iostream>
#include <string>
using namespace std;
int getPositiveInt()
{
int number;
do
{
cout << "Please enter a positive number" << endl;
cin >> number;
}
while(number < 0);
return number;
}
void printText(int number, string textMessage)
{
for(int i = 0; i < number; i++)
{
cout << textMessage << endl;
}
}
int main()
{
int number = getPositiveInt();
cout << "The positive value that was entered is " << number << endl;
string textMessage;
cout << "Please enter a message." << endl;
getline(cin, textMessage); // doesn't ask for imput
//printText(number, textMessage); // only prints blanks
getline(cin, textMessage);
printText(number, textMessage);
return 0;
}
you just need to comment out / remove one of the printlines. Don't ask me why you need two getlines though. That's just weird shit.
-
You can actually move one of the getLines anyweres else in the main function. For some reason I guess you just need to set the memory as blank, THEN have it write to memory.
-
Thanks that works. It's insane but it works.
:piss C++
:bow VB
-
C# is awesome, but fuck C++.
-
Think it's actually your getPositiveInt() that's weirding things out. cin >> number is only extracting the characters for the number itself, not the terminating newline, so the newline is still there in the stream for getline to see before anything else. That's why getline appears to work the second time but not the first. I found this by googling around BTW (my C++ work has all been writing COM DLLs through ATL so I'm not too familiar with iostream), here's a thread on another forum started by someone with almost the same problem, in which a solution is presented: http://www.linuxquestions.org/questions/programming-9/g-and-cin.getline-66968/
-
Thanks. Asked on my uni's compsci forum and my prof said it was the cin before the getline that was fucking it up too.
What was really confusing though is that when I first started getting this problem, the getline was not just taking in a blank but would take in only the first word and end at the space in between the words. O well the thing works now.
-
to go back to my favorite topic --- my ego --- I did guess that this might be the problem before googling for info on istream operator>> semantics.
-
recursive :bow2
Can you guess what I'm thinking right now?
I'll give you a hint it starts with :h