getline not working right

This is a discussion on getline not working right within the C++ Programming forums, part of the General Programming Boards category; i was working on some code and ran into some trouble. the getline function seems to be getting the data ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    69

    getline not working right

    i was working on some code and ran into some trouble. the getline function seems to be getting the data incorrectly.

    i tried the cin.ignore(1) to fix the problem with using getline and >> in the same program, which allowed me to provide the input to the getline functions, but it ignored the first letter of the first instance of getline. (it got 'ilac' instead of 'Lilac', but worked perfectly for the rest of them).

    now, when it is reading from the data file, it correctly gets the first tape_name, show date, location, but then it misses the show_name.

    the rest of the data is offset after that and doesn't fit into the outputs.

    here's the code for the input data:
    Code:
    void inputdata()
    {
    	ofstream outfile;
    	outfile.open("tapes2.dat", ios::app);
    	assert(outfile);
    
    	string tape_name_temp;
    	int month=0;
    	int day=0;
    	int year=0;
    	int hour=0;
    	int minute=0;
    	string show_name_temp;
    
    	cout<<"Please input the tape name: ";
    	cin.ignore(1);
    	getline(cin, tape_name_temp);
    	outfile<<tape_name_temp <<'\n';
    	
    	cout<<"Please input the date in mm dd yyyy format: ";
    	cin>>month >>day >>year;
    	outfile<<month <<'\n';
    	outfile<<day <<'\n';
    	outfile<<year <<'\n';
    
    	cout<<"Please input the hour and minute of the show in hh mm format: ";
    	cin>>hour >>minute;
    	outfile<<hour <<'\n';
    	outfile<<minute <<'\n';
    
    	cout<<"Please input the title of the show: ";
    	cin.ignore(1);
    	getline(cin, show_name_temp);
    	outfile<<show_name_temp <<'\n';
    
    	outfile.close();
    }
    and then here is the output of the program after it has had some data entered into the file:

    datafile is 3 places.
    tape name: ilac
    show date: 12 1 2003
    location: 3:30
    show name:
    tape name: Grounded for life
    show date: -858993460 -858993460 -858993460
    location: -858993460:-858993460
    show name:
    tape name:
    show date: -858993460 -858993460 -858993460
    location: -858993460:-858993460
    show name:
    Please input the tape name:

    and attached is the complete code if anyone wants to compile it
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    oh, and here's the Myfuncs.h file too
    Attached Files Attached Files

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    use ignore() after getline()

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    ignore after getline in what part? the inputting to file part or the reading from file part?

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    When I read a file, I never use ignore(). But, if it works that way for yours, use ignore() after getline() for both.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Just had a quick look at your code,
    Code:
    void inputdata()
    {
      // code
      cout<<"Please input the tape name: ";
      cin.ignore(1);
      getline(cin, tape_name_temp);
      outfile<<tape_name_temp <<'\n';
      // code
    Does not your code do the following:

    writes "Please input the tape name:"
    remove first byte from input stream
    read data from inputstream into tape_name_temp
    write tape_name_temp

    Move the cin.ignore till after the getline(...); and you should get the filename correctly.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    any reason to use the first ignore() at all? I'd just delete it. I'd suggest using ignore() after calls to >> or get() or other istream methods that potentially leave terminating char or other material in the input buffer and before getline(). Doing it rotely before getline() can have it's own problems, as demonstrated.

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I didn't take a very long look at your problem, but it might be a bug in the STL implementation used by Visual C++ 6.0. Check out this site and scroll down to the part about <string>

    http://www.dinkumware.com/vc_fixes.html

    or look at this one

    http://support.microsoft.com/default...;en-us;Q240015

    Once you apply their fix, you should be able to use getline without the ignore.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    alrighty, i fixed those bugs.

    one more question, is there any way to resize a vector during the program?

    i ask because upon deleting an element from the data file, the vector must be one element smaller as well. same thing goes when i add an element to the data file, i need to re-open the file and get the data again, but i'm unsure how to re-create the vector with the new correct number of spaces.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is there any way to resize a vector during the program?
    Look up the resize() and clear() member functions of the vector class.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    wow, got that done. now the program seems to be working pretty well! thanks for the help!

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    I open tapes2.dat with notepad it looks ok except cin.ignore(1); will delete the very first charactor entry when you dont need too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() help
    By AndyBomstad in forum C++ Programming
    Replies: 9
    Last Post: 02-17-2005, 02:39 PM
  2. return value for global getline method
    By danglingelse in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2005, 02:54 PM
  3. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 05:42 PM
  4. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 12:12 PM
  5. getline problem
    By jriano in forum C++ Programming
    Replies: 3
    Last Post: 06-24-2003, 11:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21