Thread: Another cin.getline() thread

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    16

    Another cin.getline() thread

    Yeah, Hello. I know there alot of getline() post but tehy didnt help me in my problem.
    Code:
    int Save()
    {
      ifstream fins1("Saves/save1.fss");
      ifstream fins2("Saves/save2.fss");
      ifstream fins3("Saves/save3.fss");
      ifstream fins4("Saves/save4.fss");
      fins1>>save1;
      fins2>>save2;
      fins3>>save3;
      fins4>>save4;
      fins1.close();
      fins2.close();
      fins3.close();
      fins4.close();
      cout<<"\nWARNING!: Saving a game will over-write the old saved game.\n";
      int sel=0;
      cout<<"\nPlease select a slot to save in:\n";
      cout<<"Slot 1:"<<save1<<"\n";
      cout<<"Slot 2:"<<save2<<"\n";
      cout<<"Slot 3:"<<save3<<"\n";
      cout<<"Slot 4:"<<save4<<"\n";
      cout<<"5:Cancel\n";
      cout<<"Save in which slot or cancel?";
      cin>>sel;
      switch (sel)
      {
      case 1:
          {
           ofstream fouts1("Saves/save1.fss");
           cout<<"Enter save name?";
           cin.getline (save1, 255);
           fouts1<<save1<<"\n";
           fouts1<<intel<<"\n";
           fouts1<<str<<"\n";
           fouts1<<day<<"\n";
           fouts1<<hour<<"\n";
           fouts1<<clr<<"\n";
           fouts1<<salary<<"\n";
           fouts1<<money<<"\n";
           fouts1<<TRtimeleft<<"\n";
           fouts1<<TRpdt<<"\n";
           fouts1<<pelletsdropedt<<"\n";
           fouts1<<numfish<<"\n";
           fouts1.close();
           return Menu();
          }
          break;
    It complies fine but because you have to select a save slot before you enter a name it skips right past the getline(); and gives save1 a name of 0 meaning NULL I guess.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >cin>>sel;
    This line ends up leaving a newline in the stream.

    >cin.getline (save1, 255);
    This line picks up the newline left over and thinks that it's reached the end of input.

    So while it looks like the call to getline is being skipped, it just terminates on the first character because getline is delimited by a newline. You have two solutions to the problem. The first is to use cin.ignore() to eat the newline after every cin>>something; call. The second is to use getline for all input and then perform conversions after you have the line in memory.

    The first would look like this:
    Code:
    cin>> sel;
    cin.ignore();
    
    ...
    
    cin.getline(save1, 255);
    The second is more flexible, but requires you to include some new headers. For now the ignore approach will suffice, but the FAQ will show you several different ways to go about it if you're interested.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM