calculations with file input creating infinite loops
Oct 23, 2013 at 1:30am UTC
Hi! Thanks for reading this. I'm inputting data from a txt file, and I'm trying to add doubles that are assigned to five different integers in the format of
101 38.00
203 124.25
203 30.00
222 65.00
112 74.50
214 97.75
101 87.00
222 102.50
101 21.50
0
with no specified length or numbers. I'm attempting to use a sentinel value read loop but I'm not sure if I'm formatting it correctly. I'm guessing I don't have my loop ordered correctly, or perhaps I'm not assigning variables correctly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
ofstream a;
ifstream b;
int homeroom;
const int SENTINEL = 0;
double count;
double money;
double money_101, money_112, money_203, money_214, money_222;
b.open ("seniors_rule.txt" );
count = 0;
b >> homeroom >> money;
while (homeroom != SENTINEL);
{
if (homeroom == 101)
{
money_101 = count + money;
}
else if (homeroom == 112)
{
money_112 = count + money;
}
else if (homeroom == 203)
{
money_203 = count + money;
}
else if (homeroom == 214)
{
money_214 = count + money;
}
else if (homeroom == 222)
{
money_222 = count + money;
}
b.close ();
}
cout << money_101 << " " << money_112 << " " << money_203 << " " << money_214
<< " " << money_222;
return 0;
}
Thank you for your feedback, and please let me know if there are adjustments I need to make to the form of my question or other concerns. Thanks!
Last edited on Oct 23, 2013 at 1:41am UTC
Oct 23, 2013 at 1:41am UTC
Why do you close the input file in the body of the while loop?
And why is only the first pair of values read from the file?
Although the logic is flawed, the semicolon here doesn't help:
while (homeroom != SENTINEL); // remove semicolon
Last edited on Oct 23, 2013 at 1:50am UTC
Oct 23, 2013 at 1:50am UTC
I moved the close file function outside of the loop and tried to apply a function to ensure more than the first value gets read. This is where I'm at now:
code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
ofstream a;
ifstream b;
int homeroom;
const int SENTINEL = 0;
double count;
double money;
double money_101, money_112, money_203, money_214, money_222;
b.open ("seniors_rule.txt" );
count = 0;
while (homeroom != SENTINEL);
{
while (b >> homeroom >> money);
{
if (homeroom == 101)
{
money_101 = count + money;
}
else if (homeroom == 112)
{
money_112 = count + money;
}
else if (homeroom == 203)
{
money_203 = count + money;
}
else if (homeroom == 214)
{
money_214 = count + money;
}
else if (homeroom == 222)
{
money_222 = count + money;
}
}
}
b.close ();
cout << money_101 << " " << money_112 << " " << money_203 << " " << money_214
<< " " << money_222;
return 0;
}
Still getting infinite loops...
thanks
Last edited on Oct 23, 2013 at 1:52am UTC
Oct 23, 2013 at 2:04am UTC
So instead of getting infinite loops, I'm now getting garbage, which is an improvement, I think? Maybe?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
ofstream a;
ifstream b;
int homeroom;
const int SENTINEL = 0;
double count;
double money;
double money_101, money_112, money_203, money_214, money_222;
b.open ("seniors_rule.txt" );
if (homeroom != SENTINEL)
{
while (b >> homeroom >> money);
{
if (homeroom == 101)
{count = 0;
money_101 = count + money;
}
else while (homeroom == 112)
{count = 0;
money_112 = count + money;
}
while (homeroom == 203);
{count = 0;
money_203 = count + money;
}
while (homeroom == 214);
{count = 0;
money_214 = count + money;
}
while (homeroom == 222);
{count = 0;
money_222 = count + money;
}
}
}
b.close ();
cout << money_101 << " " << money_112 << " " << money_203 << " " << money_214
<< " " << money_222;
return 0;
}
Last edited on Oct 23, 2013 at 2:04am UTC
Oct 23, 2013 at 2:12am UTC
The accumulators money_101 etc. should be initialised to zero.
This line should not have a semicolon:
while (b >> homeroom >> money);
Oct 23, 2013 at 2:40am UTC
ok, so adjusting based on feedback, I have
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
ofstream a;
ifstream b;
int homeroom;
const int SENTINEL = 0;
double count;
double money;
double money_101 = 0, money_112 = 0, money_203 = 0, money_214 = 0, money_222 = 0;
b.open ("seniors_rule.txt" );
while (b >> homeroom >> money);
{
count = 0;
while (homeroom != SENTINEL)
{
while (homeroom == 101)
{
count = count + money;
money_101 = count;
}
while (homeroom == 112)
{
count = 0;
count = count + money;
money_112 = count;
}
while (homeroom == 203)
{
count = 0;
count = count + money;
money_203 = count;
}
while (homeroom == 214)
{
count = 0;
count = count + money;
money_214 = count;
}
while (homeroom == 222)
{
count = 0;
count = count + money;
money_222 = count;
}
}
}
b.close ();
cout << money_101 << " " << money_112 << " " << money_203 << " " << money_214
<< " " << money_222;
return 0;
}
Oct 23, 2013 at 3:31am UTC
For the third time (do you read my posts?)
This line should not have a semicolon:
while (b >> homeroom >> money);
Also, after correcting that error, that is the only
while
loop which is needed. I see the word "while" seven times in the latest version, which is six times too many. The others should be if/else, or maybe a switch/case.
Last edited on Oct 23, 2013 at 3:39am UTC
Topic archived. No new replies allowed.