I'm trying to assign a value to an array. To be specific I'm trying to prompt the user to put in multiple numbers, which I want to assign to their respective parts in an array.
For example:
cout<<"How many peoples age would you like to enter?";
cin >> ages
cout << "Please enter " << ages <<" ages";
Now I want that many ages to be entered into their respective places in the array.
I've tried about 10 different ways of doing it.
Usually trying to declare the array first.
etc.
If you need me to post an example of the code I will, but if you're able to explain it or it's syntax Ithink it'll be easier. I don't know why but I feel as if this isn't a really complicated question, more that i'm over looking something simple.
Whenever the size of the array is unknown as compile time, you need to use dynamically allocated arrays -- arrays that are created in the heap, aka free store.
Here's an example:
1 2 3
//The value of n can be anything, such as user input, that is >0. It may or may
//not be known at compile time. Also, it can only be an integral value.
int *v=newint[n];
Is this your problem, or was it related to filling the array?
actually to be completely honest, I'm having trouble with both. I didn't realize they were two questions until now.
Alright, so in your example *v is the name of the array?
And what is new? I ask because it's in blue which I assume it's significant, and i'm unaware of it.
To be exact, arrays don't have names. Arrays are just memory set aside by you.
What does have a name, however, is the pointer pointing to one. In this case, 'v' is the name of the pointer.
You're still pretty green, so I won't confuse you with the specifics of pointer syntax. Just know the syntax I gave you to create a dynamic array.
You will access this array the same way you would access a regular (static) array: with the offset operator ([]).
new is a C++ keyword. It is an operator for sole purpose of dynamic memory allocation. The syntax above allocates space for n elements of type int and returns a pointer to the start of the array.
I would like to see what you've tried so war to fill the array.
I'll start off with the particular function I'm working on, since I really don't have much else besides this completed :/ .
int scores()
{
int Array[25];
That's my latest attempt at trying different things. As you can see it's pretty barebones since I'm just trying to get the array to work correctly. 25 is an arbitrary number I just inserted to get the creative process going.
I've also tried establishing the Array at zero, and another variable at zero and tried to get them to work together.
For example I tried one like:
void Array (int x[]);
void printArray(const int x[]);
cout << "Enter all numbers.\n"<< endl;
Array(scores);
printArray(scores);
cout << endl;
Also I tried
setting Array and scores to 0.
So I've tried a few different things.
Please excuse my "greeness" all a learning process.
Your implementation of scores is wrong because you are trying to set a value at the 26th value of your array when it has only 25 elements.
If you want to use dinamic allocation as helios suggested, read this tutorial: http://www.cplusplus.com/doc/tutorial/dynamic.html
#include <iostream>
int main()
{
int ages;
std::cout << "Enter how big you want the array of ages to be: ";
std::cin >> ages;
int *myArray = newint[ages];
for(int i = 0; i < ages; i++)
{
std::cout << "Enter age for element " << i << ": ";
std::cin >> myArray[i];
}
for(int i = 0; i < ages; i++)
{
std::cout << "Age at element " << i << " in the array: " << myArray[i] << std::endl;
}
return 0;
}
Alright I tried using your code mythios to see if I could get it to work and then tweak it to fit into my program exactly, but right off the bat I'm already getting numerous errors that don't make any sense to me at all.
code:
int main ()
{ //Open main
int ages;
cout << "Enter how big you want the array of ages to be: ";
cin >> ages;
int *myArray = new int[ages];
for(int i = 0; i < ages; i++)
{
cout << "Enter age for element " << i << ": ";
cin >> myArray[i];
}
for(int i = 0; i < ages; i++)
{
cout << "Age at element " << i << " in the array: " << myArray[i] << endl;
}
return 0;
}
I got to many errors to post here and some of them I don't even understand:
1>c:\program files\microsoft visual studio 8\vc\include\new(49) : error C3646: '_THROW0' : unknown override specifier ?
what I'm going to try and do now is take your example and read that tutorial and see what I come up with.
I think there's something wrong with my compiler.
I took a look at that tutorial and built another array and whatnot, and still got over 100 errors, all seeming to be the same as the aforementioned ones. Then for kicks I ran the example program in the tutorial and I once again got over 100 errors.
so can any one verify that the program in the tutorial is running correctly?
#include <iostream>
#include <new>
using namespace std;
int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}
EDIT=
Yeah there's something up with my compiler it's not even running programs that I know are correct. Any body know why I'd be getting this?
Like if v1[0] was say 2
and
v2[0] was say 3
then they would add together to make
v3[0] = 5
I'm trying to figure out how to get the entire array to do that.
However the both arrays are located in different functions.
So I tried:
int clapoint(Lscores, Xscores)
{
int * Point;
Point= new int [no];
int sum = 0;
for (int i=0; i<100; i++)
{
sumofscores (Lscores, Xscores)<< endl;
cout << sumofscores << endl;
}
return 0;
}
I tried calling the functions but I don't believe I'm doing it correctly.
Once again forgive my "greeness" it's all still relatively new to me.
Uh... That doesn't seem right.
First of all, why are you passing pointers to pointers to arrays?
Second, unless you want to overwrite one of the arrays, you need to create a third one, which you have done. I was about to say something else because I misread your code, but now that I read it right, I see something even worse.
Never, ever, EVER, do this: a+b=c
The assignment operator doesn't work like a the equality operator, or like the equals in algebra. The result of the right operand is always assign to the left operand. NEVER viceversa.
Ah, sorry it's late
I know the difference between = and == but I overlooked it. and that it shouldn't be a+b=c but the c = b + a, I overlooked it.
Alright but how would I go about combining the two arrays into a third array?
I understand the concept of using a for loop to add each one individually but going about it isn't exactly working the way I'd want.
and am I calling the arrays correctly because I can't get the name of the function to work in main() which leads me to believe that there's something wrong with the way I'm calling them.
Edit, actually it appears that once reversed to "c=a+b" the code doesn't spit out errors there. Now the problem lies in trying to get the function into main to execute.
Seems simple enough, and I feel like I'm overlooking something simple again.
You're on the right track.
If main() can't find the function, it may be because you're defining it after main() without declaring it before main().
Example: