First Call To cin.getline() Inside For Loop Returns No String

Dear Coders:

I'm using Microsoft Visual Studio 2010 and am attempting to input a series of strings representing playing card descriptions in a poker simulation using the C++ <iostream> cin.getline() function inside of a for loop in main() (see line 30 in main(), below). The problem is that the first call to cin.getline() does not give me an input prompt and consequently returns no string (see the debugging statements in lines 31 to 37 in main() and the console output, below).

I'm perplexed over what is going wrong, as the exact same cin.getline() statement gets executed for each iteration of the loop, and the second and third function calls inside the "for" loop allows input and returns strings as expected, but the first function call inside the loop neither prompts for nor returns a string.

Does anyone have any idea what would be the cause of this logic error? I'm hoping that this is just a silly oversight on my part of something that I am doing wrong. Kind regards,

David


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// snippit.cpp - Simulate playing card drawing code snippit.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main()
{
	int playerDiscardCount = 0;
	char playerDiscardCard[ 18 ];

	// Input the number of cards to discard ( 0 to 3) and then input the cards to replace them ...
	cout << "\nHow many cards do you want to discard? ";
	cin >> playerDiscardCount;
	while ( playerDiscardCount < 0 || playerDiscardCount > 3 )
	{
		cout << "Oops! You may only discard between zero and three cards, please re-enter: ";
		cin >> playerDiscardCount;
	}

	for ( int i = 0; i < playerDiscardCount; i++ )
	{
		if ( playerDiscardCount == 1 )
			cout << "Please enter the card to discard: ";
		else
			cout << "Please enter the " << ( i == 0 ? "first" : ( i == 1 ? "second" : "third" ) ) 
				<< " card to discard: ";
		cin.getline( playerDiscardCard, CARDDESCLENGTH, '\n' );
		if ( playerDiscardCount == 1 )
			cout << "Length of the card description is " << strlen( playerDiscardCard ) << endl;
		else
			cout << "<<<---   D E B U G !   --->>> In main(), length of the " << ( i == 0 ? "first" : ( i == 1 ? "second" : "third" ) ) 
				<< " card description is " << strlen( playerDiscardCard ) << endl;
		cout << "<<<---   D E B U G !   --->>> In main(), replacing card \"" << playerDiscardCard << "\"." << endl;
		cout << "<<<---   D E B U G !   --->>> In main(), cardIsInPlayerHand() function return is " << deckOfCards.cardIsInPlayerHand( playerDiscardCard ) << endl;
	}
	// End player discard and draw ...

	return 0; // indicates successful termination
} // end main 



How many cards do you want to discard? 3
Please enter the first card to discard: 
<<<---   D E B U G !   --->>> In main(), length of the first card description is 0
<<<---   D E B U G !   --->>> In main(), replacing card "".
Please enter the second card to discard: Jack of Spades
<<<---   D E B U G !   --->>> In main(), length of the second card description is 14
<<<---   D E B U G !   --->>> In main(), replacing card "Jack of Spades".
Please enter the third card to discard: Queen of Clubs
<<<---   D E B U G !   --->>> In main(), length of the third card description is 14
<<<---   D E B U G !   --->>> In main(), replacing card "Queen of Clubs".

Press any key to continue . . .
Last edited on
All,

I found the solution to my problem by browsing over to the MSDN C++ Language Forum. As I suspected, it was a silly oversight on my part. To wit: line 15:

 
	cin >> playerDiscardCount;


and line 19:

 
	cin >> playerDiscardCount;


which are not followed with

 
	cin.get();


From the Forum, the first cin.getline() in my "for" loop gets skipped for the following reason:

"The reason is simple, when you enter an int value, you need to press enter('\n'), and this '\n' makes the first getline get skipped, so you need to add "cin.get()" to eliminate that '\n'."

when I added the statement:

 
	cin.get();


after lines 15 and 19, which gobbles up their newlines, all was well.

Sorry to bother you all. Cheers,

David

Topic archived. No new replies allowed.