At first I wanted the user to input the number of elements in the array, but I couldn't get that so I just made a constant int. I want the user to enter number of scores that will be stored in the array and then input -99 to stop. How can I stop it without the output being like this.
The problem here is that you print all n elements even if the user has entered fewer than n values. One way to solve this is to check if scores[count] != -99.
Thanks so much, instead of using the -99 I decided to just use the allocate array which I just searched up on google lol. I haven't learned std::vector though. This is what I got and now I can finish the program Thanks man!
#include <iostream>
usingnamespace std;
int fillArray(int*,int);
int fillArray(int *scores,int n)
{
cout<<"Enter scores"<<endl;
for(int count=0;count<n;count++)
cin>>scores[count];
return n;
}
int main()
{
int n;
int *scores;
cout<<"How many scores would you like to type?"<<endl;
cin>>n;
scores= newint[n];
fillArray(scores,n);
for(int x=0;x<n;x++)
cout<<scores[x]<<endl;
system("pause");
return 0;
}
int fillArray(int scores[],int n)
{
int count=0;
cout<<"Enter scores"<<endl;
//while(scores[count] !=-99) <---- you can use only the for statement
for(count=0;count<n;count++){
cin>>scores[count];
if(scores[count]==-99)
break;
}
return count; //you must return count, not n
}
//the main
int main(){
constint n=30;
int scores[n] ;
fillArray(scores, n);
int dim=fillArray(scores,n);
for(int count=0;count<dim;count++)
cout<<scores[count]<<" "<<endl;
return 0;
}