- #1
kahless2005
- 46
- 0
The Problem (from the handout, word for word):
Write a program that reads a list of integers from the file "numbers.in" into an array, which I will call numbers. Then sort the numbers into 3 groups, a group of odd numbers, a group of even numbers, and a group of negative numbers, use an array to hold the numbers in each group. Finally, you are to output the three groups per the example below. There could be up to 150 numbers in the file numbers.in, which is located in the folder:
My Code ( no functions):
Where am I going wrong?
Write a program that reads a list of integers from the file "numbers.in" into an array, which I will call numbers. Then sort the numbers into 3 groups, a group of odd numbers, a group of even numbers, and a group of negative numbers, use an array to hold the numbers in each group. Finally, you are to output the three groups per the example below. There could be up to 150 numbers in the file numbers.in, which is located in the folder:
My Code ( no functions):
Code:
# include <iostream>
# include <cmath>
# include <fstream>
# include <iomanip>
using namespace std;
const int MAX = 150;
int main ()
{
cout << "Lab 8\t\t\tKevin O'Grady\n\n";
ifstream fin("numbers.in");
if(fin.fail())
{
cerr << "Error opening input file. \n\n";
exit(1);
}
ofstream fout("lab8a.out");
if(fout.fail())
{
cerr << "Error opening output file.\n\n";
exit(1);
}
int even[MAX];
int odd[MAX];
int negative[MAX];
int numbers[MAX];
int index;
int spotneg = 0;
int spoteve = 0;
int spotod = 0;
fout << "Lab 8\t\t\tKevin O'Grady\n\n";
fout << "This program sorts numbers into odd, even and negative groups. \n"
<< "The arrays are: \n"
<< setw(5) << "Even"
<< setw(5) << "Odd"
<< setw(10) << "Negative" << endl;
fin >> numbers[index];
while (!fin.fail() && index < MAX)
{
if (numbers[index] < 0)
{
numbers[index] = negative[spotneg];
spotneg++;
}
else if (numbers[index]% 2 == 0)
{
numbers[index] = even[spoteve];
spoteve++;
}
else
{
numbers[index] = odd[spotod];
spotod++;
}
fout << setw(5) << even[index]
<< setw(5) << odd[index]
<< setw(10) << negative[index] << endl;
fin >> numbers[index++];
}
fin.close();
fout.close();
system ("pause");
return 0;
}
Where am I going wrong?