Question
Why and I getting a infinate loop with a user uses test input such as 133 x My Code works for all test cases
Why and I getting a infinate loop with a user uses test input such as " 133 x"
My Code works for all test cases below, but if you test it with input and use a non numerical value as the last input it goes into a endless loop. Please help!!
I believe my error is with this part of the loop:
while(true){
cout
cin >> buf;
if(buf[0] == 'q' || buf[0] == 'Q')
break;
cin >> val;
input = atoi(buf);
if(input >= 0 && input
a[input] = val;
b[input] = 1;
}
} // While Close
Here is my code:
#include
using namespace std;
#include
#include
#include
class Array
{
private:
int values[100];
int dummy;
public:
Array();
int operator[](int) const;
int& operator[](int);
int capacity() const;
}; // Array class
Array::Array()
{
dummy = 0;
for(int i = 0; i
values[i] = 0;
} // constructor
int Array::operator[](int index) const
{
if(index = 100)
return 0;
return values[index];
} // getter
int& Array::operator[](int index)
{
if(index = 100)
return dummy;
return values[index];
} // setter
int Array::capacity() const
{
return 100;
} // capacity
int main()
{
//Delclarations
Array a;
Array b;
int input, val;
int count = 0;
char buf[10];
// Read user input for index and value
while(true){
cout
cin >> buf;
if(buf[0] == 'q' || buf[0] == 'Q')
break;
cin >> val;
input = atoi(buf);
if(input >= 0 && input
a[input] = val;
b[input] = 1;
}
} // While Close
for(int i = 0; i
if(b[i] == 1)
count += 1;
} // For Close
// Output
cout
cout
cout
for(int i = 0; i
if(b[i] != 0)
cout "
} // For Close
cout
// Read user input for look up values
while(true){
cout
cin >> buf;
if(buf[0] == 'q' || buf[0] == 'Q')
break;
input = atoi(buf);
if(b[input] != 0){
cout
cout
} // if
else
cout
} // While Close
} // Close Main
Here's a sample of how this should work (user input in blue) Input an index and a value [Q to quit]: 33 12 Input an index and a value [Q to quit]: 4 100 Input an index and a value [Q to quit]: 5 300 Input an index and a value [Q to quit]: x 17 Input an index and a value [Q to quit]: 33 120 Input an index and a value [Q to quit]: -1 23.4 Input an index and a value [Q to quit]: 2000 -999 Input an index and a value [Q to quit]: q You stored this many values: 4 The index-value pairs are: 0 => 17 4 => 100 5 => 300 33 => 120 Input an index for me to look up [Q to quit]: 33 Found it -- the value stored at 33 is 120 Input an index for me to look up [Q to quit]: 38 I didn't find it Input an index for me to look up [Q to quit]: 0 Found it -- the value stored at0 is 1.7 Input an index for me to look up [Q to quit]: -100 I didn't find it Input an index for me to look up [Q to quit]: 1000 I didn't find it Input an index for me to look up [Q to quit]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started