someone please help me so I want the user to be able to enter as many numbers they wish. so this is currently giving me error. how can I get the user to enter the numbers in the array?
Still new to coding myself but I can help you with your issue.
My advice to you is to read your book that was given by your instructor. It really helps to read the book after a lecture. One night you should just focus mainly on the chapter explaining memory allocation to give you a better understanding of it.
Here is the code you need but I am not sure how you wanted it done. I made it into functions because it made it easier for me instead of writing it all inside in main(). You can easily convert the code into in main() if you need to.
Code is working for me but if it doesn't work for you. please post the issue.
//read the comments
#include <iostream>
usingnamespace std;
void displayValue(int[], int);
void setValue(int[], int);
int main()
{
int size; // I just created two simple variable's because that is all I will be using for my functions.
int* value; // This one will be the variable creating the memory allocation.
cout << "How big do you want the array to be? ";
cin >> size; //I gave this variable a value. Which will give my array its size and how many times I will be looping inside my function.
value = newint [size]; // Creating a pointer here. (ie. memory allocation).
setValue(value, size); // These two are my functions, again not sure how you wanted your code but you can easily convert everything into int main().
displayValue(value, size);
return 0;
}
void setValue(int v[], int s)
{
//Now like I said this can be easy to move everything into int main(). Since I know you're a bit knowledgable in C++.
//but this function is what gives the values inside the array.
cout << "Enter the values inside the array: " << endl;
for (int count = 0; count < s; count++)
{
cout << "Value " << (count +1) << ":";
cin >> v[count];
}
}
void displayValue(int v[], int s)
{
//This simply just displays the values that are inside the array.
for (int count = 0; count < s; count++)
{
cout << (count + 1 ) << " value entered was: " << v[count] << endl;
}
}
thanks so much , I had another question:
so output looks like:
Enter the values inside the array:
Value 1: 1
Value 2: 2
1 values entered were: 1
2 values entered were: 2
How can I make it look like:
Enter the values inside the array:
Values : 1 2
values entered were: 1 2
When entering the values I think its impossible to do because its a cin >>. You always need to push "enter" on the keyboard. Not sure though, I might be super wrong but I was able to do what you requested when its displaying the values.
Just replace these two things
1 2 3 4 5 6 7
// belongs in setvalue function
cout << "Enter the values inside the array: " << endl;
for (int count = 0; count < s; count++)
{
cout << "Value " << (count +1) << ":";
cin >> v[count];
}
1 2 3 4 5
//this belongs inside in main(). where the functions are at.
setValue(value, size);
cout << "\nValue's entered were: ";
displayValue(value, size);
cout << "\n";
I also had another question like if I don't want to ask the user for the size but rather let the user enter it is that possible? For example they will enter:5 3 8 2 6 8
If you mean creating an array based on how many values the user enters it is possible, but not really simple with plain arrays.
std::vector would be a better choice for that, but you'd have to find a way to signal the program for the end of the sequence, like "enter a negative number to stop" or "enter a non-numeric character to stop".