Array Class(Infinite Loop Error)

Hi, I'm a student and have been working with c++ for 1 year now.(please go easy on me)
I am doing a homework assignment on array classes.

The task is:
Write MyArray.cpp using your class Array. Either copy/paste your code from part 1 or save the part 1 CPP as MyArray.cpp and edit it.

The app lets its user enter as many values as they like, and when that process is completed, lets the user look up values by matching index.

In a loop, the app should prompting the user to enter a pair of numbers on the same line: a whole number index and its corresponding whole- number value. Do not validate input in the app, because your template should handle out-of-range indexes, and it should allow overwriting an already-entered index. Quit the loop when an uppercase or lowercase Q is entered for either the index or the value. Indexes can be entered in any order -- they don't have to start with zero and go up by one thereafter. It's whatever the user enters.

Your app should keep track of which indexes got entered. Use a second Array for that, with whatever integer codes you wish to distinguish used indexes from those never used.

After all data entry is complete, the app should:

output how many (unique) indexes got entered,

output the list of all used indexes and their values, per the example below, and

implement an event-controlled loop that prompts for an index value and outputs whether the index is in use or not, and if in use, what is the

value stored for that index. Loop until the user elects to stop by entering uppercase or lowercase Q.

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
#include <iostream>
#include <string>
using namespace std;

#include <cassert>

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 = 999;
	for(int i = 0; i < 100; i++)
		values[i] = 0;
} // constructor

int Array::operator[](int index) const
{
	if(index < 0 || index >= 100)
		return 999;
	return values[index];
} // getter

int& Array::operator[](int index)
{
	if(index < 0 || index >= 100)
		return dummy;
	return values[index];
} // setter

int Array::capacity() const
{
	return 100;
} // capacity

int main()
{
	Array a;
	int index, value;

	while(index != 'q' || value != 'Q'){
		cout << "Input an index and a value[Q to quit]: ";
		cin >> index >> value;
		a[index] = value;
	} // while

	cout << a[index] << endl;

	cout << "You stored this many values: " << endl;
	cout << "The index-value pairs are:" << endl;
} // main 

This is what i have so far.

On the console, when i try to exit the loop my using q or Q, an infinite loop occurs repeating "Input an index and a value[Q to quit]: ".

I understand that i cannot pass a char into an int data type.
I've tried to do cin.clear()/atoi(index) and such.
However, I am not sure how to fix it.

Also, any ideas on how to continue the assignment is welcomed.
Thank you!

P.S. I am new to this forum so if I do anything wrong please correct me. Thank you!
EDIT:
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
#include <iostream>
using namespace std;

#include <cassert>
#include <cstdlib>

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 < 100; i++)
		values[i] = 0;
} // constructor

int Array::operator[](int index) const
{
	if(index < 0 || index >= 100)
		return 0;
	return values[index];
} // getter

int& Array::operator[](int index)
{
	if(index < 0 || index >= 100)
		return dummy;
	return values[index];
} // setter

int Array::capacity() const
{
	return 100;
} // capacity

int main()
{
	Array a;
	Array b;
	char buf[10];
	int input, val;
	int count = 0;

	while(true){
		cout << "Input an index and a value [Q to quit]: ";
		cin >> buf;
		if(buf[0] == 'q' || buf[0] == 'Q')
			break;

		cin >> val;
		input = atoi(buf);
		if(input >= 0 && input < 100){
			a[input] = val;
			b[input] = 1;
		}
	} // while

	for(int i = 0; i < 100; i++){
		if(b[i] == 1)
			count += 1;
	} // for
	cout << "\nYou stored this many values: " << count << endl;
	cout << "The index-value pairs are: \n\n";

	for(int i = 0; i < 100; i++){
		if(b[i] != 0)
			cout << i << " -> " << a[i] << endl;
	} // for

	cout << endl;

	while(true){
		cout << "Input an index for me to look up [Q to quit]: ";
		cin >> buf;
		if(buf[0] == 'q' || buf[0] == 'Q')
			break;
		input = atoi(buf);
		if(b[input] != 0){
			cout << "Found it!\n";
			cout << "The value stored at " << input << " is " << a[input] << "\n\n";
		} // if
		else
			cout << "I didn't find it. Try again.\n";
	} // while
} // main 


Finally solved it. Please do tell me if there is anything i need to improve on.
Thank you!
Last edited on
I'm not completely sure, I'm also a beginner, but I think one of your problems is that your while loop checks to see that index isn't q or Q, which will always be true because it can't not be both at the same time
You want to type while(index !='q' && index != 'Q')

the other problem might be with your getter setter functions but I didn't look thoroughly enough to see what you're trying to do
Thank for the reply. It was a mistake on my part. I forgot to change it back when editing.
I think the main problem is that i am trying to read a char into an integer type variable.

I have no idea how to do so.

Anyways, thanks for the help!
Topic archived. No new replies allowed.