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

Reading numbers with FOR loop solved: New problem emerges

P: n/a
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
Share this Question
Share on Google+
5 Replies


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

P: n/a

"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

P: n/a

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

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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.