Author Topic: easy c++ question  (Read 861 times)

0 Members and 1 Guest are viewing this topic.

Madrun Badrun

  • twin-anused mascot
  • Senior Member
easy c++ question
« on: November 07, 2008, 11:51:44 PM »
Code: [Select]
#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. 

MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #1 on: November 07, 2008, 11:56:13 PM »
What do you mean by the white space?

Have you tried instead of getLine doing cin >> textMessage >> "\n";
« Last Edit: November 07, 2008, 11:58:16 PM by MyNameIsMud »
USA

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: easy c++ question
« Reply #2 on: November 07, 2008, 11:58:03 PM »
A space. like the thing in between the the quotes ' '.  What the spacebar makes.   

MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #3 on: November 07, 2008, 11:59:00 PM »
Well \n doesn't do a space, so do


getline(cin, textMessage, " ", '\n');

Try that. Or try inserting " "  someweres.
USA

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: easy c++ question
« Reply #4 on: November 08, 2008, 12:01:49 AM »
the third parameter is when the the string stops getting input so getline(cin, textMessage, " "); is exactly what I don't want. 

MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #5 on: November 08, 2008, 12:02:52 AM »
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.
USA

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: easy c++ question
« Reply #6 on: November 08, 2008, 12:09:59 AM »
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++

MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #7 on: November 08, 2008, 12:11:08 AM »
If you do this:

Code: [Select]
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.
USA

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: easy c++ question
« Reply #8 on: November 08, 2008, 12:16:16 AM »
that just takes the first word in the sentence into the string.  Which is my problem.

Code: [Select]
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.


MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #9 on: November 08, 2008, 12:20:09 AM »
Well then it's a different operator than a string I think then.
USA

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: easy c++ question
« Reply #10 on: November 08, 2008, 12:21:37 AM »
Code: [Select]
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!!!

MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #11 on: November 08, 2008, 12:28:33 AM »
C++ sucks dick. Everything looks right, just can't figure it out.
USA

MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #12 on: November 08, 2008, 12:30:31 AM »
Code: [Select]
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
USA

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: easy c++ question
« Reply #13 on: November 08, 2008, 12:33:45 AM »
That's normal c++

what I don't get is why

Code: [Select]
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. 

MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #14 on: November 08, 2008, 12:35:05 AM »
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?
USA

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: easy c++ question
« Reply #15 on: November 08, 2008, 12:36:10 AM »
but then the second getline should also pass in blanks but it doesn't. 

MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #16 on: November 08, 2008, 12:38:16 AM »
Got it.

Code: [Select]
#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.
USA

MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #17 on: November 08, 2008, 12:43:35 AM »
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.
USA

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: easy c++ question
« Reply #18 on: November 08, 2008, 12:45:34 AM »
Thanks that works.  It's insane but it works. 

:piss C++
:bow  VB

MyNameIsMethodis

  • QUIT
  • Ebola Carrier
Re: easy c++ question
« Reply #19 on: November 08, 2008, 12:46:11 AM »
C# is awesome, but fuck C++.
USA

recursivelyenumerable

  • you might think that; I couldn't possibly comment
  • Senior Member
Re: easy c++ question
« Reply #20 on: November 08, 2008, 03:29:41 PM »
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/
« Last Edit: November 08, 2008, 04:20:25 PM by recursivelyenumerable »
QED

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: easy c++ question
« Reply #21 on: November 08, 2008, 04:05:26 PM »
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. 

recursivelyenumerable

  • you might think that; I couldn't possibly comment
  • Senior Member
Re: easy c++ question
« Reply #22 on: November 08, 2008, 04:07:35 PM »
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.
QED

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: easy c++ question
« Reply #23 on: November 08, 2008, 04:15:36 PM »
recursive :bow2

Can you guess what I'm thinking right now?

I'll give you a hint it starts with :h