Hey, I'm studying c++ programming at university at the moment.
We've recently moved onto arrays - which I understand mostly.

I have no problem displaying information from an already set array, e.g.

int storage[5] = {1,2,3,4,5};

However, I'm having problems when adding my own data to an array within the program.

For example, I'd like my program to say:

"Enter a Number: "

And then when I enter a number, it is added to the array, and then the whole array is displayed showing all of the numbers added.

This is my code so far:

#include <iostream>

using namespace std;

int main()
{


int storage[15]; // Array to store up to 15 integers.
int x; // Variable x, which becomes 0 in the for loop.
int number;  // This will be the number the user enters.


// Do while loop repeats until I enter 1000.

// The program asks the user what number they would like to enter.
// The number then should be entered to the storage array, and displayed.

do {
    cout << "Enter number: ";
        cin >> number;
    cout << endl << endl;

for (x = 0; x<15; x++){
    storage[x] = number;
    cout << storage[x] << " " << endl;
    break;
}

} while (number != 1000);

return 0;

}

For some reason, it only displays the most recent number added, and not the whole array. I've been trying to figure this out for hours now, any help would be much appreciated!

Thank you.

Recommended Answers

All 13 Replies

for (x = 0; x<15; x++)
{    
     storage[x] = number;    cout << storage[x] << " " << endl;    
     [b]break;[/b]
}

the rest of your code looks pretty good i have to say.

for (x = 0; x<15; x++)
{    
     storage[x] = number;    cout << storage[x] << " " << endl;    
     [b]break;[/b]
}

the rest of your code looks pretty good i have to say.

If I remove the break, and enter (for example) '10', then the program displays 15 '10's and still doesn't add it to the array.

Because if I enter '12' the next time round, then 15 '12's are displayed.

Oh ok, I see what you are saying....

What I would do then, is to keep your prompt for user input inside of your do/while loop to populate your array...

Then take your 'for' loop out and place it after the do/while to display all the array contents.

#include <algorithm>
#include <iostream>

int main(int argc, const char** argv) {
	const int N = 15;
	int storage[N]; // Array to store up to 15 integers.
	int* end = &storage[N];
//   int x; // Variable x, which becomes 0 in the for loop.
   int number;  // This will be the number the user enters.

	// Do while loop repeats until I enter 1000.

	// The program asks the user what number they would like to enter.
	// The number then should be entered to the storage array, and displayed.

   do {
		std::cout << "Enter number:";
      std::cin >> number;
      std::cout << std::endl << std::endl;

		// fill the array
		std::fill(storage, end, number);
		
		// print to the konsole
		std::copy(&storage[0], end, std::ostream_iterator<int>(std::cout, " "));

	} while (number != 1000);

	return 0;
}

:D

???

what?
the code is written in the kindergarden;)

what?
the code is written in the kindergarden;)

Is there not an easier way to do that using the code I have? I know nothing about ostream_iterators at this moment in time.

All about STL you may find at: http://www.cplusplus.com/reference/
ostream_iterator - http://www.cplusplus.com/reference/std/iterator/ostream_iterator/
This is the code that resolves your problem:

#include <iostream>
int main() {
   const int N = 15;
   int array[N] = {0};
   int* ptr = &array[0];
   while ( true ) {
      std::cout << "Enter number(`9999` for end): ";
      std::cin >> *ptr;
      if ( *ptr == 9999 ) break;
      ptr++;
   }
   ptr = &array[0];
   while ( ptr != &array[N] ) {
      std::cout << *ptr++ << std::endl;
   }
   return 0;
}

But I would write it like this:

#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>

int main(int argc, const char** argv) {
	// this is array
	std::vector<int> array;
	// read from keyboard
	std::copy(
			std::istream_iterator<int>(std::cin),
			std::istream_iterator<int>(),
			std::back_inserter(array)
	);
	// print to konsole
	std::copy(array.begin(), array.end(), std::ostream_iterator<int>(std::cout, " "));

	return 0;
}

All about STL you may find at: http://www.cplusplus.com/reference/
ostream_iterator - http://www.cplusplus.com/reference/std/iterator/ostream_iterator/
This is the code that resolves your problem:

#include <iostream>
int main() {
   const int N = 15;
   int array[N] = {0};
   int* ptr = &array[0];
   while ( true ) {
      std::cout << "Enter number(`q` for end): ";
      std::cin >> *ptr;
      if ( *ptr == 'q' ) break;
      ptr++;
   }
   ptr = &array[0];
   while ( ptr != &array[N] ) {
      std::cout << *ptr++ << std::endl;
   }
   return 0;
}

But I would write it like this:

#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>

int main(int argc, const char** argv) {
	// this is array
	std::vector<int> array;
	// read from keyboard
	std::copy(
			std::istream_iterator<int>(std::cin),
			std::istream_iterator<int>(),
			std::back_inserter(array)
	);
	// print to konsole
	std::copy(array.begin(), array.end(), std::ostream_iterator<int>(std::cout, " "));

	return 0;
}

Thanks, but the coding in both doesn't work.

Neither display the array, and the first one crashes when you press "q."

#include <iostream>

using namespace std;

int main()
{


	int storage[15]; // Array to store up to 15 integers.
	int x=0, p=0; // Variable x, which becomes 0 in the for loop.
	int number;  // This will be the number the user enters.


	// Do while loop repeats until I enter 1000.

	// The program asks the user what number they would like to enter.
	// The number then should be entered to the storage array, and displayed.

	do {
		cout << "Enter number: ";
		cin >> storage[x];
		number = storage[x];
		x++;
		p = x; // equals the amount of numbers displayed
		cout << endl << endl;


	} while (number != 1000);
		for (x = 0; x<p; x++)
			cout << storage[x] << " " << endl;

	return 0;

}

This should work. :)

Edit: To make it even better, I took out the 'number' variable, and I made it so the '1000' wouldn't be displayed:

#include <iostream>
using namespace std;

int main()
{
	int storage[15]; // Array to store up to 15 integers.
	int x=-1, p=0; // Variable x, which becomes 0 in the for loop.


	// Do while loop repeats until I enter 1000.

	// The program asks the user what number they would like to enter.
	// The number then should be entered to the storage array, and displayed.

	do {
		x++;
		cout << "Enter number: ";
		cin >> storage[x];
		p = x; // equals the amount of numbers displayed
		cout << endl << endl;
	} while (storage[x] != 1000);

	for (x = 0; x<p; x++)
		cout << storage[x] << " " << endl;

	return 0;
}

Thanks, but the coding in both doesn't work.

Neither display the array, and the first one crashes when you press "q."

O sorry, I not test it ;)
I`ve fixed it.

#include <iostream>

using namespace std;

int main()
{


	int storage[15]; // Array to store up to 15 integers.
	int x=0, p=0; // Variable x, which becomes 0 in the for loop.
	int number;  // This will be the number the user enters.


	// Do while loop repeats until I enter 1000.

	// The program asks the user what number they would like to enter.
	// The number then should be entered to the storage array, and displayed.

	do {
		cout << "Enter number: ";
		cin >> storage[x];
		number = storage[x];
		x++;
		p = x; // equals the amount of numbers displayed
		cout << endl << endl;


	} while (number != 1000);
		for (x = 0; x<p; x++)
			cout << storage[x] << " " << endl;

	return 0;

}

This should work. :)

Edit: To make it even better, I took out the 'number' variable, and I made it so the '1000' wouldn't be displayed:

#include <iostream>
using namespace std;

int main()
{
	int storage[15]; // Array to store up to 15 integers.
	int x=-1, p=0; // Variable x, which becomes 0 in the for loop.


	// Do while loop repeats until I enter 1000.

	// The program asks the user what number they would like to enter.
	// The number then should be entered to the storage array, and displayed.

	do {
		x++;
		cout << "Enter number: ";
		cin >> storage[x];
		p = x; // equals the amount of numbers displayed
		cout << endl << endl;
	} while (storage[x] != 1000);

	for (x = 0; x<p; x++)
		cout << storage[x] << " " << endl;

	return 0;
}

This works perfectly! I've spent my whole day trying to figure this out, you've saved me from falling behind in my work.

Thank you!

No Problem. I am glad that you actually tried first instead of relying on someone to give you code w/o effort. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.