By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

Getline Problem

compman9902
100+
P: 105
Thank you for reading this post, first of all.
My problem is that whenever I try to use the following code:
Expand|Select|Wrap|Line Numbers
  1. std::getline(cin,original);
Or, the getline method, it compiles fine, but when I run the program, it dosn't allow the user to enter anything. In other words, the length of the string original is set to zero automatically. When I use the following code:
Expand|Select|Wrap|Line Numbers
  1. cin  >> original
it works perfectly, but with that method, it cannot have spaces in the input. Here is the code I'm trying to use in cotext:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <sstream>
  6. #include <string.h>
  7. #include <fstream>
  8. using namespace std;
  9. int main()
  10. {
  11. string original;
  12. cout << "Please enter in the string:" << endl;
  13. cout << "String: " << endl;
  14. std::getline(cin,original);
  15. cout << "Your String Is: \n\n\n" << endl;
  16. cout << original;
  17. return 0;
  18. }
Thank you for your help and your time.

--Parker Woods
Mar 22 '07 #1
Share this Question
Share on Google+
9 Replies


Expert 100+
P: 1,510
the program works fine using g++ and Borland CBuilder. What compiler / operating systems are you using?
Mar 22 '07 #2

compman9902
100+
P: 105
the program works fine using g++ and Borland CBuilder. What compiler / operating systems are you using?
Sorry, my mistake. I forgot to put my compiler and OS.
Operating System: Windows XP Home Edition (Service Pach Two)
Compiler: DEV-C++ 4.9.9.2

This should tell yo enough. But thanks for telling we which systems it works on :).
Mar 22 '07 #3

DeMan
100+
P: 1,806
I am using close to the same setup as you and it is working. The problem I think is that the output screen disappears before you see the output. you can insert a line like
system("pause");
befiore your return and you should see the output.
Mar 22 '07 #4

compman9902
100+
P: 105
Sorry, my mistake. I forgot to put my compiler and OS.
Operating System: Windows XP Home Edition (Service Pach Two)
Compiler: DEV-C++ 4.9.9.2

This should tell yo enough. But thanks for telling we which systems it works on :).
Also, I'm sorry I didn't say this earlier, but i'm using this in quite a few loops and CASE statments. I heard on a different post (not sure which one) that this may effect it. Here is the code in my actual program (the other was just a dummy :))
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <sstream>
  6. #include <string.h>
  7. #include <fstream>
  8. using namespace std;
  9. int main()
  10. {
  11. int loopOne;
  12. int selectMode;
  13. int length;
  14. string original;
  15. string line;
  16. string fileOpen;
  17. loopOne = 1;
  18. while (loopOne !=0)
  19. {
  20. cout << "\n\n\nWhat Would You Like To Do:" << endl;
  21. cout << "Read A Text (.TXT) File - Option [1]" << endl;
  22. cout << "Write A Text (.TXT) File - Option [2]" << endl;
  23. cout << "\nPlease Select An Option Number: ";
  24. cin  >> selectMode;
  25. switch (selectMode)
  26. {
  27. case 1:
  28. cout << "You Have Chosen To Read A Text File" << endl;
  29. cout << "\nPlease Enter The Path For The File To Be Read:\n" << endl;
  30. cin  >> fileOpen;
  31. ofstream fileopen;
  32. fileopen.open(fileOpen.c_str());
  33. while (! fileopen.eof() )
  34. {
  35. getline (fileopen,line);
  36. line = line + "\n";
  37. original = original + line;
  38. }
  39. fileopen.close();
  40. }
  41. }
  42. return 0;
  43. }
Once again, it is only the "getline (fileopen,line);" line that is bothering me. Thank you for you help, time, wisdom, and allknow power.
Mar 22 '07 #5

Expert 100+
P: 1,510
in
Expand|Select|Wrap|Line Numbers
  1. ofstream fileopen;
  2. fileopen.open(fileOpen.c_str());
  3. while (! fileopen.eof() )
  4. {
  5. getline (fileopen,line);
fileopen is a ofstream(output stream) and then you attempt to read from it - change it to an input stream?
Mar 22 '07 #6

compman9902
100+
P: 105
in
Expand|Select|Wrap|Line Numbers
  1. ofstream fileopen;
  2. fileopen.open(fileOpen.c_str());
  3. while (! fileopen.eof() )
  4. {
  5. getline (fileopen,line);
fileopen is a ofstream(output stream) and then you attempt to read from it - change it to an input stream?
This may sound really dumb but..how?
Mar 22 '07 #7

compman9902
100+
P: 105
This may sound really dumb but..how?
Never mind..Just change the o to a i. Wow..talk about logic.
Thanks you guys fixed it once again :)
But, please read the first post's problem. It is strange..No?
Well, I'll be posting all night..I have a feeling we'll get to know each other soon--up and comming programmer you know?
Just look a my profile ;)
Well, thanks again
Bye
Mar 22 '07 #8

Expert 100+
P: 1,510
Never mind..Just change the o to a i. Wow..talk about logic.
Thanks you guys fixed it once again :)
But, please read the first post's problem. It is strange..No?
Well, I'll be posting all night..I have a feeling we'll get to know each other soon--up and comming programmer you know?
Just look a my profile ;)
Well, thanks again
Bye
tried your first program using DEV-C++ and it works OK
Mar 22 '07 #9

compman9902
100+
P: 105
tried your first program using DEV-C++ and it works OK
I know..It was just a dummy as I said later.
But the problem that I stated in that post was what I wanted you to look at
But the issue is resolved and I no longer require your assistance...For now
Thank you



--Parker Woods
Mar 22 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.