Help with creating a loop?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 3738 Views - Last Post: 27 March 2012 - 12:46 PM Rate Topic: -----

#31 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Help with creating a loop?

Posted 26 March 2012 - 04:25 AM

What do you mean "we haven't learned temp yet"? It's just another variable named "temp". Name it whatever you like. Or don't declare another variable at all, and just use grade.

If you don't want to print the grades and hours, remove the cout statements that are printing the grades and hours.

Frankly, I don't think the loop is your problem. You don't seem to grasp the concepts of inputting a value and then deciding what to do with it: to save it, or print it, or ignore it.
Was This Post Helpful? 0
  • +
  • -

#32 TommehTGOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 25-March 12

Re: Help with creating a loop?

Posted 26 March 2012 - 04:28 AM

shoot...my fault.....gonna fix it today...i'll let you know if there's any other problems
sorry for the confusion
Was This Post Helpful? 0
  • +
  • -

#33 TommehTGOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 25-March 12

Re: Help with creating a loop?

Posted 26 March 2012 - 10:51 AM

ok.....after trying #define's code:
while (!infile.eof())
{
	infile>>ID;
	while (!infile.eof())
	{
		infile>> grade>> crhrs;
	}
	OutputFile<<setw(15)<<ID<<setw(18)<<"Comin Soon"<<endl;
	}
        infile>>ID;
	OutputFile.close();
	infile.close();
	return 0; 
}


In the output, only the first ID is showing up...how can i get the rest to show up?? Thanks! Anything wrong I'm doing?
Was This Post Helpful? 0
  • +
  • -

#34 #define  Icon User is offline

  • Duke of Err
  • member icon

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

Re: Help with creating a loop?

Posted 26 March 2012 - 10:29 PM

The inner loop should read until the end of the line. You need to check for the newline character(s) and break from the loop if found, as discussed in some previous posts.
Was This Post Helpful? 0
  • +
  • -

#35 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Help with creating a loop?

Posted 27 March 2012 - 10:30 AM

Here is a tutorial on file input with sentinels which I hope will clear up your confusion.
Was This Post Helpful? 0
  • +
  • -

#36 TommehTGOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 25-March 12

Re: Help with creating a loop?

Posted 27 March 2012 - 12:46 PM

This is what I achieved (was trying to)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <stdlib.h>
using namespace std;

//function prototypes go here//

void printfile(ofstream &OutputFile);
double findPoints(int grade, double ch);
double findHours(double ch);
double findGPA(double points, double hours);
int main()
{
	double gpa, points, hours;
	int ID;
	int grade;
	double ch;
	
	ofstream OutputFile;
	OutputFile.open("Output.dat");

	ifstream infile;
	infile.open("Input.dat");

	printfile(OutputFile);

OutputFile<<fixed<<showpoint<<setprecision(1)<<endl;

infile>>ID;    //Reads in ID by itself
while (!infile.eof())
{
	//initialize variables
	ch=0;
	points=0;
	grade=0;
	hours=0;

infile>>grade>>ch;  //Reads in grade and credit hours
while(ch >=0)  //When flags or sentinel values are read, the loop terminates
{
	points = points + findPoints(grade, ch);
	hours = hours + ch;
	gpa = findGPA(points, hours);
	infile>>grade>>ch;  //reads the next grade and credit hours
}
OutputFile<<setw(15)<<ID<<setw(18)<<gpa<<endl;

infile>>ID;  //reads the next ID
}
	return 0; 

	OutputFile.close();
	infile.close();
}
//Function Definitions Go Here
void printfile(ofstream &OutputFile)
{
	OutputFile<<setw(30)<<"Thomas Powe"<<endl;
	OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
	OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
return;
}

double findPoints(int grade, double ch)
{
	double points;
	points=0;
	points = points + grade * ch;
	return points;
}

double findHours(double ch)
{
	double hours;
	hours=0;
	hours = ch +ch;
	return hours;
}
double findGPA(double points, double hours)
{
	double gpa;
	gpa= points/hours;
	return gpa;
}



Here is the input
30713 4 3 3 4 3 3 2 3 4 3 -9 -9 
21356 4 4 4 3 3 3 2 3 2 3 -9 -9
21265 4 1.5 3 3 3 0.5 2 3 3 3 -9 -9
19364 4 3 3 3 -9 -9
19335 0 3 0 4 2 3 3 1.5 -9 -9 
18264 2 3 3 0.5 3 0.5 3 3 2 3 -9 -9
20135 4 1 3 4 3 1.5 0 3 -9 -9
22185 4 3 4 4 4 3 3 3 4 3 -99 -99



And here's the output
           Thomas Powe
                     03-27-12

             ID               GPA


          30713               3.2
          21356               3.1
          21265               2.9
          19364               3.5
          19335               0.9
          18264               2.4
          20135               2.2
          22185               3.8



So overall, as you see in the main code how I read the ID by itself and then the grades and credit hours together. Then in my input file, where you see the -99's, those were flags or sentinel values I was using.. That way in my while (ch >=0) loop, when it ran and the -99 was read, the loop terminates and goes to read the next ID. So yeah..that's why I kept asking is there an easy way of doing. #define you were super close though! Thanks both of you guys for your help!! :gunsmilie:
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3