Sir I want to know How i Count User input:
i make a program but it is not working correct: Please Help...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream.h>
main ()
{
int c[100];
int i,z;
do
{
int z, i=0;
char x;
cout<< "Please enter the number (-1 to end input)" << endl;
cin >> z;
if (z!= -1)
{
c[i]=z;
}
i++;
}while(z!=-1 && i<100);
cout <<"The total number of positive intergers entered by user is: " << i-1;
}
But:
The out put of this pogram is not as under:
Please enter the number(-1 to end input) 1
2
3
4
5
6
-1
The total number of Positive integer entered by user is 6
#include <iostream>
int main ()
{
int c[100], i, z; // One time declare i and z.
std::cout<< "Please enter the number (-1 to end input)" << std::endl;
for (i = 0, z = 0; (i < 100) && (z != -1); ++i) // For loop works just like do while.
{
//std::cout<< "Please enter the number (-1 to end input)" << std::endl; // If you leave this here it will reappear every loop. So leave it commented.
std::cin >> z;
if (z != -1)
{
c[i]=z;
}
}
std::cout <<"The total number of positive integers entered by user is: " << i-1 << "\n";
return 0;
}