cin.getline and cin problems in a while loop

This is a discussion on cin.getline and cin problems in a while loop within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been learning C++ for about 8 weeks now and I'm having a little problem with an exercise I've ...

  1. #1
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128

    Question Beginner Programmer seeks help, Please, cin.getline in a while loop

    Hi, I've been learning C++ for about 8 weeks now and I'm having a little problem with an exercise I've been doing, the program in itself is very simple, take a string from the keyboard display information about its length and display it in forward and reverse oreder, no problem.

    The problem I have come across is that when I put it into a while loop to make the program run again if 'y' is entered and terminate if 'n' is entered, if 'n' is entered it terminates but if 'y' is entered, the second time the code runs (and subsequent) times it seems to skip past the cin.getline command and just print out a length of nothing and print out the forward and reverse of nothing, here is my code.

    Code:
    #include <iostream.h>
    #include <string.h>
    #include <conio.h>
    #include <mem.h>
    #define NULL '/0'
    
    void arraylength (void);
    
    void main()
    {
    	 arraylength();
    }
    
    void arraylength (void)
    {
    	 char array[101];
    	 char yesno;
    
    
    	 while (yesno!='n'&&yesno!='N')
    	 {
    		  memset (array, NULL, sizeof(array));
    		  clrscr();
    		  cout<<"Enter a sentence up to 100 hundred characters.\n\n";
    		  cin.getline(array,100,'\n');
    
    		  cout<<"\nLength: "<<strlen(array)<<"\n";
    
    		  cout<<array<<"\n";
    		  cout<<strrev(array)<<"\n\n";
    
    		  cout<<"Again?: ";
    		  cin>>yesno;
    	 }
    	 cout<<"\n\n";
    	 cout<<"Thankyou Bye.";
    }
    I tried to solve the problem by intitializing the string with null values but that hasn't solved the problem, I guess it's something to do with that line of code as it just seems to ignore it, I want the program to continue until the iser signals its end by entering 'n' or 'N', any help is greatly appreciated.

    Thanks.

    Ben Leedham.
    Last edited by UnclePunker; 05-07-2002 at 08:24 AM.

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    either cout << flush or i think you can also use cin.ignore()

  3. #3
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Hi, as I say I'm only a beginner, so I don't really understand what you mean by cout<< flush or cin.ignore, I don't want any of the code to be ignored but it seems that it is doing, maybe my explanation isn't very clear, sorry I am trying.

    Thanks.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    6

    void main is illegal!

    Being a beginner myself it would defiantly be to your benefit to get a book that complies with the ANSI/ISO standards. Not trying it preach but you should learn correctly if your book is teaching you to program with void main I would say get a new book that complies with ANSI/ISO standards, if you just starting out it would be wise to learn by the standards.

    http://www.research.att.com/~bs/bs_faq2.html#void-main
    Last edited by Tenacious_newb; 05-07-2002 at 08:54 AM.

  5. #5
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    I'm sorry but that sort of thing doesn't really help, I also understand how to use:

    Code:
    int main(void)
    {
         //statements
        return 0;
    }
    but it just so happens that on this program I haven't, I alternate between the two to keep both methods fresh in my head, people have different views on this subject and as I am a beginner I am keeping my options open.

    If you have any useful comments about the question asked it would be very helpful, I have thought about it using everything that I know so far and I haven't come up with the answer so that leasd me to the conclusion that the answer lies in something else that I don't know, like do I have to refresh the exe to take cin for the same string?

    stuff like that would be greatly appreciated, if I'd asked for a critique on my code structure then I would be happy with your answer, I didn't and I'm not in the business of other people bigging themselves up by putting others down, don't make assumptions.

    Thanks.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    6
    Iím sorry you interpreted my comments as putting you down, that wasnít my intention. Putting the book that your learning from down maybe. I also sent you a link from the designer of the language and member of the standards committee. If you dont want to learn by the standards thatís your right, I also being an newb just tried to give you some info that I thought you might find useful. But dont worry I wont make that mistake again!
    Last edited by Tenacious_newb; 05-07-2002 at 09:20 AM.

  7. #7
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    I'm sorry mate, I have just been trying to sort this problem out all day, and the fact that the program worked fine before it went into a while loop is even more frustrating, I realise you are just trying to help and I am grateful for that, I am actually not learning from a book I am learning at college night school and the teacher has taught us that people opinions differ, I seriously didn't mean to sound off hand saying I didn't want those sort of comments I'm just getting annoyed with he code as it has to be something that I don't know that is the problem (that is not supposed to sound arrogant), so again I apologise and I hope to receive help from you again in the future, please forgive me.

    Ben.

  8. #8
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    this should work...

    after getting the line use this code (i think) to ........ errrrrr...... im kind of a beginner too..... but something about clearing input buffers (i think)

    Code:
    cin.ignore(80, '\n');
    if im wrong about the buffer thing someone who knows tell me please
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    I think your problem has to do with buffered input
    You may have a \n chaacter still in the buffer (having pressed enter)
    So the next time you cin it reads the enter from the buffer and seemingly skips your input that is why you need to flush the buffer I am just a newbie too but hope that helps
    I am guessing after your cin >> yesno;

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    6
    Sorry I did change it a little, but it works,




    Code:
    #include <iostream.h>
    #include <string.h>
    #include <conio.h>
    #include <mem.h>
    #include <stdlib.h>
    #include <cctype>
    
    void arraylength (void);
    
    int main()
    {
    	 arraylength();
      system("PAUSE");
          return 0;
    }
    
    void arraylength (void)
    {
    	 char array[101];
    	 char yesno;
    
    
    	 while (tolower(yesno)!='n')
    	 {
    		  memset (array, NULL, sizeof(array));
    
    		  cout<<"Enter a sentence up to 100 hundred characters.\n\n";
    		  cin.getline(array,100,'\n');
    
    		  cout<<"\nLength: "<<strlen(array)<<"\n";
    
    		  cout<<array<<"\n";
    		  cout<<strrev(array)<<"\n\n";
    
    		  cout<<"Again?: ";
    		  cin>>yesno;
              cin.ignore(101, '\n');
    
          }
    	 cout<< endl;
    	 cout<<"Thankyou Bye.\n";
    
         }

  11. #11
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Thanks you lot, that sounds like it makes sense to me, thats what I was pretty much thinking but wasn't sure how to go about it, the cin.ignore(80, \n) command, would I substitute 80 for the length of my string, I'm just asking as I'm typing, I'll have a go now, again, very much appreciated.

    Ben.

  12. #12
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Hopefully I've got you now, is 80 the the screen size, as I say I've only been doing it a bit so these things are new to me, basically the way I understand it is that ignores a full line unless a new line is encountered, ifthat is correct then I am happy if not then I'm still happier than earlier today.

    Ben.

  13. #13
    Unregistered
    Guest
    cin.ignore();

    would probably work just fine, too, as long as the user behaves appropriately. This uses the default value of ignoring one char, presumably the new line char left in the input buffer by the call to cin >>, and the default terminating char of newline. 80 is an arbitrary value in case the user has pressed the space bar and not let up, or pressed a bunch of keys in frustration. Even ignoring up to 80 char isn't a gaurantee that you have cleared the intake buffer. You may want to ignore up to the limit of the size of the input buffer, listed in limits.h somewhere, I believe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin and getline() problems....
    By AdampqmabA in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2008, 06:55 PM
  2. Problems with cin and GetAsyncKeyState
    By KgNe in forum C++ Programming
    Replies: 32
    Last Post: 08-21-2008, 11:00 AM
  3. while (cin) loop problems
    By SeXy_Red in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2005, 04:04 PM
  4. cin >> buf endless loop
    By cfriend in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 05:01 PM
  5. cin infinite loop
    By progbass8 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2005, 04:50 PM

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