Simple getline problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 89
Reputation: raul15791 is an unknown quantity at this point 
Solved Threads: 7
raul15791 raul15791 is offline Offline
Junior Poster in Training

Simple getline problem

 
0
  #1
Sep 2nd, 2008
I have this problem. This program runs well except that it skip the value of the first item in list. I think it is because of the cin.ignore(). But if I don't use the cin.ignore(). The cin will only read the first input and none after that. Help?

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class list // list class
  7. {
  8. private:
  9. string *Data;
  10. int size;
  11. int num;
  12.  
  13. public:
  14. list() // constructor
  15. {
  16. size = 1;
  17. num = 0;
  18. }
  19.  
  20. void add(); // member function
  21. void display();
  22. };
  23.  
  24.  
  25.  
  26. void list :: add()
  27. {
  28. const int SIZE_INPUT = 50;
  29. char choice;
  30. char temp_input[SIZE_INPUT] = "";
  31.  
  32. do
  33. {
  34. Data = new string[size];
  35. cout << "\n Enter item\t: ";
  36.  
  37. if(num>0)
  38. {
  39. cin.ignore(INT_MAX, '\n' );
  40. }
  41.  
  42. cin.getline(temp_input, sizeof(temp_input));
  43.  
  44. Data[num] = temp_input;
  45.  
  46. num++;
  47. size++;
  48.  
  49. cout<<"Continue to Enter Item (Y=Yes/N=No)\t:\t";
  50. cin >> choice;
  51.  
  52. } while(tolower(choice) != 'n');
  53. }
  54.  
  55. //This function only display the last item. But if I try to display item one by one, no problem except that I cant display the first item (because of the cin.ignore?).
  56. void list :: display()
  57. {
  58. int i;
  59.  
  60. cout << num << endl;
  61.  
  62. for (i=0; i<num; i++)
  63. {
  64. cout << Data[i] << endl;
  65. }
  66. }
  67.  
  68. int main()
  69. {
  70. list testing;
  71.  
  72. testing.add();
  73. testing.display();
  74.  
  75. system("PAUSE");
  76. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Simple getline problem

 
0
  #2
Sep 2nd, 2008
Read http://www.augustcouncil.com/~tgibso....html#problems and perhaps you need to call cin.clear() to reset some of the flags
Last edited by jencas; Sep 2nd, 2008 at 4:51 am.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Simple getline problem

 
0
  #3
Sep 2nd, 2008
> Data = new string[size];
Not only does this leak memory, it also loses all your previous input.
You need a way of expanding your array of lines.
Have you considered that std::vector would be ideal for this.

Put the cin.ignore directly after any I/O functions which leave newlines behind.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 41
Reputation: RenjithVR is an unknown quantity at this point 
Solved Threads: 7
RenjithVR RenjithVR is offline Offline
Light Poster

Re: Simple getline problem

 
0
  #4
Sep 3rd, 2008
Replace your line 9 with "string Data[20];"
also don't forget to comment line 34.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 89
Reputation: raul15791 is an unknown quantity at this point 
Solved Threads: 7
raul15791 raul15791 is offline Offline
Junior Poster in Training

Re: Simple getline problem

 
0
  #5
Sep 3rd, 2008
RenjitVR, if I do that, means I can only input maximum 20 string item right? What I'm trying to get is unlimited input. Something like a simple dynamic array.

** Thanks Jencas and Salem for your reply
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC