I wanted to make a program to output the values inputed by the user, storing them in an array. If the user inputed a negative number the program would close.However ive got an error in the loop: i input a number and the program outputs it, but for example, if its the first time i input the number, it will output one time; if its the second time, it will output 2 times...
the array was initialized to 1, changing sizeOfArray won't change the size.
getchar() only waits for user input if the stream is empty, otherwise it gets and removes the first char from the stream. In your case, cin has a newline remaining from the user's input.
You could have the array dynamically allocated to a size the user inputs. int *iArray = newint[sizeFromUser];
otherwise, the array size has to be a constant value.
If you don't want the user to specify a size you can use an STL container such as a deque.
im a beginner to c++, and i wanted to only use the basic arrays. Just found a program (from c++ for dummies) that is similar to what i want and it only uses arrays and functions. However, i havent understand it yet, i dont know what is missing to mine xD. Could u helped me? Here is the srce:
#include <cstdio>
#include <cstdlib>
#include <iostream>
usingnamespace std;
// prototype declarations
int sumArray(int integerArray[], int sizeOfloatArray);
void displayArray(int integerArray[], int sizeOfloatArray);
int main(int nNumberofArgs, char* pszArgs[])
{
// input the loop count
int nAccumulator = 0;
cout << "This program sums values entered "
<< "by the user\n";
cout << "Terminate the loop by entering "
<< "a negative number\n";
cout << endl;
// store numbers into an array
int inputValues[128];
int numberOfValues;
for(numberOfValues = 0;
numberOfValues < 128;
numberOfValues++)
{
// fetch another number
int integerValue;
cout << "Enter next number: ";
cin >> integerValue;
// if it’s negative...
if (integerValue < 0)
{
// ...then exit
break;
}
// ... otherwise store the number
// into the storage array
inputValues[numberOfValues] = integerValue;
}
// now output the values and the sum of the values
displayArray(inputValues, numberOfValues);
cout << "The sum is "
<< sumArray(inputValues, numberOfValues)
<< endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
// displayArray - display the members of an
// array of length sizeOfloatArray
void displayArray(int integerArray[], int sizeOfArray)
{
cout << "The value of the array is:" << endl;
for (int i = 0; i < sizeOfArray; i++)
{
cout.width(3);
cout << i << ": " << integerArray[i] << endl;
}
cout << endl;
}
// sumArray - return the sum of the members of an
// integer array
int sumArray(int integerArray[], int sizeOfArray)
{
int accumulator = 0;
for (int i = 0; i < sizeOfArray; i++)
{
accumulator += integerArray[i];
}
return accumulator;
}
that allocated a big array at size 128, and used another integer (numberOfValues) to keep track of which indexes of the array actually contain user input.
I actually said something wrong the first time. You shouldn't be able to initialize an non dynamic array to a non constant value.
humm, understood, i´ll try to apply it to my code... i have to do a for loop do determine the size of the array (numberOfValues) and then another for loop to display them. Simple..
"got it, but how can i define the size of iArray, if it depends on the number of values inputed by the user? if it doesnt change... "
you must to read about dynamic allocation to make it. for instance:
1 2 3 4 5 6 7
int* p;
cin>>sizeOfArray;
p = newint[sizeOfArray]; // Now, you can use 'p' as an array;
for(int i = 0 ; i < sizeOfArray ; i++){
cout<<"Enter the number: ";
cin>>p[i];
}
Are you aware that the getChar() call is not reachable? You should be getting a compiler warning about that.
I would recommend renaming some variables. values should be singular. sizeOfArray does not represent the size of anything. It is simply indicating the current number of elements that have been filled with data. Consider better naming conventions so that the variables really represent what it is that they contain.
if you are getting an error that says
"main should return a value"
then the return statement is missing but i dont think it would be such a trivial error can tell you what error is shown