Help *urgent

I'm supposed to write a program that prompts the user to enter as many values as they want between 1-100. Using an array, I believe I'm supposed to display the value of those numbers and when the user enters -99 the program is supposed to stop. This is what I have so far,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  int main() {
	int x; 
	int result = 0;

	const int size = 100;
	int y[size] = {};

	
		cout << "Enter values between 0-100 and enter -99 to stop" << endl;
		cin >> x;


	
		for (int i = 0; i <= 100; i++) {
			result += y[i];
		}
			
}


-line 9 and 10 should be inside a loop.
-for each value of X entered check with an if statement if X is -99,
- if so, exit the loop,
- if not, check again if the value is between 1 and 100,
- if so, store it in array y

- use loop on line 14 to print values stored in array y until the value -99
was enterd
I'm just learning about arrays, can you elaborate how I would store it into y if x >0 && x<100? is it simply cin>>y[size] ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main() {
	int x;
	int result = 0;

	const int size = 100;
	char y[size] = {};

	
		cout << "Enter values between 0-100 and enter -99 to stop" << endl;
		for (int x = 0;x < 100;x++) {
			cin >> y[size];
			if (x == -99) {
				break;
			}
		}
		 
		for (int i = 0; i <= size; i++) {
			result += y[i];
		}
			
}


how do I go about do I go about line 14?
Last edited on
sorry for late reply, also you realize i can't just give you the correct answer in that case i will not be helping you learn, i'll give you some references
where you can dig out and learn.

an array is a contiguous section of memory, so if i have an array
of strings like this.

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
string array[6];
///each of the array[0], array[1],array[2],array[3],array[4] till array[5]
///are like individual variables.

///to read data into my array,  i'll simply put it in a loop like you have done 
//on line 11 but i'll use the variable " x" as my index.

///in your case you are only reading data into one variable 
cin>>y[size] ///wrong , y[size] doesn't even exist, it is one past the end of
/// your array. use "x" instead of "size".


///for using conditional statements, i see you have an idea.
/// before you read a value into your array it must be within 0 -100.
///that can be done with a simple if statement that checks if a value
/// is within those bounds you should read the value into a temporal
///variable and then check it.
if(value greater that 0 and value lesser than 100)
    {
        if(value is -99) exit loop
        else  {store value into the current index.} 
    }

//your line 18 is wrong. you should print the elements until you find
//-99 not add them 


read about arrays.
https://bytes.com/topic/c/answers/616887-filling-arrays-input
https://stackoverflow.com/questions/32290948/user-input-into-an-array
http://www.cplusplus.com/doc/tutorial/arrays/

read about conditions
http://www.learncpp.com/cpp-tutorial/36-logical-operators/
http://en.cppreference.com/w/cpp/language/operator_logical
https://cal-linux.com/tutorials/conditionals.html
https://stackoverflow.com/questions/6241672/multiple-if-statements-in-c

printing elements of an array
http://www.cplusplus.com/forum/beginner/22835/
http://www.dreamincode.net/forums/topic/227165-how-do-i-print-the-contents-of-an-array/
Last edited on
I don't understand how to do line 18. Can you guide me?

this is my code and it's not displaying anything at all. What am I doing wrong?
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
int main() {
	int x;
	int result = 0;

	const int size = 100;
	int y[size] = {};

	while (true){
	cout << "Enter values between 0-100 and enter -99 to stop" << endl;
	cin >> x; 

	if (x > 0 && x < 100) {
		if (x == -99) {
			break;
		}
	}
	else
		cin >> y[x];
	}

	for (int i = 0; i <=size; i++) {
		y[i] = x;
		cout << y[x];
	}
	return 0; 
}
Hello hh98265,

Yolanda has given you some good links. In addition to that if you would do your program like this:
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
int main()
{
	int x;
	int result = 0;

	const int size = 100;
	int y[size] = {};

	while (true)
	{
		cout << "Enter values between 0-100 and enter -99 to stop" << endl;
		cin >> x;

		if (x > 0 && x < 100)
		{
			if (x == -99) // <--- if x == -99 this line will never be reached because of the first if.
			{  // <--- Not necessary for one line.
				break;
			}  // <--- Not necessary for one line.
		}
		else
			cin >> y[x];  // <--- You already have a value for "x" you need to use it not read from the keyboard again.
	}

	for (int i = 0; i <= size; i++)
	{
		y[i] = x;  // <--- This line does not do what you want and is not needed.
		cout << y[x];  // <--- This line OK.
	}

	return 0;
}

It is much easier to find your problems. As you can see if "x" = -99 line 14 will fail and go to line 21/22 which is not what you want to do at this point. Since line 14 fails line 16 will never be reached.

Line 27 in the for loop will overwrite everything in the array with the last value of "x" before you print it to the screen.

I noticed that you will need a variable to keep track of how many entries are in the array before -99 is entered. You will need this in the for loop to only print what is entered int the array not the whole array.

One last note const int size = 100; is better written as const int SIZE = 100;. The capital letters will let you know that it was defined as a "const".

Hope that helps,

Andy
Sorry to be such a bother, but I fixed most of the errors in the program, but now the problem that I have is that after I input (for example) 5, 10 , 15, and -99. It will display the values and break out of the loop as intended, but it is also displaying a ton of zeros. How do I fix this and what did I do wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
        const int SIZE = 100;
	int y[SIZE] = {};

	for (int i = 0; i < 100;++i){
		cout << "Enter a value between 1-100 (Press -99 to stop)" << endl;
		cin >> y[i];

		if (y[i] == -99)
			break;
	}

	for (int i = 0; i < 100; ++i)
		cout << y[i] << endl;

	return 0;
}
Last edited on
In the for loop at line 13-14 the code outputs all 100 elements of the array. If you only want to output the actual numbers you've entered, you need to somehow keep track of how many numbers were input before -99 was entered.

-99 probably shouldn't get added into the array in the initial input.
Registered users can post here. Sign in or register to post.