473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading in a file of numbers using FOR statement

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
4 2070
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
Apr 10 '07 #2

"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);
}
Okay here is code.

int main()
{

ifstream inFile;
int months = 1, data;
char quest;
int a, 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 <= 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<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;
}
--
OU

Apr 10 '07 #3
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

Apr 10 '07 #4
Here is the current code, still not functioning properly

#include<iomani p>

#include<fstrea m>

#include<iostre am>

#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_bu ffer;

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
getline(inFile, dummy_string_bu ffer); // Gets a line from the file and
stores it in dummy_string_bu ffer

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

}
Apr 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5215
by: adpsimpson | last post by:
Hi, I have a file which I wish to read from C++. The file, created by another programme, contains both text and numbers, all as ascii (it's a .txt file). A sample of the file is shown below: << LEDAR V1.3 - Real Time Detection >> <LEFT 144> <TOP 165> <RIGHT 265> <BOTTOM 376>
3
3383
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be created when the program executes. But the compile keeps reading that it can't find the text file. Karl you wrote the original program from which...
8
9504
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that contains the following three floating-point numbers: 1.0 2.0 3.0
6
3756
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple...
3
24582
by: Millennium Falcon | last post by:
Hi! Help is kindly requested in reading floating point numbers from a text file. File is organized like this : 2000 // number of data -1.00000 -2.000000 -0.008944 // x, y & z-coordinates to the end of file ... ...
24
2394
by: felixnielsen | last post by:
The question is pretty simple, i have a file called "primes.txt" and funny enough it contains alot of primes (one per line) Besides that i have an empty vector: vector<__int64> P(0); How do i fill that with the contents of the file? P.push_back(line 1); P.push_back(line 2); ect.
10
3277
by: LuTHieR | last post by:
Hi, I'm reading a string of numbers from a file (using Borland C++ Builder 6), and I'm doing it like this: first I use FileRead to store all the data in the file to a char* variable (appropriately called 'data'). Then, I read every number using char *ptr; int value;
9
9849
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I should loop 15,870,080 times. The code is: newprogram.cpp =============
32
2321
by: Bill Cunningham | last post by:
I am interested in writing a numeric text reader. This only reads numbers of securities and stores them. Nice practice. I have determined that these functions are needed. isalpha, isdigit, isspace, and file io. The thing is I don't know how C scans lines. This is a parser of sorts. price average
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7689
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.