4 Replies - 3865 Views - Last Post: 11 October 2011 - 02:12 PM Rate Topic: -----

#1 elpach  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-October 11

While Loop with Input File writing to Output File

Posted 11 October 2011 - 11:46 AM

Hello! I have been looking around for what seems like ages for the answer to this problem (which I'm almost positive is a fairly easy solution). Apologies if it has been answered before, but I cannot find it.

Okay, so I have this program that is supposed to take an input file of 10 different student information (name, ssn, grades), compute a weighted grade and a letter grade, and finally stick it all into an output file. I have done that much. I'm having trouble with the loop. My professor insists that we use a while loop, which after all my reading and whatnot does not seem to be the best way but oh well.

My problem is simply getting the loop to repeat the program after the first block of input. The input file goes something like:
John Doe
393-39-2822
90.93
92.39
93.39

Where the first set of numbers is a Social Security number, and the last three sets are his grades. The input file contains 10 of these blocks of input. When I run the program with just one input, it works fine. So clearly I have trouble with multiple input.

I have tried to fix it, and right now I'm pretty sure the problem lies in my using the getline function for the name and SSN. When I run the program as is, it will display the first perfectly but give blanks for the next name and SSN, while repeating the numbers from the first. I think it has something to do with the buffer, but I'm really unsure.

Right now I'm using a variable 'x' that increases by 1 every time up until 10 to terminate the loop. It does work, as I get 10 messed up outputs!

Perusing here really helped me out with this program (especially the bit on eof!) but I can't for the life of me figure this out. Thanks in advance for helping out a newbie!

The code in question (apologies for the excessive comments, my professor requires that many, if not more):

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

int main()
{
	// Define variables for the three test scores and the weighted numeric grade.
	double grade1, grade2, grade3, numericGrade;
	const int SIZE = 81;

	// Define variables to compute the numeric grade. scale1 is for the first two tests,
	// scale2 is for the final exam, and totalPoints is the total points allowed for the test.
	int scale1 = 25, scale2 = 50, totalPoints = 100;

	// Define variables for the name, social security number, and letter grade.
	char name[SIZE], ssn[SIZE], letterGrade;
	int x = 0; // for the while loop
	// Define and open the input file. Input file uncreatively named "inputfile.txt"
	ifstream inFile;
	inFile.open("inputfile.txt");

	// Tests for file open errors.
	if (!inFile)
	{
		cout << "Error opening file.\n";
	}

	// Tells the user that something is happening.
	cout << "Reading data from the file.\n\n";

	while (x < 10)
	{
		x++; //increments x until it terminates the loop.

		// Formats output to display a fixed floating point number with 3 decimal places.
		cout << setprecision(3) << fixed;

		// Reads the input file and assigns each line to a variable, then prints them to the console.
		inFile.getline (name, SIZE);
		cout << left << setw(26) << "Name of Student: " << setw(16) << name << endl;

		inFile.getline (ssn, SIZE);
		cout << setw(26) << "Student Soc. Security #: " << setw(16) << ssn << endl;

		inFile >> grade1;
		cout << setw(26) << "Test 1: " << setw(16) << grade1 << endl;

		inFile >> grade2;
		cout << setw(26) << "Test 2: " << setw(16) << grade2 << endl;

		inFile >> grade3;
		cout << setw(26) << "Final Exam: " << setw(16) << grade3 << endl;

		// Computes the numeric grade based on the provided scale.
		numericGrade = (grade1 / totalPoints * scale1) + (grade2 / totalPoints * scale1) + (grade3 / totalPoints * scale2);
		// Prints numeric grade to the console.
		cout << setw(26) << "Numeric Grade: " << setw(16) << numericGrade << endl;

		// Using if/else if statements, determines the letter grade from the numeric grade.
		if (numericGrade < 60)
		{
			letterGrade = 'F';
		}
		else
		{
			if (numericGrade < 70)
			{
				letterGrade = 'D';
			}
			else
			{
				if (numericGrade < 80)
				{
					letterGrade = 'C';
				}
				else
				{
					if (numericGrade < 90)
					{
						letterGrade = 'B';
					}
					else
					{
						if (numericGrade <= 100)
						{
							letterGrade = 'A';
						}
						else // Tells the user if a number in the input file was incorrect.
						{
							cout << "\nThe maximum score you can get is 100. Please check the input.\n";
							cout << "All grade numbers must be 100 or less and cannot be negative.";
						}

					}
				}
			}
		}

		// Prints the letter grade to the console.
		cout << setw(26) << "Letter Grade:" << setw(16) << letterGrade;


		// Close the input file.
		inFile.close();

		// Create an output file if it does not exist. Append to it if it does.
		ofstream outFile;
		outFile.open("outputfileA.txt", ios::app);


		// Prints all data to the file.
		outFile << setprecision(3) << fixed;
		outFile << setw(26) << "Name of Student: " << setw(16) << name;
		outFile << setw(26) << "Student Soc. Security #: " << setw(16) << ssn;
		outFile << setw(26) << "Test 1: " << setw(16) << grade1 << endl;
		outFile << setw(26) << "Test 2: " << setw(16) << grade2 << endl;
		outFile << setw(26) << "Final Exam: " << setw(16) << grade3 << endl;
		outFile << setw(26) << "Numeric Grade: " << setw(16) << numericGrade << endl;
		outFile << setw(26) << "Letter Grade:" << setw(16) << letterGrade;
		outFile << "\n----------------------------------\n";
	}

	return 0;
}



Also, a quick note: I've noticed that much of the advice around here deals in things I do not yet understand. What you see here is the most I know. Please try to "dumb down" for me! I'm trying to learn!

Is This A Good Question/Topic? 0
  • +

Replies To: While Loop with Input File writing to Output File

#2 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1676
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: While Loop with Input File writing to Output File

Posted 11 October 2011 - 01:15 PM

The problem is that getline() and insert >> do not play nicely together.

>> leaves white space (including newlines) on the input stream. Unfortunately, a newline is also getline()'s early exit from doing any more work.

inFile.getline (ssn, SIZE);
inFile >> grade1;
infile.ignore();  // burn one character, hopefully a newline


http://en.cppreferen..._istream/ignore

The better long-term answer is to read ALL input using getline(), then extract what you need from the buffer in memory.
Was This Post Helpful? 1
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




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

Re: While Loop with Input File writing to Output File

Posted 11 October 2011 - 01:16 PM

your reopening the file(for output) on every loop, create your file handles outside the loop. your issue is mentioned above by Salem.

This post has been edited by ishkabible: 11 October 2011 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,613
  • Joined: 19-February 09

Re: While Loop with Input File writing to Output File

Posted 11 October 2011 - 01:57 PM

You could use the whitespace manipulator ws to extract whitespace, only needed once, at the end of all the reads possibly.

052	        inFile >> grade3 >> ws;


Was This Post Helpful? 0
  • +
  • -

#5 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: While Loop with Input File writing to Output File

Posted 11 October 2011 - 02:12 PM

Quote

The code in question (apologies for the excessive comments, my professor requires that many, if not more):


No need to apologize. There's no such thing as too many comments.

This post has been edited by Yatta!: 11 October 2011 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1