473,388 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Reading numbers with FOR loop solved: New problem emerges

Thanks for the help obnoxious.
Reading in the rest of the line with "getline" worked great and now
the averages work.

Now for the new problem. Program on first run works flawlessly.
Next runs enter in some data I don;t know what instead of the data in
the file. Tried closing and opening file again, but it's not working.

#include<iomanip>
#include<fstream>
#include<iostream>
#include<string>
using namespace std;

int main()
{

ifstream inFile;
int months = 1;
char quest;
int x, y, z;
float avg = 0, data;
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 << dummy_string_buffer;
cout <<": " << setw(2) << static_cast<int>(avg/
static_cast<float>(months));
cout << " inches\n";
avg=0;
} //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
//inFile.close();
// inFile.open("input4.txt");
//data =0;
//if (!inFile)
//{
//cerr << "Error Opening File" << endl;
//system ("pause");
//return 1;
// }
} //end of else
} //end of while loop

inFile.close();
system ("pause");
return 0;
}

Apr 10 '07 #1
5 1905
On Apr 11, 11:47 am, "GeekBoy" <tcr...@gmail.comwrote:
Now for the new problem. Program on first run works flawlessly.
Next runs enter in some data I don;t know what instead of the data in
the file. Tried closing and opening file again, but it's not working.
You will need to be more specific than "not working".

Also, what do you mean by "next runs". Did you quit the program
and run it again, or did you actually mean that you went through
your main loop again in the same run?
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
What will happen if they enter -1, or 15, or "Hello", for the
number of months?

In fact, what is the purpose of the 'while' condition, since you
never check it after the user input?

Apr 10 '07 #2

"Old Wolf" <ol*****@inspire.net.nzwrote in message
news:11*********************@w1g2000hsg.googlegrou ps.com...
On Apr 11, 11:47 am, "GeekBoy" <tcr...@gmail.comwrote:
>Now for the new problem. Program on first run works flawlessly.
Next runs enter in some data I don;t know what instead of the data in
the file. Tried closing and opening file again, but it's not working.

You will need to be more specific than "not working".

Also, what do you mean by "next runs". Did you quit the program
and run it again, or did you actually mean that you went through
your main loop again in the same run?

The latter. Running again in the same loop.
Running it on inital run alaways functions properly.

>
>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

What will happen if they enter -1, or 15, or "Hello", for the
number of months?

In fact, what is the purpose of the 'while' condition, since you
never check it after the user input?
Oppppss...Thanks for alerting me to that...time to do another while loop.

>

Apr 11 '07 #3

Just don't know what I am doing.

It functions a bit better

-----------------------------------------------------------------------

#include<iomanip>
#include<fstream>
#include<iostream>
#include<string>
using namespace std;

int main()
{

ifstream inFile;
int months = 1;
char quest = 'y';
int x, y, z;
float avg = 0, data;
string dummy_string_buffer;

inFile.open("input4.txt");
if (!inFile)
{
cerr << "Error Opening File" << endl;
system ("pause");
return 1;
}
while (quest != 'n')
{

cout << "Please enter the number of months/year to be averaged: ";
cin >months;

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 << dummy_string_buffer;
cout <<": " << setw(2) <<
static_cast<int>(avg/static_cast<float>(months));
cout << " inches\n";
avg=0;
} //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
//inFile.close();
// inFile.open("input4.txt");
//data =0;
//if (!inFile)
//{
//cerr << "Error Opening File" << endl;
//system ("pause");
//return 1;
// }
} //end of else
}//end of while loop #2
} //end of while loop #1

inFile.close();
system ("pause");
return 0;
}

Apr 11 '07 #4
On Apr 11, 2:14 pm, "GeekBoy" <n...@nerdy.comwrote:
Just don't know what I am doing.
It functions a bit better
That's not very helpful either. You should say:
- what input you provided
- what output you expected
- what output actually happened
while (quest != 'n')
{
cout << "Please enter the number of months/year to be averaged: ";
cin >months;

while (months >= 1 && months <= 12)
{
cout << "Please enter the number of months/year to be averaged: ";
cin >months;
So you ask for the months twice in a row, and still do not bother
to check what they typed in for the second one.

You have to check the result of every input (user input and
file read) to check that it was successful, and that it was
what you were expecting. If you are getting strange behaviour
then try to print out exactly what the inputs read; it might
not be what you thought.

Apr 11 '07 #5

"Old Wolf" <ol*****@inspire.net.nzwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
On Apr 11, 2:14 pm, "GeekBoy" <n...@nerdy.comwrote:
>Just don't know what I am doing.
It functions a bit better

That's not very helpful either. You should say:
- what input you provided
- what output you expected
- what output actually happened
Same thing as before.
The input file is not read and outputs a single number for ALL data fields

>
>while (quest != 'n')
{
cout << "Please enter the number of months/year to be averaged: ";
cin >months;

while (months >= 1 && months <= 12)
{
cout << "Please enter the number of months/year to be averaged: ";
cin >months;

So you ask for the months twice in a row, and still do not bother
to check what they typed in for the second one.

You have to check the result of every input (user input and
file read) to check that it was successful, and that it was
what you were expecting. If you are getting strange behaviour
then try to print out exactly what the inputs read; it might
not be what you thought.

Apr 11 '07 #6

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

Similar topics

2
by: Satish Chimakurthi | last post by:
Hi all, This is in continuation of my email today regarding reading data files. I am stuck with one more problem now. Here are my two data files: a. fluid_grid.dat 1.00000000000000 ...
9
by: EkteGjetost | last post by:
I would like to first apologize to those of you who read my last post "desperately need help". As a regular on other forums i can understand how aggravating it would be to have someone come on who...
2
by: pesso | last post by:
I have a string that contains the following: string s = "130,41,43,178,41,17,6,78,244,35,202,144,115"; They are comma separated byte numbers, and I need to initialize my byte array with them....
8
by: Jon Maz | last post by:
Hi All, Quick one: several times in vb.net code I have got off the net there are lines such as: For i As Integer = 0 To 10 which, on compiling with VS2002, produce the following error: ...
9
by: Alex Buell | last post by:
I have a small text file which consist of the following data: ]] And the code I've written is as follows: ]] The trouble is, I can't work out why it goes into an infinite loop reading the...
4
by: GeekBoy | last post by:
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
9
by: Eric Lilja | last post by:
Hi! I have a program with a class that needs to be able to write itself to a file in clear text format. The file has two integers and vector of struct objects. The struct has a string that can...
5
by: imailz | last post by:
Hi all, since I'm forced to switch from Fortran to C I wonder if there is posibility in C: 1) to use implicit loops 2) to parse several variables which number is determined at runtime. ...
32
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,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.