While loop is a bit off...
Feb 8, 2014 at 3:31am UTC
I have a dat file I'm trying to read and produce output from. When all the numbers are added together I'm supposed to get 199 boys, 198 girls, and 397 total students. But I end up getting 217 boys, 215 girls, and 432 total students. My dat file is formatted as January 9 8 all the way to December. I'm thinking my December is getting added twice, but I do not see how. Shouldn't it read all 12 lines of data and terminate due to the Infile.eof condition?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/*-------------------------------------------------------------------------------------------------------
Program: E41.cpp
Author: Ryan Youngen
Purpse: Stream I/O
-------------------------------------------------------------------------------------------------------*/
#include<iostream>
#include<iomanip>
#include<fstream>
#include<assert.h>
#include<string>
using namespace std;
using namespace System;
const char * AUTHOR = "Ryan Youngen Assignment 1 P01.cpp \n\n" ;
const char * LINE = "----------------------------------------" ;
void main()
{
int total;
int totalboys = 0;
int totalgirls = 0;
int totalstudent = 0;
int boys;
int girls;
string Name;
char month[80], subString[80];
int strLength;
Console::Clear();
cout << AUTHOR << LINE << endl;
ifstream Infile("P01.DAT" );
assert(Infile);
while ( !Infile.eof() )
{
Infile >> month >> boys >> girls;
strncpy(subString, month, 3);
strLength = strlen(month);
if (strLength <= 3) subString[strLength] = '\0' ;
else subString[3] = '\0' ;
cout << subString << setw(5);
totalboys += boys;
totalgirls += girls;
total = boys + girls;
totalstudent = totalboys + totalgirls;
for (int stars = 0; stars <= total; stars++)
cout << "*" ;
cout << endl;
}
cout << endl;
Infile.close();
cout << "Total Number of Boys = " << totalboys << endl;
cout << "Total Number of Girls = " << totalgirls <<endl;
cout << "Total Number of Students = " << totalstudent << endl;
cout << endl << endl;
}
Feb 8, 2014 at 10:19am UTC
Topic archived. No new replies allowed.