Finding errors in code

I'm doing a practice problem and I'm told there are 4 syntax errors and 2 logical errors in the code below, starting below the body of the main. I labelled what I think I should fix, but I want to make sure it's right. I also don't know how to fix part of the code without re-writing it myself., and I only found one logical error.

Edit:
Also not sure what the . in the line below means. It says "0." but I already found 4 other syntax errors, and it's compiling anyways.
while(theMark<=0. &&theMark>=maxMark){


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Example program
#include <iostream>
#include <string>
using namespace std;
int grade(double test, double exam);
double getMark(double maxMark);

int main()
{
 double midterm;
 double final;
 cout<<endl<<"please input the midTerm mark[0,30.0]:";
 midterm=getMark(30.0);
 cout<<endl<<"and the final mark [0,50.0]:";
 final=getMark(50.0);
 cout<<"This student got "<<grade(midterm,final);
 cout<<"% in the course";
 return 0;
}
/* errors start here */
int grade(double test, double exam){
        double mark=test+70*exam/50;
       Return (mark+.5)  /// Two syntax errors. no ; and Return should be return.
}
        double getMark(maxMark){ // One syntax error, no double maxMark.
            double theMark;
            
            while(theMark>=0. &&theMark<=maxMark){ // Possible logic error, grades should only say out of range if they are under 0, or above maxMark. Tried fixing but code won't compile.
            
            cout<<endl<<"that's out of range.";
            
            cout<<"please input a mark on [0, "<<maxMark<<"]:";
            
            cin<< theMark; // One syntax error. cin should be cin>> not cin<<.
}
return theMark;
}





Partially fixed code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Example program
#include <iostream>
#include <string>
using namespace std;
int grade(double test, double exam);
double getMark(double maxMark);

int main()
{
 double midterm;
 double final;
 cout<<endl<<"please input the midTerm mark[0,30.0]:";
 midterm=getMark(30.0);
 cout<<endl<<"and the final mark [0,50.0]:";
 final=getMark(50.0);
 cout<<"This student got "<<grade(midterm,final);
 cout<<"% in the course";
 return 0;
}
/* errors start here */
int grade(double test, double exam){
        double mark=test+70*exam/50;
       return (mark+.5);
}
        double getMark(double maxMark){
            double theMark;
            
            while(theMark<=0. &&theMark>=maxMark){
            
            cout<<endl<<"that's out of range.";
            
            cout<<"please input a mark on [0, "<<maxMark<<"]:";
            
            cin>> theMark;
}
return theMark;
} 

Last edited on
Not sure if it's just a copy and paste error into the forum post, but your partially corrected code has an extra return statement and closing brace at the very end (lines 44-45).


The user doesn't have any opportunity to enter a grade before you try to compare theMark (which has some garbage uninitialize value) here.

1
2
3
4
5
double getMark(double maxMark)
{
            double theMark;
            
            while(theMark<=0. &&theMark>=maxMark)
> Also not sure what the . in the line below means.
So the number is treated as double


> and I only found one logical error.
gcc fails to notice, but clang says
foo.cpp:26:9: warning: variable 'theMark' is used uninitialized whenever function 'getMark' is called
(always compile with warnings enabled)


> Possible logic error, grades should only say out of range if they are under 0, or above maxMark.
> Tried fixing but code won't compile
your catch is correct, you may also notice that the condition would never be fulfilled.
If your "fix" is giving you issues, show that code and the errors that generates.


PS: you should also fix the indentation
Yes that was an error I just removed it, thank you.

I tried moving the cin<< above the while loop, but my problem is the code will never enter the while loop, and now it also accepts values outside of the range. I could fix it if I re-wrote the code, but based on how the question was worded there should be something easier to fix?

Here's my updated code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Example program
#include <iostream>
#include <string>
using namespace std;
int grade(double test, double exam);
double getMark(double maxMark);

int main()
{
 double midterm;
 double final;
 cout<<endl<<"please input the midTerm mark[0,30.0]:";
 midterm=getMark(30.0);
 cout<<endl<<"and the final mark [0,50.0]:";
 final=getMark(50.0);
 cout<<"This student got "<<grade(midterm,final);
 cout<<"% in the course";
 return 0;
}
/* errors start here */
int grade(double test, double exam){
double mark=test+70*exam/50;
return (mark+.5);
}
double getMark(double maxMark){
double theMark;
cin>> theMark;

while(theMark<=0 &&theMark>=maxMark){
            
cout<<endl<<"that's out of range.";
            
cout<<"please input a mark on [0, "<<maxMark<<"]:";
            
}
return theMark;
} 
Last edited on
Since you're using the logical AND operator &&, both conditions must be true for the while contents to execute. But a number is never going to be both less than zero and greater than whatever max grade is possible. If only one of those conditions needs to be true, try using the logical OR operator || instead. But you'll also need to give the user an opportunity to re-input the grade in the while loop, otherwise you'll have an infinite loop.


1
2
3
4
5
6
7
8
9
while(theMark<=0 && theMark>=maxMark){
            
cout<<endl<<"that's out of range.";
            
cout<<"please input a mark on [0, "<<maxMark<<"]:";
      
//allow re-input      

}
So, wouldn't I technically have three logical errors? I had to put a cin<<myMark;, I had to change and to or, and I also had to change the <= and >=.

Original code:
1
2
3
4
5
6
7
8
9
10
  double getMark(double maxMark){
            double theMark;
            
            while(theMark>=0. &&theMark<=maxMark){
            
            cout<<endl<<"that's out of range.";
            
            cout<<"please input a mark on [0, "<<maxMark<<"]:";
            
            cin>> theMark;


Edited code:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Example program
#include <iostream>
#include <string>
using namespace std;
int grade(double test, double exam);
double getMark(double maxMark);

int main()
{
 double midterm;
 double final;
 cout<<endl<<"please input the midTerm mark[0,30.0]:";
 midterm=getMark(30.0);
 cout<<endl<<"and the final mark [0,50.0]:";
 final=getMark(50.0);
 cout<<"This student got "<<grade(midterm,final);
 cout<<"% in the course";
 return 0;
}
/* errors start here */
int grade(double test, double exam){
double mark=test+70*exam/50;
return (mark+.5);
}
double getMark(double maxMark){
double theMark;

cin>>theMark;
while(theMark<0 ||theMark>maxMark){
            
cout<<endl<<"that's out of range.";
            
cout<<"please input a mark on [0, "<<maxMark<<"]:";
cin>> theMark;
            
}
return theMark;
} 
Last edited on
Topic archived. No new replies allowed.