Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reading numbers with FOR loop solved: New problem emerges

Reply
Thread Tools

Reading numbers with FOR loop solved: New problem emerges

 
 
GeekBoy
Guest
Posts: n/a
 
      04-10-2007
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;
}

 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      04-10-2007
On Apr 11, 11:47 am, "GeekBoy" <(E-Mail Removed)> wrote:
> 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?

 
Reply With Quote
 
 
 
 
GeekBoy
Guest
Posts: n/a
 
      04-11-2007

"Old Wolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> On Apr 11, 11:47 am, "GeekBoy" <(E-Mail Removed)> wrote:
>> 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.


>



 
Reply With Quote
 
GeekBoy
Guest
Posts: n/a
 
      04-11-2007

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



 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      04-11-2007
On Apr 11, 2:14 pm, "GeekBoy" <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
GeekBoy
Guest
Posts: n/a
 
      04-11-2007

"Old Wolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On Apr 11, 2:14 pm, "GeekBoy" <(E-Mail Removed)> wrote:
>> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
A New Candidate Emerges! Annika1980 Digital Photography 5 08-28-2008 01:14 PM
Another IE 6.0 vulnerability emerges Au79 Computer Support 0 07-05-2006 09:07 PM
New Microsoft Office Flaw Emerges Au79 Computer Support 0 06-25-2006 05:50 AM
Second zero-day Excel flaw emerges Au79 Computer Support 0 06-20-2006 10:16 PM



Advertisments