Clearing cin

My program takes in 3 integers and finds the middle, then 3 floating point values and finds the middle, then 3 characters and finds the middle. The middle is found with the doMid function in a seperate header file. Everything works except if I input excess. For example, during the integer section, if I put 1 2 3 4, when doing the double section, 4 is the first input. How do I clear that before starting? Also any advice on how to make this more efficient is welcome, although I'm not very concerned about that.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include "Middle.h"

int main()
{
  int user_input1, a, b, c, i, j, k;
  double user_input2, d, e, f;
  char user_input3, x, y, z;
  
  cout << "Enter 3 integers: ";

  for(i = 1; i <= 3; i++)
  {
    while (!(cin >> user_input1))   // While we have an error reading...
     {
       cin.clear();  // Remove the error state in stream
       cin.ignore(); // Read but ignore the next character
     }
    
    if(i == 1)
    {
      a = user_input1;
    }
   
    if(i == 2)
    {
      b = user_input1;
    }
    
    if(i == 3)
    {
      c = user_input1;
    }
  }

  cout << "The middle value of " << a << ", " << b << ", and " << c << " is " << doMid(a,b,c) << endl << endl
    << "Enter 3 floating point values: ";

  for(j = 1; j <= 3; j++)
  {
    while (!(cin >> user_input2))
    {
      cin.clear();
      cin.ignore();
    }

    if(j == 1)
    {
      d = user_input2;
    }
   
    if(j == 2)
    {
      e = user_input2;
    }
    
    if(j == 3)
    {
      f = user_input2;
    }
  }

  cout << "The middle value of " << d << ", " << e << ", and " << f << " is " << doMid(d,e,f) << endl << endl
    << "Enter 3 characters: ";

  for(k = 1; k <= 3; k++)
  {
    while (!(cin >> user_input3))
    {
      cin.clear();
      cin.ignore();
    }

    if(k == 1)
    {
      x = user_input3;
    }
   
    if(k == 2)
    {
      y = user_input3;
    }
    
    if(k == 3)
    {
      z = user_input3;
    }
  }

  cout << "The middle value of " << x << ", " << y << ", and " << z << " is " << doMid(x,y,z) << endl;
}
Last edited on
Whenever you want to clear cin call these two methods:
1
2
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');


Don't forget at the top of the code to #include <limits>

As for efficiency... this probably isn't more efficient, but I think it is a little easier on the eyes. There's nice symmetry:
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
#include <iostream>
#include <limits>

//I use this so much, might as well turn it into its
//own function so I don't have to type as much later!
void clearCin()
{
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

int main()
{
    int integerInputs[3];
    double doubleInputs[3];
    char charInputs[3];

    for(int i = 0; i < 3; i++)
    {
        while( (std::cout << "Enter integer " << i+1 << " of 3: ") &&
               !(std::cin >> integerInputs[i]))
        {
            std::cout << "No, no... an INTEGER!" << std::endl;
            clearCin(); //clear cin in the error case
        }
        clearCin(); //clear cin after we've received a satisfactory input
    }

    //I don't have any of these 'doMid' functions...
    //int mid = doMid(integerInputs[0], integerInputs[1], integerInputs[2]);

    for(int i = 0; i < 3; i++)
    {
        while( (std::cout << "Enter a floating point value: " << i+1 << " of 3: ") &&
               !(std::cin >> doubleInputs[i]))
        {
            std::cout << "No, no... a FLOATING POINT VALUE!" << std::endl;
            clearCin();
        }
        clearCin();
    }

    //double mid = doMid(doubleInputs[0], doubleInputs[1], doubleInputs[2]);

    for(int i = 0; i < 3; i++)
    {
        while( (std::cout << "Enter a character: " << i+1 << " of 3: ") &&
               !(std::cin >> charInputs[i]))
        {
            std::cout << "No, no... a CHARACTER!" << std::endl;  //ha, but aren't they all characters in the end?
            clearCin();
        }
        clearCin();
    }

    //double mid = doMid(charInputs[0], charInputs[1], charInputs[2]);

    return 0;
}
Last edited on
I get errors in that code despite having #include<limits>
Got it. Thanks.
Topic archived. No new replies allowed.