By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,818 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,818 IT Pros & Developers. It's quick & easy.

Reading in a file of numbers using FOR statement

P: n/a
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.

Thanks a lot.

GB

Apr 10 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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_input) {
// ...
while(read_numbers < numbers_to_read) {
//... read in
}
std::getline(in,dummy_string_buffer);
}

--
OU
Apr 10 '07 #2

P: n/a

"Obnoxious User" <OU@127.0.0.1wrote in message
news:46***********************@alt.teranews.com...
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_input) {
// ...
while(read_numbers < numbers_to_read) {
//... read in
}
std::getline(in,dummy_string_buffer);
}
Okay here is code.

int main()
{

ifstream inFile;
int months = 1, data;
char quest;
int a, x, y, z;
float avg = 0;
inFile.open("input4.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 <= 12; z++) // loop for 12months of rain fall
{
inFile >data;
} // end of for loop #3
for (a = 1; a <= months; a++)
{
cout << setw(4) << data << setw(4);
avg = data + avg;
} // end of for loop #4
cout <<": " << setw(2) << setprecision(2)<<
avg/static_cast<float>(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;
}
--
OU

Apr 10 '07 #3

P: n/a
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<fstream>

#include<iostream>

#include<iomanip>

using namespace std;

int main()

{

ifstream inFile;

int months = 1, data;

char quest;

int x, y, z;

float avg = 0;

inFile.open("input4.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<float>(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.1wrote in message
news:46***********************@alt.teranews.com...
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_input) {
// ...
while(read_numbers < numbers_to_read) {
//... read in
}
std::getline(in,dummy_string_buffer);
}

--
OU

Apr 10 '07 #4

P: n/a
Here is the current code, still not functioning properly

#include<iomanip>

#include<fstream>

#include<iostream>

#include<string>

using namespace std;

int main()

{

ifstream inFile;

int months = 1, data;

char quest;

int x, y, z;

float avg = 0;

string dummy_string_buffer;

inFile.open("input4.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
getline(inFile, dummy_string_buffer); // Gets a line from the file and
stores it in dummy_string_buffer

cout <<": " << setw(2) << static_cast<int>(avg/static_cast<float>(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;

}
Apr 10 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.