Hello there, my question is , i read upto 20 numbers from a file, then i have to sort them into, negatives, odd's and even's, then i have to display total negatives, odd's, and even's, i also have to display all the acutal values of the negatives, odd's, and even's. one problem i m getting is it's only reading 19 numbers (0 to 19), and the rest i think u can see from the output (preety messed up),


here is the input
Code:
54
5
45
4
5
-123
-232
-454
654
54
478
2221
-35
4565
-12
2
33
45
-12
-15


Here is the code

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

int main()
{
	
	ifstream fin;
	int arnNum[21];
	//int Even[20]={0},Odd[20]={0},Neg[20]={0}; // don't know how if this line work, so commented.
	int EvenCount=0,OddCount=0,NegCount=0;
	fin.open("numbers2.txt");
	while (fin)
	{
		for (int x=0;x<20;x++)
		{
			fin>>arnNum[x];
			if (arnNum[x]>0)
			{
				NegCount+=1;
			}
			else if (arnNum[x]%2==0)
			{
				EvenCount+=1;
			}
			else if (arnNum[x]%2!=0)
			{
				OddCount+=1;
			}
			if (fin.eof ())
				break;
		cout<<"Total Negative numbers = "<<EvenCount<<endl;
		cout<<"Total Even numbers = "<<EvenCount<<endl;
		cout<<"Total Odd numbers = "<<OddCount<<endl;
		cout<<x<<endl;// to check for total numbers
		}
	}
	/*cout<<"All the Negative number are "<< // don't know how
	cout<<"All the Even numbers are "<< // don't know how
	cout<<"All the Odd numbers are"<< // don't know how*/
	return 0;
}

Here is what i get as output


Code:
Total Negative numbers = 0
Total Even numbers = 0
Total Odd numbers = 0
0
Total Negative numbers = 0
Total Even numbers = 0
Total Odd numbers = 0
1
Total Negative numbers = 0
Total Even numbers = 0
Total Odd numbers = 0
2
Total Negative numbers = 0
Total Even numbers = 0
Total Odd numbers = 0
3
Total Negative numbers = 0
Total Even numbers = 0
Total Odd numbers = 0
4
Total Negative numbers = 0
Total Even numbers = 0
Total Odd numbers = 1
5
Total Negative numbers = 1
Total Even numbers = 1
Total Odd numbers = 1
6
Total Negative numbers = 2
Total Even numbers = 2
Total Odd numbers = 1
7
Total Negative numbers = 2
Total Even numbers = 2
Total Odd numbers = 1
8
Total Negative numbers = 2
Total Even numbers = 2
Total Odd numbers = 1
9
Total Negative numbers = 2
Total Even numbers = 2
Total Odd numbers = 1
10
Total Negative numbers = 2
Total Even numbers = 2
Total Odd numbers = 1
11
Total Negative numbers = 2
Total Even numbers = 2
Total Odd numbers = 2
12
Total Negative numbers = 2
Total Even numbers = 2
Total Odd numbers = 2
13
Total Negative numbers = 3
Total Even numbers = 3
Total Odd numbers = 2
14
Total Negative numbers = 3
Total Even numbers = 3
Total Odd numbers = 2
15
Total Negative numbers = 3
Total Even numbers = 3
Total Odd numbers = 2
16
Total Negative numbers = 3
Total Even numbers = 3
Total Odd numbers = 2
17
Total Negative numbers = 4
Total Even numbers = 4
Total Odd numbers = 2
18

Thanks

First off, print out the number just read in addition to all the other stuff.

Second:

fin.open("numbers2.txt");
	while (fin)
	{
		for (int x=0;x<20;x++)
		{
			fin>>arnNum[x];

1) Why the while statement followed by a for statement?
2) What happens if fin>>arnNum[x]; causes an error?

Third, if you need to "display all the acutal values" you' should save them in an array for later use.

>one problem i m getting is it's only reading 19 numbers (0 to 19)
If you're only expected to read up to 20 numbers, that's correct. 0 to 19 is exactly 20 numbers.

>and the rest i think u can see from the output (preety messed up)
Reading from a file is pretty straightforward and consistent between applications. It follows this logic:

open the file

while there are more records
  read one record
  process one record
loop

print statistics if any
print saved records if any

close the file

In your case a record is one number, so you would convert that logic into C++ like so:

//open the file
ifstream in ( "numbers2.txt" );
int record;

//while there are more records
//read one record
while ( in>> record ) {
  //process one record
  if ( record < 0 )
    Neg[NegCount++] = record;
  else if ( record == 0 || record % 2 == 0 )
    Even[EvenCount++] = record;
  else
    Odd[OddCount++] = record;
}

//print statistics if any
cout<<"Negatives: "<< nNegatives <<'\n';
cout<<"Evens: "<< nEvens <<'\n';
cout<<"Odds: "<< nOdds <<'\n';

//print saved records if any

//ifstream closes the file from the destructor

Since you'll probably be storing the numbers in one of the three arrays that you commented out, you only need a single int for holding the record.

>one problem i m getting is it's only reading 19 numbers (0 to 19)
If you're only expected to read up to 20 numbers, that's correct. 0 to 19 is exactly 20 numbers.

>and the rest i think u can see from the output (preety messed up)
Reading from a file is pretty straightforward and consistent between applications. It follows this logic:

open the file

while there are more records
  read one record
  process one record
loop

print statistics if any
print saved records if any

close the file

In your case a record is one number, so you would convert that logic into C++ like so:

//open the file
ifstream in ( "numbers2.txt" );
int record;

//while there are more records
//read one record
while ( in>> record ) {
  //process one record
  if ( record < 0 )
    Neg[NegCount++] = record;
  else if ( record == 0 || record % 2 == 0 )
    Even[EvenCount++] = record;
  else
    Odd[OddCount++] = record;
}

//print statistics if any
cout<<"Negatives: "<< nNegatives <<'\n';
cout<<"Evens: "<< nEvens <<'\n';
cout<<"Odds: "<< nOdds <<'\n';

//print saved records if any

//ifstream closes the file from the destructor

Since you'll probably be storing the numbers in one of the three arrays that you commented out, you only need a single int for holding the record.

Sorry i meant (0 to 18), 19 numbers

Thanks

>one problem i m getting is it's only reading 19 numbers (0 to 19)
If you're only expected to read up to 20 numbers, that's correct. 0 to 19 is exactly 20 numbers.

>and the rest i think u can see from the output (preety messed up)
Reading from a file is pretty straightforward and consistent between applications. It follows this logic:

open the file

while there are more records
  read one record
  process one record
loop

print statistics if any
print saved records if any

close the file

In your case a record is one number, so you would convert that logic into C++ like so:

//open the file
ifstream in ( "numbers2.txt" );
int record;

//while there are more records
//read one record
while ( in>> record ) {
  //process one record
  if ( record < 0 )
    Neg[NegCount++] = record;
  else if ( record == 0 || record % 2 == 0 )
    Even[EvenCount++] = record;
  else
    Odd[OddCount++] = record;
}

//print statistics if any
cout<<"Negatives: "<< nNegatives <<'\n';
cout<<"Evens: "<< nEvens <<'\n';
cout<<"Odds: "<< nOdds <<'\n';

//print saved records if any

//ifstream closes the file from the destructor

Since you'll probably be storing the numbers in one of the three arrays that you commented out, you only need a single int for holding the record.

Sorry i meant (0 to 18), 19 numbers, but i fixed that, by removing the while loop

if (arnNum[x]>0 && arnNum[x]%2==0)
        {
            EvenCount+=1;
        }
        else if (arnNum[x]>0 && arnNum[x]%2!=0)
        {
            OddCount+=1;
        }
        else if (arnNum[x]<0)
        {
            NegCount+=1;
        }

i don't no y it;s not reading only 6 negatives numberes, but there are actully 7 negatives, this is what i get as output.

Total Negative numbers = 6
Total Even numbers = 6
Total Odd numbers = 7

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.