Endless loop when incorrect value

Hi! I am creating a program that uses arrays. I'm struggling in one area when a user puts an incorrect input (like a letter), I don't know where the code should be placed, or what it is. Currently, when I place an incorrect value, it is stuck in an endless loop.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include<iostream>
#include<stdlib.h>
using namespace std;
int len=10;
void displayNumber(int arr[])
{
  cout<<endl<<"Array is : ";
  for(int i=0;i<len;i++)
{
  cout<<arr[i]<<" ";
}
}
  void displayRevNumber(int arr[])
{
  cout<<endl<<"Reverse Array is : ";
  for(int i=len-1;i>=0;i--)
{
  cout<<arr[i]<<" ";
}
}
  void displayValAtPos(int arr[])
{
  int n;
  cout<<endl<<"Enter position: ";
  cin>>n;
  cout<<"Value is : ";
  for(int i=0;i<len;i++)
{
  if(i+1==n)
  cout<<arr[i]<<" ";
}
}
  void InsertVal(int arr[])
{
  int val,pos;
  cout<<"Enter position: ";
  cin>>pos;
  cout<<endl<<"Enter value: ";
  cin>>val;
  cout<<"Insert value: ";
  for(int i=0;i<len;i++)
{
  if(i+1==pos)
  arr[i]=val;
}
}
  void searchVal(int arr[])
{
  int val,pos;
  cout<<endl<<"Enter value: ";
  cin>>val;
  for(int i=0;i<len;i++)
{
  if(arr[i]==val)
{
  cout<<endl<<"value found:";
}
}
}
  int main()
{
  int choice;
  int arr[10];
  cout<<"Enter 10 values:\n";
  for(int i=0;i<10;i++)
{
  cin>>arr[i];
}
  while(1)
{
if (cin.fail())

  cout<<endl;
  cout<<"1. Display Number\n";
  cout<<"2. Display Reverse Number\n";
  cout<<"3. Display at position:\n";
  cout<<"4. Search Number\n";
  cout<<"5. Insert Number\n";
  cout<<"6. Quit\n";
  cout << "Enter choice:";
  cin >> choice;

  switch (choice)
{
case 1:
displayNumber(arr);
break;
case 2:
displayRevNumber(arr);
break;
case 3:
displayValAtPos(arr);
break;
case 4:
searchVal(arr);
break;
case 5:
InsertVal(arr);
break;
case 6:
exit(1);
break;
default:
cout<<"Invalid choice";

}

}

return 0;
}
Hello skydawgg,

The if statement at line 71 is missing its opening and closing {}s.

Line 101 will exit the program. the "1" denotes that there was a problem f some kind. Since this case statement is in main a "return 0;" will work fine. Also line 102 will never be reached because of the exit or return.

Line 71. You check if "cin" has failed, but you check is for only one of the state bits and this may not always get set. At least it is best not to count on this. A better choice is if (!cin).

What you could do is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 0; i < 10; i++)
{
	cin >> arr[i];

	while (!cin)
	{
		std::cout << "An error message" << std::endl;

		std::cin.clear(); // <--- Resets the state bits.
		std::cin.ignore(30000, '\n');  // <--- Clears the input buffer.

		std::cin >> arr[i];
	}
}


This will eliminate the need for line 71.

The same code could also follow line 81.

I will need to load up the code to see if i can see anything else.

Hope that helps,

Andy
Topic archived. No new replies allowed.