8 Replies - 603 Views - Last Post: 09 September 2010 - 03:02 PM Rate Topic: -----

#1 mick0311  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-January 10

Dealing with whitespace in text file

Posted 09 September 2010 - 01:53 PM

Hello big brains,

I am working on a project that calculates GPA for a set of students. The program will pull the students initials and the following grades. The format of the text document is this (initials followed by space, followed by grades):

HYS AABCCA
TYP BBBAAC

The issue I am having is in my switch statement I am using to convert the letter grades to a point value, ie... A = 4 points etc..

I have a feeling that the switch is pulling in the white space after the initials, so I keep getting the default error message at the bottom of the switch. Thanks in advance folks, here is my code. Be easy one me, I am but a rookie.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

static int gradeA = 4;
static int gradeB = 3;
static int gradeC = 2;
static int gradeD = 1;
static int gradeF = 0;

void process (ifstream&);


int main ()
{
	ifstream gradeDoc;  //stream variable
	
	gradeDoc.open ("GRADES.txt"); //opens text file

	if (!gradeDoc)  // checks to see if file is open
	{
		cout << endl << endl;
		cout << "File was not found!" << endl;
		cout << "Please check if the file exists and run the program again." << endl;
		cout << endl;
		system ("Pause");
	}
	else
	{
		process(gradeDoc); //continues program
	}

	gradeDoc.close(); //closes file
	return 0;
	system ("Pause");
}


//******************************************************************************
void process (ifstream &gradeDoc)
{
	char peeker; //watches for EOL 
	string student; //stores initials of current student in loop
	char letter; //current letter grade
	int gpa = 0, iGradeCounter = 0, iStudentCounter = 0, gradeNumber = 0; 
	double sum = 0;

	while (!gradeDoc.eof())
	{
        
	  gradeDoc >> student;
		peeker = gradeDoc.peek();
 	  
 	  
		do
		{
			gradeDoc >> letter;
			iGradeCounter++;
      			
			switch (letter)
			{
				case 'A':
					gradeNumber += gradeA;
					break;
				case 'B':
					gradeNumber += gradeB;
					break;
				case 'C':
					gradeNumber += gradeC;
					break;
				case 'D':
					gradeNumber += gradeD;
					break;
				case 'F':
					gradeNumber += gradeF;
					break;
				default:
					cout << "Ensure letter grades are capital letters in text file." << endl;
					break;
			}  //End switch
    
		} while (peeker != '\n');//End of Do/While (EOL)
		
		sum += (gradeNumber / iGradeCounter);
		iStudentCounter++;
		
		cout << student << " " << (gradeNumber / iGradeCounter);
	}//End While (gradeDOc)
	
	cout << "the overall average gpa is: " << (sum / iStudentCounter);
	
	system ("Pause");
}




Is This A Good Question/Topic? 0
  • +

Replies To: Dealing with whitespace in text file

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Dealing with whitespace in text file

Posted 09 September 2010 - 02:05 PM

add one for white space that dose nothing
Was This Post Helpful? 0
  • +
  • -

#3 mick0311  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-January 10

Re: Dealing with whitespace in text file

Posted 09 September 2010 - 02:10 PM

Yeah, I tried doing that, but I keep getting the same result. Absolutely clueless as to what is wrong here, I thought I had a good plan on this one too :( Never fall in love with a plan I guess. Anyone else see anything or have suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Dealing with whitespace in text file

Posted 09 September 2010 - 02:17 PM

I would suggest using getline() and parsing the result.

This post has been edited by JackOfAllTrades: 09 September 2010 - 02:18 PM

Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Dealing with whitespace in text file

Posted 09 September 2010 - 02:25 PM

Your program logic is utterly broken. Your code implementation is further broken. Hence, a disaster. Furthermore, you seem to write programs like essays. Entire thing in one go, then revise. Terrible idea. Build programs like houses. Check that your foundation works before putting up the walls. That is, build it up in iterations. You should never have been writing code to calculate the GPA when you don't have code to read from the file properly.

You said you had a plan. I take it you mean you have plain English logic written down. Show it to us.
Was This Post Helpful? 0
  • +
  • -

#6 mick0311  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-January 10

Re: Dealing with whitespace in text file

Posted 09 September 2010 - 02:26 PM

Jack,
That sounds like a good idea, but wouldn't I have to know the length of the line beforehand? The letter grades given after each name are unknown in length. I like the idea, but I'm not sure how to implement it, and I'm trying to use the tools I have been taught up to this point. Could you give me a generic example of what you are talking about? Now I'm curious. Am I correct in thinking that the white space is giving me trouble with the switch. Good Lord, I didn't think something could be fun and frustrating at the same time :)
Was This Post Helpful? 0
  • +
  • -

#7 mick0311  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-January 10

Re: Dealing with whitespace in text file

Posted 09 September 2010 - 02:33 PM

View PostOler1s, on 09 September 2010 - 01:25 PM, said:

Your program logic is utterly broken. Your code implementation is further broken. Hence, a disaster. Furthermore, you seem to write programs like essays. Entire thing in one go, then revise. Terrible idea. Build programs like houses. Check that your foundation works before putting up the walls. That is, build it up in iterations. You should never have been writing code to calculate the GPA when you don't have code to read from the file properly.

You said you had a plan. I take it you mean you have plain English logic written down. Show it to us.



As far as I can tell, the reading from the file is working just fine. I put a cout check on the variables at key points to make sure I am gathering the proper info from the file. For example, when I place a cout << student << " " << peeker; right before the do loop containing the switch, I am gathering the right initials and the whitespace is where the pointer is. That is my problem. I'm sure my logic is not perfect, but it works as planned up to the whitespace, which leads me back to my original issue of handling the whitespace. I have my pseudo-code on scratch paper in front of me (it is easier to erase that way ;)
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Dealing with whitespace in text file

Posted 09 September 2010 - 02:47 PM

Quote

As far as I can tell, the reading from the file is working just fine.
Run your program and say that again. Here's a sanity check. Do you see the do/while loop where you have peeker != '\n'? Tell me where you change the value of peeker in that loop.

.... You don't. Not only does that break your file reading algorithm, since peeker never changes, you've got an infinite loop. You consider this reading from the file just fine?

Look, your logic is broken. Here's the relevant part of your code:

    while (!gradeDoc.eof())
    {
        
      gradeDoc >> student;
        peeker = gradeDoc.peek();
      
      
        do
        {
            gradeDoc >> letter;
            iGradeCounter++;
                
            switch (letter)
            {
                case 'A':
                    gradeNumber += gradeA;
                    break;
                case 'B':
                    gradeNumber += gradeB;
                    break;
                case 'C':
                    gradeNumber += gradeC;
                    break;
                case 'D':
                    gradeNumber += gradeD;
                    break;
                case 'F':
                    gradeNumber += gradeF;
                    break;
                default:
                    cout << "Ensure letter grades are capital letters in text file." << endl;
                    break;
            }  //End switch
    
        } while (peeker != '\n');//End of Do/While (EOL)



Let me translate this to English. Then you tell me if it even makes sense:

Assume we have numbers gradeNumber, gradeA, gradeB...gradeF
while a previous attempt to read from the file did not encounter the end of file:
    Read a string, skipping leading whitespace, store that as student
    Take a look at what the next character is, and store that as peeker
    Do the following:
        Read a letter, store as letter
        Increment the gradecounter
        Do the following based on letter:
            'A': add gradeA to gradeNumber
            'B','C'..'F': similar
            otherwise: show an error message
        Repeat, unless peeker is a newline
    <snip>



Does this look right? Yes? Take a piece of paper and follow this above recipe. Let us know if it makes sense to you.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Dealing with whitespace in text file

Posted 09 September 2010 - 03:02 PM

View Postmick0311, on 09 September 2010 - 03:26 PM, said:

Jack,
That sounds like a good idea, but wouldn't I have to know the length of the line beforehand? The letter grades given after each name are unknown in length. I like the idea, but I'm not sure how to implement it, and I'm trying to use the tools I have been taught up to this point. Could you give me a generic example of what you are talking about? Now I'm curious. Am I correct in thinking that the white space is giving me trouble with the switch. Good Lord, I didn't think something could be fun and frustrating at the same time :)


If you're using C++ strings, then you don't need to know the string size before you use getline. But if you need to use what you've learned that probably does not include stringstreams. It's probably overkill in this case anyway.

You have two strings per line. Read directly into each string and operate on the second string as necessary (of what is a string made up?).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1