Hi, I\'m making a program that calculates grade by using Terminal. Here\'s what
ID: 3855739 • Letter: H
Question
Hi, I'm making a program that calculates grade by using Terminal.
Here's what I've got.
--------------------------------------
#include
using namespace std;
const int MAX_CHAR = 100;
const int MIN_A = 90;
const int MIN_B = 80;
const int MIN_C = 70;
double getAvg();
char determineGrade(double);
void printMsg(char grade);
int main()
{
double score;
char grade;
score = getAvg();
grade = determinGrade(score);
printMsg(grade);
return 0;
}
double getAvg()
{
double finalAvg;
cout << "Please enter your final average: ";
cin >> finalAvg;
while(!cin)
{
cin.clear();
cin.ignore(MAX_CHAR, ' ');
cout << "input has to be numerical!" << endl;
cin >> finalAvg;
}
cin.ignore(MAX_CHAR, ' ');
return finalAvg;
}
char determinGrade(double finalAvg)
{
char grade = 'F';
if(finalAvg >= MIN_A)
{
grade = 'A';
}
else if(finalAvg >= MIN_B)
{
grade = 'B';
}
else if(finalAvg >= MIN_C);
{
grade = 'C';
}
return grade;
}
void printMsg(char grade)
{
char msg[MAX_CHAR];
switch(grade)
{
case 'A':
strcpy(msg, "Excellent!");
break;
case 'B':
strcpy(msg, "Good job!");
break;
case 'C':
strcpy(msg, "You've passed!");
break;
default:
strcpy(msg, "Need to put down that video game!");
break;
}
cout << msg << endl;
}
------------------------------------------
And I typed the command "g++ -Wall -g app.cpp -o Lab2" to check if it works well, then I found some error.
It says,
-----------------------------------------------
app.cpp: In function ‘int main()’:
app.cpp:19:29: error: ‘determinGrade’ was not declared in this scope
grade = determinGrade(score);
^
app.cpp: In function ‘char determinGrade(double)’:
app.cpp:52:7: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
else if(finalAvg >= MIN_C);
^~
app.cpp:53:2: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’
{
^
app.cpp: In function ‘void printMsg(char)’:
app.cpp:65:28: error: ‘strcpy’ was not declared in this scope
strcpy(msg, "Excellent!");
^
--------------------------------------------
Can you list all of bugs, both syntax and semantic, that you have found and fixed?
you need to use gdb to find the semantic bug as well.
Thanks.
Explanation / Answer
Below are the bugs I found: -
1. #include on top needs name of header file . Changed it to #include<iostream>
2. Spelling of function determineGrade in declaration above is wrong. corrected it.
3. For strcpy, we need to include cstring header file. included it.
4. there was a semi colon after 3rd if clause in determine grade function. which is actually a semantic error. removed it.
Below is the working code.. Let me know in comments if you found more issues.
#include<iostream>
#include<cstring>
using namespace std;
const int MAX_CHAR = 100;
const int MIN_A = 90;
const int MIN_B = 80;
const int MIN_C = 70;
double getAvg();
char determinGrade(double);
void printMsg(char grade);
int main()
{
double score;
char grade;
score = getAvg();
grade = determinGrade(score);
printMsg(grade);
return 0;
}
double getAvg()
{
double finalAvg;
cout << "Please enter your final average: ";
cin >> finalAvg;
while(!cin)
{
cin.clear();
cin.ignore(MAX_CHAR, ' ');
cout << "input has to be numerical!" << endl;
cin >> finalAvg;
}
cin.ignore(MAX_CHAR, ' ');
return finalAvg;
}
char determinGrade(double finalAvg)
{
char grade = 'F';
if(finalAvg >= MIN_A)
{
grade = 'A';
}
else if(finalAvg >= MIN_B)
{
grade = 'B';
}
else if(finalAvg >= MIN_C)
{
grade = 'C';
}
return grade;
}
void printMsg(char grade)
{
char msg[MAX_CHAR];
switch(grade)
{
case 'A':
strcpy(msg, "Excellent!");
break;
case 'B':
strcpy(msg, "Good job!");
break;
case 'C':
strcpy(msg, "You've passed!");
break;
default:
strcpy(msg, "Need to put down that video game!");
break;
}
cout << msg << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.