Problem with do/while loop or cin.getline

closed account (SNUXoG1T)
Hello,

The program below takes a person's full name as input in Last, First, Middle format and outputs the same data in First, Middle, Last format. The purpose of the program is to play with dynamic arrays and string functions. The program runs in a do/while loop so that the exercise can be repeated.

My problem is that when the code is run for the second time, from the loop, the first user input is skipped. I've pasted the error condition below.

This code compiles, links, and runs in Visual C++ Express 2008 on Vista32 (with the error/problem below of course).

Any thoughts? Thanks in advance,

Rob

----------Error Condition---------------

Please enter your last name: Thompson
Please enter your first name: Kenneth
Please enter your middle name: Lane
Your Name is: Kenneth Lane Thompson
Enter in 1 to quit.
Enter in any other number to run again.
> 0
Please enter your last name: Please enter your first name: Dennis
Please enter your middle name: MacAlistair
Your Name is: Dennis MacAlistair
Enter in 1 to quit.
Enter in any other number to run again.
> 0
Please enter your last name: Please enter your first name:

----------Code---------------
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream> // for cin, cout
#include <cstring> // for string manipulation
using namespace std;

void DisplayName(char *Name);

void main ()
{
//Declorations
const int UserInputLength = 80; //Set Name Length (one screen width)
char LastName[UserInputLength]; //Array for last name
char FirstName[UserInputLength]; //Array for first name
char MiddleName[UserInputLength]; //Array for middle name
int FNLength, MNLength, LNLength, NameLength; //Name properties
int LoopControl=1; //Loop control variable to terminat loop

//Run loop at least one time with do/while
do
{
cout << "Please enter your last name: ";
cin.getline(LastName, UserInputLength, '\n');
cout << "Please enter your first name: ";
cin.getline(FirstName, UserInputLength, '\n');
cout << "Please enter your middle name: ";
cin.getline(MiddleName, UserInputLength, '\n');

//Determin length of user data in arrays
FNLength = strlen(FirstName);
MNLength = strlen(MiddleName);
LNLength = strlen(LastName);

//Sum name lengths plus space for spaces and null
NameLength = FNLength + MNLength + LNLength + 3;

//Create dynamic array large enough to hold all user data plus spaces and null
char *WholeName = new char[NameLength];

//Determine if enough memory is available
if (WholeName == 0)
{
cout << "DMA not allocated!" << endl; //Display error message
exit(1); //Terminate program
}

strcpy(WholeName, FirstName); //Copy FirstName to WholeName
strcat(WholeName, " "); //Add Space to WholeName
strcat(WholeName, MiddleName); //Add MiddleName to WholeName
strcat(WholeName, " "); //Add Space to WholeName
strcat(WholeName, LastName); //Add LastName and NULL to WholeName

DisplayName(WholeName); //Run DisplayName function to display WholeName array

delete [] WholeName; //Free up memory

//Prompt user to repeat program or exit
cout << endl;
cout << "Enter in 1 to quit." << endl;
cout << "Enter in any other number to run again." << endl;
cout << "> ";
cin >> LoopControl;
}
//If the user enters 1 the program exits, otherwise the program repeats
while ( LoopControl != 1 );
}

void DisplayName(char *Name)
{
//Display text with array contents
cout << "Your Name is: " << Name << endl;
}
Last edited on
I'm not sure why this happens but a cin.ingore(1, '\n'); after cin>>LoopControl; solves it.
I guess cin>> doesn't remove the '\n' form the stream... or something like that
closed account (SNUXoG1T)
Hello,

So I contacted my instructor and he provided the cin.ignore syntax:

cin.ignore(cin.rdbuf()->in_avail(),'\n');

We haven't covered classes yet and only just briefly touched on the string classes for this lab. I haven't been able to find much out about the cin.ignore command, what it does, or why I only need it after the first cin.getline and none of the others.

Anyway, here's the working snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
      do
      {
            //Prompt user for last name input
            cout << "Please enter your last name: ";
            //Get input from user.
            //Read pre-defined length (same as array length) until return character is encountered
            cin.ignore(cin.rdbuf()->in_avail(),'\n');
            cin.getline(LastName, UserInputLength, '\n');
            //Prompt user for first name input
            cout << "Please enter your first name: ";
            //Get input from user.
            //Read pre-defined length (same as array length) until return character is encountered
            cin.getline(FirstName, UserInputLength, '\n');
            //Prompt user for middle name input
            cout << "Please enter your middle name: ";
            //Get input from user.
            //Read pre-defined length (same as array length) until return character is encountered
            cin.getline(MiddleName, UserInputLength, '\n');


Thanks!

Rob
Last edited on
Topic archived. No new replies allowed.