Using cin in arrays

Hi, I cant figure out how to use the list of numbers that the user inputs to output back to the screen and show how many their is. I have tried the i++ and other similar things, yet it just gives me the first number that is input. Here is the code...

/*
Assignment 2
2/12/08
*/

#include<iostream>
#include<cmath>
using namespace std;


int main()
{
cout<<"Sum, Mean, Minimum and Maximum Calculator"<<endl;
cout<<"This pragram calculates the sum, mean, minimum, and maximum of a series of real numbers"<<endl;
cout<<"Enter any number, up to a series of 100 numbers and the program will output the answer"<<endl;


float numslist[100];

for (int i=0; i<100; i++)
{
cout<<"enter Numbers Here:"<<endl;
cin>>numslist[i];
{
if(numslist>=0);
cout<<"Numbers:"<<numslist[i]<<endl;

cout<<"Numbers Processed="<<i<<endl;

if(i<0);

cout<<"error, please enter a possitive real number"<<endl;
break;

if(i>100);

cout<<"error, you have entered more than 100 numbers"<<endl;
break;
}

}
system("pause");
return 0;
}
I'm pretty much a n00b here too, but it seems to me that you need another loop for your output, much the same as your "if" loop for your input, so you can output one number per iteration of the loop.

Hello Rolopez,

You've got a few problems here.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
cout<<"Sum, Mean, Minimum and Maximum Calculator"<<endl;
cout<<"This pragram calculates the sum, mean, minimum, and maximum of a series of real numbers"<<endl;
cout<<"Enter any number, up to a series of 100 numbers and the program will output the answer"<<endl;


float numslist[100];

for (int i=0; i<100; i++)
{
cout<<"enter Numbers Here:"<<endl;
cin>>numslist[i];


Up to this point everything is ok.

 
if(numslist>=0);


When we get to this point you're getting a little off track. The first thing is that you are doing a comparison on an array and that isn't going to provide you with any useful information. It won't tell you if any of the elements are used or not. That is something you have to keep track of yourself. Second you aren't doing anything with the answer. By placing a semicolon at the end of your statement you are causing it to do nothing regardless of the result. Your if statement should look like this.

1
2
3
4
5
6
if(test)
{
    command1;
    command2;
    command3;
}


The commands between the { } (curly braces) will execute if "test" returns a positive result.

 
cout<<"Numbers:"<<numslist[i]<<endl;


To print out the elements of an array you have to use a loop structure. In this case since you are using a for loop to increment through your array you could use a nested for loop to print you the elements currently entered.

1
2
3
4
5
for(int k=0; k < i+1; k++)
{
    cout << numlist[k] << " ";
}
cout << endl;


This would print out all of the elements entered.

 
cout<<"Numbers Processed="<<i<<endl;


This is fine.

1
2
3
4
5
6
7
8
9
10
if(i<0);

cout<<"error, please enter a possitive real number"<<endl;
break;

if(i>100);

cout<<"error, you have entered more than 100 numbers"<<endl;
break;
}


Again you are ignoring the results of your if statements. Everything after your if statements is executing regardless of the results. The break; statement is causing your loop to terminate on the first iteration since it isn't inside an if statement.

Also since for loops will increment your counter variable ("i" in this case) automatically if(i<0); will return false every time you loop through it. I think you want to compare the entered number numlist[i]. Also if(i>100) will never be true since your for loop will terminate when i = 100;
Last edited on
Skroegs pointed you to quite a few things that you need to look at your code
Just a small addition here: (Comments inline)

if(numslist>=0);
/This compares the base address of the array with 0. Remember, that the name of the array is actually the base address of the array or a pointer to the first element in the array. So you'll end up comparing the address in memory with 0 in this case.

You would rather do if( (i-1) >= 0)
std::cout <<" Total no of numbers is"<<i-1<<std::endl;
Hope that helps.
Good luck!

Topic archived. No new replies allowed.