Reading numbers with FOR loop solved: New problem emerges

G

GeekBoy

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;
}
 
O

Old Wolf

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

GeekBoy

Old Wolf said:
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.

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

GeekBoy

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;
}
 
O

Old Wolf

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

GeekBoy

Old Wolf said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top