1 error in my program please help

Sep 19, 2012 at 10:37pm
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>

using namespace std;

double math(int test1, int test2, int test3, int avg){
    avg= (test1+test2+test3)/3.0;
}
void output (double avg1, char lg){
    cout<<"Your average is:"<< avg1;
}

void input(int &t1, int &t2, int &t3){
  cout<<"Enter test1"<<endl;
    cin>>t1;
   cout<<"Enter test2"<<endl;
    cin>>t2;
    cout<<"Enter test3"<<endl;
    cin>>t3;
}

{char {lettergrade (double average)};{

if (average>=93);{
    return (A);

else if (85<= average)&&(average <93);{
    return (B);
}

else if (76<= average)&&(average <85);{
    return (C);
}

else if(70<= average)&&(average <76);{
    return (D);
}
else (average<=69)&&(average<= 70);{
    return (F);


}

}

int main()
{
    int grade1;
    int grade2;
    int grade3;
    double average;
    char let;
    cout<<"Enter grade1:"<<endl;
    cin>>grade1;
    cout<<"Enter grade2:"<<endl;
    cin>>grade2;
    cout<<"Enter grade3:"<<endl;
    cin>>grade3;
    math(grade1, grade2, grade3, average);
    output (average);

return 0;{}
}


Compiling: C:\Users\KILL\Downloads\avg2.cpp
C:\Users\KILL\Downloads\avg2.cpp:21: error: expected unqualified-id before '{' token
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings


program is suppose to calculate the average from 3 test and give that average as a number and a letter grade.
Sep 19, 2012 at 11:05pm
You have a lot more than just one error. I fixed all the compiler errors in the following code, but you still have some logic errors.

Errors I fixed:
Semicolons after every if and else if
Else does not have any condition that needs to go along with it
Conditions after if and else if need to be surrounded by parentheses
( http://cplusplus.com/doc/tutorial/control/ )
char lettergrade on line 21 had extra of '{', '}', and ';'
double math on lines 5-7 does not return a double
double average needed typecasting on the integers to insure the answer returned was a double:
http://www.cplusplus.com/doc/tutorial/typecasting/

Logic errors you will still need to fix:
lettergrade function has some '<=' that might should be '>='

Suggestions:
Lines 52-57 could be replaced with input(grade1, grade2, grade3);
double math only needs the grades passed to it (I went ahead and did this suggestion for you)
void output does not need char lg passed since it is not used in the function

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;

double math(int test1, int test2, int test3){
    return double(test1+test2+test3)/3.0;
}
void output (double avg1, char lg){
    cout<<"Your average is:"<< avg1;
}

void input(int &t1, int &t2, int &t3){
    cout<<"Enter test1"<<endl;
    cin>>t1;
    cout<<"Enter test2"<<endl;
    cin>>t2;
    cout<<"Enter test3"<<endl;
    cin>>t3;
}

char lettergrade (double average)
{
    if (average>=93){
        return 'A';
    }
    else if ((85<= average)&&(average <93)){
        return 'B';
    }
    else if ((76<= average)&&(average <85)){
        return 'C';
    }
    else if ((70<= average)&&(average <76)){
        return 'D';
    }
    else if ((average<=69)&&(average<= 70)){
        return 'F';
    }
}

int main()
{
    int grade1;
    int grade2;
    int grade3;
    double average;
    char let;
    cout<<"Enter grade1:"<<endl;
    cin>>grade1;
    cout<<"Enter grade2:"<<endl;
    cin>>grade2;
    cout<<"Enter grade3:"<<endl;
    cin>>grade3;
    average = math(grade1, grade2, grade3);
    let = lettergrade(average);
    output (average, let);
    return 0;
}
Last edited on Sep 20, 2012 at 3:48am
Sep 20, 2012 at 3:15am
void output (double avg1, char lg)
i think, you don't need lg

return double(test1+test2+test3)/3.0;
shouldn't it be like this?
return (double) (test1+test2+test3)/3.0;

CMIIW
Sep 20, 2012 at 3:44am
I think both cast the same way in that situation? I just prefer the functional style casting.
In my programs lately I have been doing static_cast<type>(expr) as of lately; however I find that new things of that nature can tend to confuse new people.
Also thanks for mentioning that because I forgot to include that in my post earlier.

I also think he might want to output the letter version of the grade in his output function?
I do agree at the moment he does not need it since it is not used in the function.
Last edited on Sep 20, 2012 at 3:49am
Sep 20, 2012 at 4:12am
i'm just wondering because i never see that kind of explicit conversion, it used to be like:
(type) variable / expression;
Sep 20, 2012 at 9:04pm
I got it all working and received a 100 on my program, thanks for the help guys.
Topic archived. No new replies allowed.