i have to create a program in which a user is asked for:
How many values (s)he wants to enter (maximum 50);
Asks for the values and stores them into an array of double.
Sorts the values in descending order.
Prints the sorted array to the console.

this is what ive got so far:

#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{
	int quantity;

	
	cout << "Enter the amount of values you want to sort (max 50): ";
	cin >> quantity;
	do
	{
		cout << "That is too many values. Please enter a max of 50!: ";
		cin >> quantity;
	}
	
	while (quantity >50);
		
	{
		cout << "Enter " << quantity << " values: ";
		double values[quantity];
		cin >> values[quantity];
		cout << values;
	}
	
	
	return 0;
}

i know its not finished yet. so far i have done the initial input and now am trying to get the array to work.

My problem is the array 'values'. Every time i run this program i can enter the amount of values normally but when i input the values it just gives me out a load of jibberish. i.e 0x7fff5fbff700 or something similar. could someone tell me why this happens and how to fix it.

thanks

Firstly, you have you have this line wrong:

while (quantity >50);

Surely it should be '<' instead?

Reply, but paste in:
Any program errors.
Your inputs.
Expected outputs.
Actual outputs.

that bit is for when you enter a value greater than 50 and you should enter 50 or less.

there are no program errors.
Here is a sample input/output scenario.
amount of values: 3
input 345
expect to get 345 back
actually get 0x7fff5fbff700 back.

Firstly, you have you have this line wrong:

while (quantity >50);

Surely it should be '<' instead?
...

No, that loop is an input validation loop so the OP has the condition correct. The problem is they used a do-while loop, which isn't the correct type of loop. Since it's a do loop, it is guaranteed to execute at least once telling the user that their input is invalid even if it, in fact, isn't.

>>there are no program errors
Actually, there are no compiler errors, be mindful of your usage of vocabulary/lingo. Your funky results mean your syntax is correct and thus compilable and runnable. The problem though is that your logic is not correct. Thus, by extension, this also means that there are several program errors.

>>My problem is the array 'values'. Every time i run this program i can enter the amount of values normally but when i input the values it just gives me out a load of jibberish. i.e 0x7fff5fbff700 or something similar. could someone tell me why this happens and how to fix it.
That's not your only problem. Honestly, I'm rather amazed this doesn't crash with segmentation faults.

Your loop:
I have already addressed this above. Your condition is correct, but your loop structure itself is not. A do loop is not the appropriate loop type for that particular use. You should re-format it to a standard while loop instead.

Your array:
An array is essentially a pointer to the first of several contiguous blocks of memory. When you do something like this:

double myArray[arraySize] = {0};
cout << myArray;

You are actually outputting the memory address stored in the pointer, not the data stored in the array elements. As a result, your output will be the "jibberish" that you're getting and not zero (0).
I'm assuming this assignment is part of a chapter that is an introduction to arrays. I think you need to take a closer look at your reference materials.

If you want to work with this array properly, you should add 2 new loops, one for input, and one for output. Then, inside your loops, make sure you keep track of your indexes properly so you don't wind up with an infinite loop or overruns and seg faults.

Edited 6 Years Ago by Fbody: n/a

Oh OK sorry I misunderstood that bit (brain's-built-in-compiler-error-passed-as-silent).

Line 23:

double values[quantity];

is not permitted in standard C++ unless quantity is known at compile time. Your compiler may permit it as an extension; but if you want to rely on that extension, you should be aware that what you're writing is not really a C++ program.

Line 23:

double values[quantity];

is not permitted in standard C++ unless quantity is known at compile time. Your compiler may permit it as an extension; but if you want to rely on that extension, you should be aware that what you're writing is not really a C++ program.

I don't have access to the actual Standard or the Draft for 0x Standard, so I can't confirm, but according to others here that is acceptable under the forthcoming 0x Standard.

Under the old Standard(s) though, you are correct, it isn't technically legal.

Edited 6 Years Ago by Fbody: n/a

I don't have access to the actual Standard or the Draft for 0x Standard, but according to others here, that is acceptable under the forthcoming 0x Standard.

Under the old Standard(s) though, you are correct, it isn't technically legal.

I can't find it in my copy of the draft for the new standard either. Unless I'm missing something, that draft says that the number of elements in an array must be a constant expression.

This article has been dead for over six months. Start a new discussion instead.