Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Me! (FUNCTIONS & <FSTREAM) Code is CRAZZYY!! I need help BAD!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have the following program and it's not working. I worte the code all in void main() at first, then I broke it up into different functions. I dont know if i'm not passing the arguments correctly or what, but it's not working. The code reads in the following data file

CPSC 230 01
SPRING 01
6
able 90 90
baker 80 80
charlie 70 70
delta 60 60
fox 59 59
echo 0 0

and outputs the information in corresponding HTML code. It takes the two grades (i.e. 90 90; 80 80) and calculates the average, and returns a letter grade (i.e. 'A', 'B') My code follows below...

********

#include <iostream>
#include <fstream>
#include <string>
using std::ifstream;
using std::eek:fstream;
using namespace std;

// FUNCTION PROTOTYPES
void read_in(ifstream in, char course[13], char semester[10], int numstudents, int gradecalc, int midterm, int final, char grade, char student);

char grade_calc(int gradecalc, char grade);

void output(ifstream in, ofstream out, char course [13], char semester[10], char student[128], int midterm, int final, char grade);

void main()
{
ifstream in(&quot;c:\\info.txt&quot;);
ofstream out(&quot;output.txt&quot;);

char student[128];
char course[13];
char semester[10];
int numstudents;
char grade;
int gradecalc;
int midterm, final;

read_in(in, course, semester, numstudents, gradecalc,
midterm, final, grade, student[128]);
output(in, out, course, semester, student, midterm, final, grade);
}

void read_in(ifstream in, char course[13], char semester[10], int numstudents, int gradecalc, int midterm, int final, int grade, char student[128])
{
in.getline(course,13);
in.getline(semester, 10);
in >> numstudents;

int i=0;

while (i < numstudents){
in >> student;
in >> midterm >> final;
gradecalc = (midterm + final) /2;

grade_calc(gradecalc, grade);
i++;}

}


char grad_calc(int gradecalc, char grade)
{

if(gradecalc >=90)
grade = 'A';
if((gradecalc >=80) && (gradecalc <90))
grade = 'B';
if((gradecalc >=70) && (gradecalc <80))
grade = 'C';
if((gradecalc >= 60) && (gradecalc <70))
grade = 'D';
if(gradecalc <60)
grade = 'F';

return grade;
}

void output(ifstream in, ofstream out, char course[13], char semester[10], char student[128], int midterm, int final, char grade, int numstudents)
{


out << &quot;<HTML>\n&quot; << &quot; <TITLE>&quot; << &quot;GRADE INFORMATION&quot; << &quot;</TITLE>\n&quot;
<< &quot;<BODY>\n&quot; << &quot; <H1>&quot; << course << &quot;, &quot; << semester << &quot;</H1>\n&quot;
<< &quot; <TABLE BORDER=1>\n&quot;;

int i =0;
while (i < numstudents)
{
out << &quot; <TR>\n&quot;;
out << &quot; <TD>&quot; << student << &quot;</TD><TD>&quot; << midterm << &quot;</TD><TD>&quot; << final
<< &quot;</TD><TD>&quot; << grade << &quot;</TD>\n&quot; << &quot; </TR>\n&quot;;
}
}
 
hello,
wow, what a mess. Does the program work before you broke it up? If it does then post that rather this, and we will try to break it up for you.I think it would be faster breaking it up rather debugging this.
 
1. you declare char grade but sometimes you use it as integer , but it is not a problem but I don't suggest to do so because size of integer is system dependent...

2. After the brief look I see eternal loop in your code:
in function void output() you are not advancing i inside the while loop...



Issahar Gourfinkel
senior software engineer
Softwatch LTD
Israel
 
This is my original code *****

#include <iostream>
#include <fstream>
#include <string>
using std::ifstream;
using std::eek:fstream;
using namespace std;

void main()

{

ifstream in(&quot;c:\\info.txt&quot;);
ofstream out(&quot;output.txt&quot;);

char student[128];
char course[13];
char semester[10];
int numstudents;
char grade;
int gradecalc;
int midterm, final;

in.getline(course,13);
in.getline(semester, 10);
in >> numstudents;

out << &quot;<HTML>\n&quot; << &quot; <TITLE>&quot; << &quot;GRADE INFORMATION&quot; << &quot;</TITLE>\n&quot;
<< &quot;<BODY>\n&quot; << &quot; <H1>&quot; << course << &quot;, &quot; << semester << &quot;</H1>\n&quot;
<< &quot; <TABLE BORDER=1>\n&quot;;
int i = 0;

while (i < numstudents)
{
in>>student;
in>>midterm>>final;
gradecalc = (midterm + final) /2;

if(gradecalc >=90)
grade = 'A';
if((gradecalc >=80) && (gradecalc <90))
grade = 'B';
if((gradecalc >=70) && (gradecalc <80))
grade = 'C';
if((gradecalc >= 60) && (gradecalc <70))
grade = 'D';
if(gradecalc <60) grade = 'F';

out << &quot; <TR>\n&quot;;
out << &quot; <TD>&quot; << student << &quot;</TD><TD>&quot; << midterm << &quot;</TD><TD>&quot; << final
<< &quot;</TD><TD>&quot; << grade << &quot;</TD>\n&quot; << &quot; </TR>\n&quot;;
i++;
}
}

 
exactly like I said - you forgot i++;
in the end of the last loop in void output function Issahar Gourfinkel
senior software engineer
Softwatch LTD
Israel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top