C++ Arrays

Associate
Joined
6 Mar 2009
Posts
495
Hi, i'm starting to learn C++ and its really confusing me! I want to create a program that will prompt the user to enter 10 numbers and when they are entered there are stored inside an Array and then printed to screen.

I know it sounds quite easy but im struggling a little:(

Any suggestions please..
 
Have you written any code for it so far? It would also help to know what you've learned already. :)

A simple way to do this would be to create an array long enough to store the numbers you want to collect, an integer variable which can be used to keep track of how many have been entered so far, then create a for loop to go through and prompt the user for each number until you reach the total and then print them out.
 
Thanks for the response. This is what i have done so far:

#include <iostream>

using namespace std;

int main()
{
int nums[10];
int i=0;

cout << "Please enter a number";

for(i=0;i<=10;i++)
{
cout << nums;
}

return 0;

}
 
Thanks for the response. This is what i have done so far:

#include <iostream>

using namespace std;

int main()
{
int nums[10];
int i=0;

cout << "Please enter a number";

for(i=0;i<=10;i++)
{
cout << nums;
}

return 0;

}


Ok, awesome start.

Little aside: In standard C, you have to declare all of your variables at the top of your function (here i and nums). In C++ you dont, and one of the lovely little things you can do is declare your i variable in your for loop with the statement:

for(int i = 0; i < 10; ++i )
{ ...



Second, and more importantly, what you need is not a cout, but a cin in your for loop. Cout represents an output stream (i.e. outputs to the console in this case) and cin is an input stream (i.e. getting input data from the keyboard). Furthermore, you need a different stream operator for cin. As you're retrieving data from an input stream, instead of going "cout << ..", you want "cin >> ..".

Last thing, when you declare an array of 10 integers, the array itself is 0 indexed as you've noticed, so it only indexes between 0 and 9! Trying to access the element nums[10] should throw an exception and make your computer say bad things at you :D. Only access from 0 to (numberofelements - 1). This makes life easy as instead of <= you should just use < for your condition in your for loop.


So, your code should look something like,

#include <iostream>

using namespace std;

int main()
{
int nums[10];

cout << "Please enter a number";

for(int i=0;i<10;i++)
{
cin >> nums;
}

return 0;

}
 
Last edited:
Bah, Alex74 stole my thunder. :mad:

The only thing I can add is that you're going to need a second for loop to output the numbers afterwards. :)
 
Ah thanks a millions thats great help:)

Have another small query. When i create an new C++ file to run the above code when i hit F5 to run it says that the file cannot be found:( Any suggestions??
 
Bah, Alex74 stole my thunder. :mad:

The only thing I can add is that you're going to need a second for loop to output the numbers afterwards. :)

Hehe :p

Another thought, you might want to put your first cout inside your for loop so every time it goes around it asks your user for a number.
 
Ah thanks a millions thats great help:)

Have another small query. When i create an new C++ file to run the above code when i hit F5 to run it says that the file cannot be found:( Any suggestions??

You using VS? What does the build log say exactly?
 
Heys Guys im back again and need a little more help:)

I am having problems with methods as im getting errors when i go call them. I am looking to used different methods to find the smallest,largest,median etc of numbers that are entered by the user. Here is the code so far:

#include <iostream>
#include <conio.h>

using namespace std;

void totalNums(int total)
{
cout <<"The total amount of numbers entered is " << total;



}//totalNums()

int main()
{
int nums[10];
int total=0;

cout << "Please enter a number";

for(int i=0;i<10;i++)
{
cin >> nums;
}


for(int i=0;i<10;i++)
{
cout << nums;
cin.clear();
total++;
}
cout << endl;
totalNums();


getch();
return 0;

}

I am getting an error when i call the method totalNums();
 
You're not passing an argument to totalNums, despite declaring it as having an int parameter. To use it correctly, you would call it as follows:

Code:
totalNums(total);

The variable total declared in the function parameters is not the same as the variable total declared in the main function; it's important to understand the scope of variables in your programs. To avoid confusion, it might also be better to avoid giving things the same name in some cases (although things like i, n etc. for iterators and simple, one-use variables are something I tend not to bother about).
 
Back
Top Bottom