loops

using a loop to read data from an external file

Page 1 of 1

1 Replies - 829 Views - Last Post: 09 June 2008 - 11:14 PM Rate Topic: -----

#1 centraspike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-June 08

loops

Posted 09 June 2008 - 10:54 PM

//  : Defines the entry point for the console application.
// A program that computes the G.P.A and course averages of ten students.
// i need a little bit of help here.  i have ten students who took the same type of courses in a given semester. i am using a sentinel controlled loop but i'm unable to align the align the special notes colum to to the GPA and student id.

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
using namespace std;




int _tmain(int argc, _TCHAR* argv[])// function main begins execution
{
	cout << setiosflags(ios::fixed|ios::showpoint);
	cout << setprecision(3);
	
	int idNUM; // student identification nmber
	double course1;// first course taken by student
	double course2; // second course taken by student
	double course3; // third course taken by student
	double course4; // forth course taken by student

	double GPA;
	double averagecourse1; // course 1 average for all students
	double averagecourse2; // course 2 average for all students
	double averagecourse3; // course 3 average for all students
	double averagecourse4; // course 3 average for all students

	

	ifstream infile("c:\\input.txt");    	// input file in hard drive
	if ( ! infile ) 
		{
		cerr << "Input file could not be opened" << endl;
		exit(1);	
		}			// need header file stdlib.h for this function
	ofstream outfile("c:\\output.txt"); // output file in hard drive
	if ( ! outfile ) 
		{
		cerr << "Output file could not be opened" << endl;
		exit(1);
		}
				
			outfile << setiosflags(ios::fixed|ios::showpoint);
			outfile << setprecision(3);

			
			const int SENTINEL = 0000; // sentinel value 
			 infile >> idNUM;

				if (idNUM != SENTINEL)
				 cout << setw(15) << "Student ID ";
				 cout << setw(25) << "G.P.A ";
				 cout << setw(30) << "Special note " << endl << endl;

				 outfile << setw(30) << "Student ID ";
				 outfile << setw(40) << "G.P.A ";
				 outfile << setw(50) << "Special notes "  << endl << endl;

				


			while (idNUM != SENTINEL)
				while (infile >> course1 >> course2 >> course3 >> course4)
				{	
					cout << setw(10) << idNUM;
					outfile << setw(26) << idNUM;
					GPA = (course1 + course2 + course3 + course4)/4;
					setprecision(3);
					cout << setw(29) << GPA << endl << endl;
					outfile << setw(46) << GPA << endl;
				
				//if (GPA >= 3.5)
				//cout << setw(65) << "Honors " << endl;
				///outfile << setw(130) << "Honors " << endl;
			
				
				//if (GPA <= 1.5)
				//cout << setw(65) << "Warning" << endl;
				//outfile << setw(130) << "Warning " << endl;
				
				
				infile >> idNUM;

				}

				{
				const int SENTINEL = 0000;
				infile >> idNUM;
					while (idNUM != SENTINEL);
				
					while (infile >> course1);// >> course2 >> course3 >> course4);
						averagecourse1 = (course1/10);
				cout << setw(30) << "Course 1 Average: " << averagecourse1 << endl;
				outfile << setw(30) << "Course 1 Average: " << averagecourse1 << endl;

						while (infile >> course2);
						averagecourse2 = (course2)/10;
				cout << setw(30) << "Course 2 Average: " << averagecourse2 << endl;
				outfile << setw(30) << "Course 2 Average: " << averagecourse2 << endl;

						while (infile >> course3);
						averagecourse3 = (course3)/10;
				cout << setw(30) << "Course 3 Average: " << averagecourse3 << endl;
				outfile << setw(30) << "Course 3 Average: " << averagecourse3 << endl;

						while (infile >> course4);
						averagecourse4 = (course4)/10;
				cout << setw(30) << "Course 4 Average: " << averagecourse4 << endl;
				outfile << setw(30) << "Course 4 Average: " << averagecourse4 << endl;

				infile >> idNUM;
				}



			system ("PAUSE");

		return 0;
}// function main ends


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 09 June 2008 - 10:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: loops

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: loops

Posted 09 June 2008 - 11:14 PM

The very first thing that I can observe here is the use of curly braces. Please use the braces always when you are using conditional or loop statements. Doesn't matter whether there is single statement under that branch or multiple statements. Using braces to show block of code under particular branch always helps.

Seems that you have if statement if (idNUM != SENTINEL) without braces, just see if it has multiple lines under it's branch. plus you are having a code block in braces [line 90] but it doesn't have any statement [no loop or branch condition?] for it. See if you have misplaced it's statement somewhere else.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1