C++ Hates Me!!

I am writing a program that will extract information from a file on my disk (DataSet.txt), use that information to make a graph of sorts, and then spit that graph out to another file (Graph.txt).

On my screen, the program appears to be flawless, but when I execute it, I am running into problems.

Here’s what I have so far…

DataSet

Orville’s Acres, 114.8 43801
Hoffman’s Hills, 77.2 36229
Jiffy Quick Farm, 89.4 24812
Jolly Good Plantation, 183.2 104570
Organically Grown Inc., 45.5 14683
This is the farm that really matters, 50.3 10000

Program

while(enterfile)
{
count = 0;
enterfile.get(FarmName);
while(FarmName != ‘,’ && count < 28)
{
exitfile << FarmName;
enterfile.get(FarmName);
count++;
}
}

Graph.txt

Orville’s Acres 114.8 43801
Hoffman’s Hils 77.2 36229
Jiffy Quick Fam 89.4 24812
Jolly Good Platation 183.2 104570
Organically rown Inc. 45.5 14683
This is the fam that really matters 50.3 10000000000000000000

From what I can tell, it seems to be completely ignoring the while statement. It should stop getting letters once it hits a comma or 28 characters. In actuality, it just keeps going, even leaving out randon letters.

I’m a bit frazzeled at the moment, as I simply cannot figure out what’s wrong. If anyone could be of help, I would be soooo appreciative.

I see the problem Doper. It’s not that your while loop is skipped, it’s that you’re just continuing on after a comma is found. Note that when you encounter a comma, you are purposely skipping the innermost while loop so the comma is not output to your exitfile. On the next pass, count is reset to zero, but you just move on to the next character in the line.

At the completion of your inner loop, you need to do one more scan to find the end of the line before continuing. I’m not sure how you want your data represented in your ouput file or other conditions, but that’s the basic problem.

Hope this helps.

Stone

The problem, as JZStone says, is in your outer while loop, not your inner one. When enterfile.get reaches the end of the file, it does not set enterfile to FALSE or NULL. It merely does not read a new character, and consequently, FarmName retains the same value (that final zero) and outputs it as part of the while(enterfile) loop. Instead of making while(enterfile) your loop condition, you should use something to the effect of while(!enterfile.eof())

I understand what you are saying. Yes, the loop stops once it hits a comma or 30 characters, but then it continues right on to the next character on the line which is a space followed by a number.

Is there any way I can fix that? I am at a total loss as to what to do.

Thanks again,
DoperChic

I think I’d also check in the inner loop for EOF, although in this case it doesn’t hurt to let it be handled outside of the loop. You just have to watch so that you don’t have problems in case you do hit EOF in the middle of the inner loop. I’d do something like this …


while(!enterfile.eof()){ // per **cmkeller**'s suggestion

for(register int count = 0; ( !enterfile.eof() &&  enterfile.get(FarmName) != ',')  && (count < 28); count++)
	exitfile << FarmName;
 

/* This is where you need to add something.  Maybe a dummy
  * loop to scan for end of line (AND end of file -- here, it's 
  * necessary, unless you use another count variable).
  * something like 
   *while (!enterfile.eof() && enterfile.get  (FarmName) != EndofLine) ;,
  * where EndOfLine may depend on the machine
  * or something that gets the numeric data from this line ...*/
}


(hey, it’s what I would very likely do. I don’t get paid to program (at least not in higher-level languages).)

There was more to the program than what I cut and pasted into the thread.

Following the name of each farm are two values: the number of acres in the farm and the number of jars of popcorn produced by the farm. The program uses these numbers to make a “bar” graph. The graph is actually comprised of stars with each star representing 25 jars per acre.

I managed to fix the name problem by simply finishing writing the program. I assigned the two numbers on each line to be acres and jars. This kept them from being put through the original FarmName loop.

Now for the new problems…

The first line in the graph is indented one space. The name is fine, but the actual graph itself is indented.

The other problem seems to lie within the formula for figuring out the number of stars. For all of the farms until the last one, the formula works. The last farm (This is the one that really matters) should have 7 stars. Instead, it has 12.

I can’t for the life of me figure out what’s wrong.

–DoperChic

DataSet

Orville’s Acres, 114.8 43801
Hoffman’s Hills, 77.2 36229
Jiffy Quick Farm, 89.4 24812
Jolly Good Plantation, 183.2 104570
Organically Grown Inc., 45.5 14683
This is the farm that really matters, 50.3 10000

Program

#include <string> // For strings
#include <fstream> // File input/output
#include <iostream>

const string HEADER1 = “Jessica M Kleiss CSC141-05 | Production in Hundreds |”;
const string HEADER2 = “Project #6 4/1/2002 | Of Pint Jars Per Acre |”;
const string HEADER3 = " | 1 2 3 4 5 6 |";
const string HEADER4 = " F A R M N A M E |—|---|—|---|—|---|—|";
const string DONE = “Job is finished”;
//const char BLANKS = ’ ';
//const char STARS = ‘*’;

char FarmName;
int count;
int count2;
int count3;
int number;
int counts;
int blanks;
float acres;
float jars;
float stars;

int main()
{

ifstream enterfile; // identifies enterfile as type ifstream
ofstream exitfile; // identifies exitfile as type ofstream

enterfile.open(“DataSet.txt”); // Gets data from DataSet.txt
exitfile.open(“Graph.txt”); // Directs output to Graph.txt
//Print Headers

exitfile << HEADER1 << endl << HEADER2 << endl << HEADER3 << endl << HEADER4 << endl;

//Print Farm Names

while(enterfile)
{
count = 0;
enterfile.get(FarmName);
while(count <= 30 && FarmName != ‘,’)
{
exitfile << FarmName;
enterfile.get(FarmName);
count++;
}

//Print Blanks and Opening |

while (count < 31)
{
exitfile << ’ ';
count ++;
}

exitfile << “|”;

//Print Stars

enterfile >> acres >> jars;
stars = (jars/acres)/25;

count2 = 1;
while (count2 <= stars)
{
while (count2 == 20)
{
exitfile << “#”;
count2 ++;
}
exitfile << “*”;
count2 ++;
}

//Print Blanks and Closing |

while (count2 < 28)
{
exitfile << ’ ';
count2 ++;
}
exitfile << “|”;
}
cout << DONE << endl << endl << endl;

return 0;

}

Graph.txt

Jessica M Kleiss CSC141-05 | Production in Hundreds |
Project #6 4/1/2002 | Of Pint Jars Per Acre |
| 1 2 3 4 5 6 |
F A R M N A M E |—|---|—|---|—|---|—|
Orville’s Acres |*************** |
Hoffman’s Hills |****************** |
Jiffy Quick Farm |*********** |
Jolly Good Plantation |#** |
Organically Grown Inc. |
|
This is the farm that really m|
***** |

Well that sucked… I hit submit rather than preview.
Graph.txt should have read as"

Jessica M Kleiss CSC141-05 | Production in Hundreds |
Project #6 4/1/2002 | Of Pint Jars Per Acre |
| 1 2 3 4 5 6 |
F A R M N A M E |—|---|—|---|—|---|—| |
Orville’s Acres |*************** |
Hoffman’s Hills |****************** |
Jiffy Quick Farm |*********** |
Jolly Good Plantation |#** |
Organically Grown Inc. |
|
This is the farm that really m|
***** | **

You can disregard all of the “|'s” except for the one on the first line for Orville’s Acres. It was impossible to get them all to line up correctly. For all intents and purposes, just pretend that all of hte “|'s” line up except for the one on the first line for Orville’s Acres which is indented one space to the right.

–DoperChic

Doperchic, you can get text to keep spaces in your post by using the [cod[sub][/sub]e] tag ( marked as a # in the editing buttons if you use those). Writing [co[sub][/sub]de], then any text you want to keep the spaces on, then [/co[sub][/sub]de] will set it off in a code block (it also puts it in a monospace font – see my previous post, and remember – unlike me – to not make your lines too long).

Since this is homework, I won’t tell you the answer directly, but hopefully hint you in the right direction.

As for the indent problem, I’m not sure, but what I am wondering (and it might be related to it), is whether there is supposed to be an extra | after the end of the “F A R M N A M E” line.

To fix the problem with the last farm : If you know how to use a debugger, check the values of acres and jars after they are read in, especially for that line. If you don’t want to use the debugger, you could just print them to cout after you read them in from the file.

Once you’ve done that, look at the farm name in the input and in the output. What happens in between?

You also have another bug in there, but I won’t tell you what it is.

Just kidding. Try adding or changing the values of a farm to 100 acres and 50000 jars, and see what comes out. Consider using an if … then (… else) statement instead of a while when you only want to catch one case.