You are here:

C++/Can't get this program to work please help

Advertisement


Question
QUESTION: This is what I have so far and cannot get it to work any help is appreciated thanks. I also for the life of me cannot figure out the calculateGrade function

Write a program that reads a student's name together with his or her test scores. The program should then compute the average test score for each student and assign the appropriate grade. The grade scale is as follows: 90-100 A, 80-89 B, 70-79 C, 60-69 D, 0-59 F. Your program must use the following functions:

A. A void function, calculateAverage, to determine the average of the five test scores for each student. Use a loop to read and sum the five test scores.(This fucntion does not output the average test score that task must be done in the function main).

B. A value-returning function, calculateGrade, to determine and return each students's grade. (This fucntion does not output the average test score that task must be done in the function main).

Test your program on the following data. Read the data from a file and send the output to a file. Do not use any global variables. Use the appropriate parameters to pass values in and out of functions.

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 98
Sunny 79 85 28 93 82
Smith 85 72 49 75 63

Sample Output:
The output should be of the following form: (Fill the last two columns and the alst line showing the class average

Student Test1 Test2 Test3 Test4 Test5 Average Grade
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 98
Sunny 79 85 28 93 82
Smith 85 72 49 75 63

Class Average=



#include <iostream>
#include <fstream>
#include <string>



using namespace std;

int main () {

float calculateAverage(int[]);
char Grade(float);
char line[100];
int marks[100];
ifstream input;
ofstream output;
clrscr();
input.open("k:\\input.txt");
output.open("k:\\OUTPUT.txt");
if(!output)
cout<<"Output file error";
if (input)
{
while (!input.eof() )
{
input>>line;
cout << line <<endl;
output<<line;
for(int i=1;i<=5;i++)
{
int num;
input>>num;
marks[i]=num;
}
float avg=calculateAverage(marks);
char grd=Grade(avg);
output<<grd<<endl;
}
input.close();
}

else cout << "Unable to open file";
getch();
return 0;
}
float calculateAverage(int marks[])
{
int tot;
float avg;
for(int i=1;i<=5;i++)
{
tot=tot+marks[i];
}
avg=tot/5.0;

return avg;
}
char Grade(float avg)
{
char g;
if((avg<=100)&&(avg>=90))
g='A';
else if((avg<=89)&&(avg>=80))
g='B';

else if((avg<=79)&&(avg>=70))
g='C';
else if((avg<=69)&&(avg>=60))
g='D';
else
g='F';
return g;
}

Sorry when I build it this is the errors that I get also having troubles figuring out how to put the void function in. Thanks

1>c:\users\the smith's\documents\visual studio 2008\projects\651616103\651616103\5151.c... : error C3861: 'clrscr': identifier not found
1>c:\users\the smith's\documents\visual studio 2008\projects\651616103\651616103\5151.c... : error C3861: 'getch': identifier not found


ANSWER: Hi Dear Jerry !

 The error what you have specified is nothing !! , since this error is because the usage of the two functions clrscr() and getch() , both these functions are specific to Turbo C/C++ compiler's perhaps its not at all a ANSI standard function. Microsoft Visual C/C++ doesn't support these functions and that's why its reporting you with error once if you compile!

 A simple solution is just remove the function call clrscr() , and getch() and re-compile your programme , sure you won't get any error's.

Im not clear with your second question >> troubles figuring out how to put the void function in


Note : Upon removal of the function call's clrscr() , getch() will not have any impact on your program logic or the output but a minor effect on how it writs the output over the screen, since you know well clrscr() is used to clear the contents on the screen and getch() is used to freeze the screen until user hits a key !

If you got logical issues please revert me back , ill help you out  !

Thanks and Regards !
Prince M. Premnath

---------- FOLLOW-UP ----------

QUESTION: Hello sir thank you for your reply. I took those 2 things out and then rebuilt it and it succeded but it gave a few warnings.

1>c:\users\the smith's\documents\visual studio 2008\projects\class2\class2\class2.cpp(54) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>c:\users\the smith's\documents\visual studio 2008\projects\class2\class2\class2.cpp(52) : warning C4700: uninitialized local variable 'tot' used

Then when I went to debug it the program comes back with an error stating that tot (total) was not initialized but when I tried to initialize it to float or int I get the same thing. The second question I had was about this

B. A value-returning function, calculateGrade, to determine and return each students's grade. (This fucntion does not output the average test score that task must be done in the function main).

I cannot figure out for the life of me on how to add this into the program

ANSWER: Hi Dear Jerry !

Two small corrections will make your program bug free :-)

Choice 1:
 replace all the float data types to double since because double is the default and not float

and also initialize the variable  
int tot = 0; initializing a variable is must ,because by default it would have some garbage data and those garbage values will persist in all addition which would result in erroneous result.

Choice 2:
 >>avg=tot/5.0;
alter the above statement like this

avg = (float)tot/(float)5.0;

and also initialize the variable  
  int tot = 0;


Select your own choice ( obviously the second is the simplest ) try rebuild your solution. if error comes again don't worry just revert me :-) ( in my machine im not having C++ compiler otherwise i would have corrected all your bugs in a single stretch :-) )


Thanks and Regards !
Prince M. Premnath




---------- FOLLOW-UP ----------

QUESTION: ok sir the new code is below along with the errors. Am i missing something also any idea on how to put the calculateGrade function in that is listed below?

B. A value-returning function, calculateGrade, to determine and return each students's grade. (This fucntion does not output the average test score that task must be done in the function main).

Code
__________________________________________________________
#include <iostream>
#include <fstream>
#include <string>



using namespace std;

int main () {

double calculateAverage(int[]);
char Grade(float);
char line[100];
int marks[100];
ifstream input;
ofstream output;

input.open("k:\\input.txt");
output.open("k:\\OUTPUT.txt");
if(!output)
cout<<"Output file error";
if (input)
{
while (!input.eof() )
{
input>>line;
cout << line <<endl;
output<<line;
for(int i=1;i<=5;i++)
{
int num;
input>>num;
marks[i]=num;
}
float avg=calculateAverage(marks);
char grd=Grade(avg);
output<<grd<<endl;
}
input.close();
}

else cout << "Unable to open file";

return 0;
}
double calculateAverage(int marks[])
{
int tot =0;
double avg;
for(int i=1;i<=5;i++)
{
tot=tot+marks[i];
}
avg = (float)tot/(float)5.0;

return avg;
}
char Grade(int avg)
{
char g;
if((avg<=100)&&(avg>=90))
g='A';
else if((avg<=89)&&(avg>=80))
g='B';

else if((avg<=79)&&(avg>=70))
g='C';
else if((avg<=69)&&(avg>=60))
g='D';
else
g='F';
return g;
}

Errors
___________________________________________________________

1>Source1.cpp
1>c:\users\the smith's\documents\visual studio 2008\projects\yuhgf\yuhgf\source1.cpp(35) : warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Linking...
1>Source1.obj : error LNK2019: unresolved external symbol "char __cdecl Grade(float)" (?Grade@@YADM@Z) referenced in function _main
1>C:\Users\The Smith's\Documents\Visual Studio 2008\Projects\yuhgf\Debug\yuhgf.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\The Smith's\Documents\Visual Studio 2008\Projects\yuhgf\yuhgf\Debug\BuildLog.htm"
1>yuhgf - 2 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========  

Answer
Hai Dear !
 
 You have done some additional corrections that's why you got these many errors !

Check out this programme ive rectified all bugs using what ive said earlier ( using choice 2 ) kindly verify

#include <iostream>
#include <fstream>
#include <string>



using namespace std;

int main () {

  float calculateAverage(int[]);
  char Grade(float);
  char line[100];
  int marks[100];
  ifstream input;
  ofstream output;
  input.open("k:\\input.txt");
  output.open("k:\\OUTPUT.txt");
  if(!output)
     cout<<"Output file error";
  if (input)
  {
     while (!input.eof() )
     {
        input>>line;
        cout << line <<endl;
        output<<line;
        for(int i=1;i<=5;i++)
        {
         int num;
         input>>num;
         marks[i]=num;
        }
        float avg=calculateAverage(marks);
        char grd=Grade(avg);
        output<<grd<<endl;
     }
     input.close();
  }

  
  return 0;
}
float calculateAverage(int marks[])
{
  int tot = 0; // Here is the correction
  float avg;
  for(int i=1;i<=5;i++)
  {
     tot=tot+marks[i];
  }
  avg=tot/(float)5.0; // and here as well !

  return avg;
}
char Grade(float avg)
{
  char g;
  if((avg<=100)&&(avg>=90))
     g='A';
  else if((avg<=89)&&(avg>=80))
     g='B';

  else if((avg<=79)&&(avg>=70))
     g='C';
  else if((avg<=69)&&(avg>=60))
     g='D';
  else
     g='F';
  return g;
}

Thanks and Regards !
Prince M. Premnath

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2016 About.com. All rights reserved.