Problem with array and loop

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>             
using namespace std;
int main()
{  
    
    int values;
    int sizeofArray = 1;
    cin >> values;
    while(values>=0)
    {
       int iArray[sizeofArray];   //i dont know what the size ive got to put.
       
       cout << "You inputed the following values:" << endl;
       for(int i=0 ; i<sizeofArray ; i++)
         {
               iArray[i] = values;
               cout << iArray[i] << endl;
         }
       sizeofArray++;
       cin >> values;
    }
    if(values<0)
    {
       cout << "You entered a negative number(" << values << "). Closing...";
       getchar();
       getchar();
    }
}


why do i have to put 2 getchar() ?
please help..
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.
Last edited on
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 could have the array dynamically allocated to a size the user inputs.
int *iArray = new int[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.
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace 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.
Last edited on
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..
why cant i compile that :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>             
using namespace std;
int main()                     
{                               
                                
    int values;
    int sizeofArray;
    int iArray[128];
       for(int sizeOfArray = 1; sizeOfArray < 128; sizeOfArray++)
       {
               cin >> values;
               if(values<0)
               {
                   cout << "You entered a negative number(" << values << "). Closing..." << endl;
                   cout << "sizeOfArray is " << sizeOfArray;
                   break;
                   getchar();
               }
               iArray[sizeOfArray] = values;
       }
       for(int i = 0; i <= sizeOfArray; i++)
       {
               cout << "You´ve inputed the following numbers: " << endl
                    << iArray[i];
       }
}
"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 = new int[sizeOfArray]; // Now, you can use 'p' as an array;
for(int i = 0 ; i < sizeOfArray ; i++){
       cout<<"Enter the number: ";
       cin>>p[i];
}



I hope have helped.



1
2
3
4
5
6
7
if(values<0)
{
    cout << "You entered a negative number(" << values << "). Closing..." << endl;
    cout << "sizeOfArray is " << sizeOfArray;
    break;
    getchar();
}


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
Fix it. Now im going to read a bit about dynamic allocation ^^. Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>             
using namespace std;
int main()                     
                               
{                               
                                
                                
    int values;
    int sizeOfArray;
    int iArray[128];
       for(sizeOfArray = 0; sizeOfArray < 128; sizeOfArray++)
       {
               cout << "Please input the numbers: " << endl;
               cin >> values;
               if(values<0)
               {
                   cout << "You entered a negative number(" << values << ")." << endl;
                   cout << "sizeOfArray is " << sizeOfArray << endl << endl;
                   break;
               }
               iArray[sizeOfArray] = values;
       }
       cout << "Youve inputed the following numbers: " << endl;
       for(int i = 0; i < sizeOfArray; i++)
       {
               cout << iArray[i] << endl;
       }
system("PAUSE");
return 0;
}


With cin.get() at the end the program (instead of system("PAUSE");return 0;), wouldnt stay open, weird...
It's probably because of a stray character(s) (probably \r or \n) from line 14. You can use cin.clear() before it to fix it I believe.
cin.sync(). cin.clear() clears error flags.
didnt work with cin.clear(), but worked with cin.ignore() :D Altough i dont understand why do we have to put it. what is a stray character ?
a character left in the stream. cin.get only waits for user input if the stream is empty, otherwise it gets it from what's in the stream.
Topic archived. No new replies allowed.