getline problem

Hi,

I'm trying to write a loop that lets the user type in a string, and then process that. Previously I've had some problems with strings getting messed up when they are 'reused', that is, when I change it multiple times it has been acting a bit weird (which of course might be just because I'm not a very skilled programmer).

What I have so far is a prompt asking if you want to continue looping or not (enter 1 means 'Yes! Continue this fantastic loop!' and 0 means 'No, please stop this nonsense!'). This part works.
Then after this 'quit-or-continue' prompt, there's yet another, identical prompt that asks if you want to change the string I was talking about earlier.

Now, the problem is that when the code gets to the 'getline(-----)' (line 31) part, it simply passes it without letting me input anything. It's like it's rushing through it all.

I've included my code below here, and then after that an example of how it looks when running.

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
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string.h>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{

std::string input1;
std::string input2;

int choice1;
int close=1;

while(close!=0)
{
// INTEGER PROMPT:
cout << "\nEnter 'choice1' (1 or 0): ";
cin >> choice1;

// INPUT:
if(choice1==1)
{
input1.clear();
cout << "\n--- INPUT --- \n String 'input1' reset! Check if empty --> " << input1 << " <--" << endl;
cout << "Enter 'input1': ";
getline(cin,input1);
cout << "Testing new input: ";
// Converting string to char (not important for now)
char * writable = new char[input1.size() + 1];
std::copy(input1.begin(), input1.end(), writable);
writable[input1.size()] = '\0';
for(int i=0;i<input1.size();i++)
	cout << writable[i];
}

// FINAL INTEGER PROMPT (CONTINUE):
cout << "Continue? (YES:1, NO:0): ";
cin >> close;
}
return 0;
}


1
2
3
4
5
6
7
8
9
10
11
Enter 'choice1' (1 or 0): 1
 String 'input1' reset! Check if empty -->  <--
Enter 'input1': Testing new input: Continue? (YES:1, NO:0): 1

Enter 'choice1' (1 or 0): 1

--- INPUT --- 
 String 'input1' reset! Check if empty -->  <--
Enter 'input1': Testing new input: Continue? (YES:1, NO:0): 1

Enter 'choice1' (1 or 0): 0
Continue? (YES:1, NO:0): 0


So my question is: is there a way to make it slow down a bit so that I can actually input anything? Or is there a better way to do this?

Thanks!
Lines 23/43 are your issue. When you use cin to get an integer like that, it leaves junk in the input buffer (namely, a '\n' character).

You'll need to remove it:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Last edited on
Ah! Thanks! Works now! :)
I'm working on a similar program and ran into this issue as well. Mine is a simplistic "grading" program where the user specifies how many students are to be entered (the outer loop) followed by how many tests each student took (the inner loop). Basic math calc's are then applied (sum, avg, etc)

The outer loop (based on the entered number of students) runs and prompts for the student name. This is where I ran into the same issue outlined above. I was using cin >> to obtain the number of students and getline () for the student name.

The code that firedraco provided resolved my issue as well (thank you!) I'm just curious if there is a better way to obtain the values for the number of students / student name that doesn't require.. purging the input buffer?
If it makes a difference, studentName is declared as a string, the rest are all Int's.

Thanks in advance; nice to have found a community were people actually offer constructive suggestions!


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
  

 cout << " How many student will be processed? ";
   cin >> numberStudents;
   cout << endl;
     
  for (;numberStudents>0;numberStudents--)
      {
      
      studentCount=(studentCount + 1); 
                          
   // prompt user to enter a students first name
      cout << " ** Please enter the students name ** --> ";
      
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      
      getline (cin,studentName);  
      
      cout << endl; 
      
  // prompt user to enter the number of tests student took
     cout << " How many test did " << studentName << " take? (Max of 99) ";
     cin >> numberTests;
     totalTests = numberTests; //variable to hold student test count
     allTests = allTests + numberTests; //variable to hold total test count
     cout << endl;
   
          for (;numberTests>0;numberTests--) //loop to input student scores
          
          {
             
           currentTest = currentTest + 1; //track which test we are storing
           cout << " Please enter test score #" << currentTest << " ";
           cin >> testScore; //input current test score
...

Last edited on
Well, a better way would be to get all of your input using getline(), then use a stringstream to parse out the parts you want.

It requires more variables and boilerplate though, so I'd probably stick to just using .ignore if you just want to read one integer and don't care about input validation.
Topic archived. No new replies allowed.