Thread: cin.getline issue

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    cin.getline issue

    So this program is supposed to take the sentence you put in, then capitalize it and print it out backwards. And it does that fine. However, as soon as I try to put it in a loop to repeat it as often as wanted, it acts as if the sentence has already been given, and doesn't let you put in a new one. I'm using Visual C++ 6.0.
    Code:
    #include "stdafx.h"
    #include <iostream.h>
    #include <cmath>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include "stdio.h"
    
    
    int main(int argc, char* argv[])
    {
    	const int max = 80;
    	char sentence[max];
    	int i = 0;
    	int x=0;
    	int count = 0;
    
    	char cont = 'y';
    
    	do
    	{
    		system("CLS");
    		cout << "Please input your phrase here. ";
    		cin.getline(sentence,max);
    	
    		x=strlen(sentence);
    		//cout <<x;
    	/*	do
    		{
    			count++;			
    		}
    		while (sentence[count] != '\0');*/
    
    		i = x;
    		do
    		{
    			cout << sentence[i];
    			i--;
    		}
    		while (i >= 0);
    		cout << endl;
    		cout << "Would you like to try again? y/n ";
    		cin >> cont;
    		i = 0;
    		count = 0;
    	//	sentence[max] = '\0';
    	}
    	while (cont == 'y');
    
    	return 0;
    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    After reading input with >>, cin contains a newline (from pressing Enter). getline next sees the newline, consumes it and thinks it has already read a line of input. So you get to another prompt to repeat.

    When mixing getline and >>, you'll need to clean the stream from left-over characters after using >>.

    Code:
    #include <limits>
    
    ...
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    Other notes:

    I'm using Visual C++ 6.0.
    I guess an upgrade is suggested since that compiler does not support standard C++ correctly.

    Code:
    #include <iostream.h>
    #include <cmath>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include "stdio.h"
    It is strange that you know about <cmath> but not that the same thing applies to all standard C headers in C++: drop the extension and prepend 'c'. Also there's no <iostream.h>, it's <iostream> (standard headers don't have an extension) and all standard identifiers live in the std namespace. Hence:

    Code:
    #include <iostream>
    #include <cmath> //you don't seem to be using anything from this one
    #include <cstdlib>
    #include <cstring>
    #include <cctype> //I guess you'll also need to use toupper from this one
    #include <cstdio> //you are not using this one
    
    using namespace std;
    Last edited by anon; 04-17-2009 at 08:05 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    Okay, so I'll try that tomorrow when I get back to my school computer. Yes, this is a school assignment; yes, I did get the teacher's permission before asking here. He doesn't even know how to fix it. That's also kinda why I can't upgrade to a newer Visual C++.
    We were taught to use <iostream.h>; I don't know why. We don't even know exactly what the headers do. We just use what we were told.
    Also, what exactly does "using namespace std;" do? Every time I've seen it mentioned on the internet as a fix for something, it doesn't work.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> what exactly does "using namespace std;" do?

    It allows the compiler to look in the standard library for the names you use in your program. Things like cout and cin are part of the standard library, and those names are in the std namespace. So either you type std::cout or you can type cout but tell the compiler to check for cout under the std namespace.

    In your case, though, because you use <iostream.h>, you wouldn't need to worry about the std namespace, because <iostream.h> was the header from before the standard was created. You can (and probably should) switch to <iostream> if you use VC++ 6.0, because that compiler does support most of the standard. However, if you're just trying to get through the class then it's ok to do whatever works as long as you know that there are more proper ways of doing things.

    BTW, the fix for your problem as pointed out by anon is to use cin.ignore(). However, you probably don't need the fancier version shown. Instead, simply using cin.ignore() after every call to cin >> should work. (Don't put cin.ignore() after calls to cin.getline, because getline does the ignore automatically.)

    The reason this is necessary is that when the user of your program types an answer and hits <enter>, the <enter> leaves a newline character in the stream. Using cin>> leaves that newline there, and later cin.getline sees the newline and stops because it's job is to get a line and when it sees the newline it thinks the line is finished. The cin.ignore() causes the extra newline to be ignored, so later, when you call getline, it gets the phrase from the user before it finds the end of that line.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    However, you probably don't need the fancier version shown. Instead, simply using cin.ignore() after every call to cin >> should work.
    The fancier version is necessary for naughty users. When you do cin >> cont, only one character will be read from the stream (since cont is of char type). But when a naughty user types more than one character, such as "yes sir", more than just the newline will be left unread ("es sir\n"). The fancy version says, remove all characters up to and including the newline (the stream cannot contain anything beyond that) but no more than as many characters as the stream could possibly contain. Instead of numeric_limits<stream_size>::max() you could use any large number (e.g 128) hoping that no user will be naughty enough to give such a long answer to a simple yes/no question.
    Last edited by anon; 04-17-2009 at 04:13 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM