Here is the correct code. The other is what I was playign around with.
//*************** *************** *************** *************** *************** ********
// Trent Creekmore CSCI 1380.06 Spring 2007 Assignment # 4
// Due date: MM/DD/YY
//
// (Brief description of what the program does)
//
//*************** *************** *************** *************** *************** ********
#include<fstrea m>
#include<iostre am>
#include<iomani p>
using namespace std;
int main()
{
ifstream inFile;
int months = 1, data;
char quest;
int x, y, z;
float avg = 0;
inFile.open("in put4.txt");
if (!inFile)
{
cerr << "Error Opening File" << endl;
system ("pause");
return 1;
}
while (months >= 1 && months <= 12)
{
cout << "Please enter the number of months/year to be averaged: ";
cin >months;
cout << "Year/" << endl;
cout << " /Month";
for (x = 1; x <= months; x++) //prints out number of months header
{
cout << setw(4) << x;
} // end of for loop #1
cout << " Avg\n" << endl ;
for (y = 1; y <= 10; y++) // loop for 10 years of rain fall data
{
cout << setw(2)<< y << " ";
for (z = 1; z <= months; z++) // loop for (x)months of rain fall
{
inFile >data;
cout << setw(4) << data << setw(4);
avg = data + avg;
} // end of for loop #3
cout <<": " << setw(2) << setprecision(2) << avg/static_cast<flo at>(months);
cout << " inches\n";
} //end of for loop #2
cout << "Do you want to do it again? (y/n:)";
cin >quest;
if (quest == 'n'){
break;
}
else
system ("cls"); //clears screen
} //end of while loop
inFile.close();
system ("pause");
return 0;
}
"Obnoxious User" <OU@127.0.0.1wr ote in message
news:46******** *************** @alt.teranews.c om...
GeekBoy skrev:
>I am reading a file of numbers using for loops.
The numbers are in a grid as follows:
8 36 14 11 31 17 22 23 17 8 9 33
23 32 18 39 23 25 9 38 14 38 4 22
18 11 31 19 16 17 9 32 25 8 1 23
19 13 33 28 5 28 34 19 34 32 0 7
25 0 16 5 12 4 15 39 33 33 29 21
6 0 34 1 16 14 15 2 29 0 16 20
16 29 24 6 37 24 38 4 21 6 7 13
20 29 11 32 18 6 4 3 31 20 21 4
19 5 8 3 20 17 35 15 25 24 11 8
7 9 1 22 26 33 12 0 30 0 32 31
There are 2 for loops. The first one takes a user input and reads each
line up to a point of the user input: (eg User: 4; grabs the first 4
numbers of each row)
The problem is reading the next line. Instead the for statement going
to next line it contines on from where it left off (eg 4-again; 8 36
14 11; 31 17 22 23;)
I have the numbers as ints and not using , nor cannot use arrays.
Any advice how to get to the next line? I did try reading in the
whole line then printout the desired number length.
Hm, where's the code? If you need to discard the remaining line, you
could do it with for example std::getline().
std::ifstream in(...);
while(user_inpu t) {
// ...
while(read_numb ers < numbers_to_read ) {
//... read in
}
std::getline(in ,dummy_string_b uffer);
}
--
OU