im trying to practice on: Basic User Defined Functions/ Pass by Value Functions for class and im confused. this code has a problem with it, its not printing everyting i want it to say. can someone help me understand this and what i am doing what wrong and what i need to do ?

enter number of questions:
number of questions correct:
grade:

#include<iostream>
using namespace std;
double calculateGrade(double grade);
int enterTotalQuestions(int tot_quest);
int main()
    {
        int quest_corr;
        cout << "how many questions are correct " ;
        cin >> quest_corr;
        int enterTotalQuestions(int tot_quest);
        double calculateGrade(double grade);
return 0;
    }
double calculateGrade(double grade)
{
    int quest_corr, tot_quest;
    grade = quest_corr/tot_quest;
    cout << grade;
return grade;
}
int enterTotalQuestions(int tot_quest)
{
   cout << "total number of questios: " ;
   cin >> tot_quest;
   return tot_quest;
}

Recommended Answers

All 9 Replies

You don't even need to pass in grade or tot_quest into your functions if you are getting their values within the function. You can just define them within the function (if you don't need them anywhere else in the program.)

double calculateGrade(double grade)
{
    int quest_corr, tot_quest;
    grade = quest_corr/tot_quest;
    cout << grade;
return grade;
}

This will not give you the correct answer because you define quest_corr and tot_quest here, but do not give them a value. Then you use them in a calculation. I would define the variables, get the values and then pass them in, or get the values within the function as you do with tot_quest in your other function.

You don't even need to pass in grade or tot_quest into your functions if you are getting their values within the function. You can just define them within the function (if you don't need them anywhere else in the program.)

double calculateGrade(double grade)
{
    int quest_corr, tot_quest;
    grade = quest_corr/tot_quest;
    cout << grade;
return grade;
}

This will not give you the correct answer because you define quest_corr and tot_quest here, but do not give them a value. Then you use them in a calculation. I would define the variables, get the values and then pass them in, or get the values within the function as you do with tot_quest in your other function.

\

this is wat i want to do: no homework just practice

You are to write a program to determine the grade received on a test. The program should ask the user to type in the number of questions they got correct and the number of questions on the test. It should then compute the grade. The program should continue computing grades until the user types a negative number to exit.
Requirements
One function, calculateGrade, that returns the grade (you should be able to determine what formal parameters are needed.)
One function, enterTotalQuestions, that asks the user to enter the total number of questions. It should have one integer formal parameter. It should error check the input value to make sure that a number at least as big as the formal parameter is entered. The input value should be returned.
Answers should have one digit of precision. i want to error trap it but im just doin one thing at a time. this is practice 4 me . is the layout of this good?

error: `double calculateGrade' redeclared as different kind of symbol.

this is the error im getting and i believe it to be the glitch that is stopping my program to run. could anyone help? im trying to call the functions above int main() but my compiler is saying that i am redeclaring them.

any suggestions?????

Instead of calling the function caclulateGrade in main, you are redefining it there.

Replace the below two lines in main with

int enterTotalQuestions(int tot_quest);
        double calculateGrade(double grade);

the actual function calls.

I can't give you an example because I would have to rewrite most of your code and that would defeat the purpose of practice for you.

look there are 3 ways a function can be written

first the declaration or prototype eg
return type function(data type identifier);
this is generally used as forward declaration.

int function(int a,......);

second actuall definition of function

int function(int a,float b)
{
..
..
..
}

third is calling that function
there u dont need to write return typa and data type

int main()
{
int a =5;
float b = 6.0;

function(a,b);
return 0;
}

you could give me the answer i will just see what am i doing wrong and do another example. b/c this is the way that i think it is done and its still not reading it so im stuck still

im trying to call the functions above int main() but my compiler is saying that i am redeclaring them.

You declare functions outside of int main() but you call them within int main(). So if you're trying to call them outside of the main() function, then it (the compiler, I mean) would think you're trying to declare them again.

Also, it is much easier to see what the problem is if you post your code.

this is the code

#include<iostream>
using namespace std;
double calculateGrade(double);
int enterTotalQuestions(int);
double calculateGrade(grade)
{
    int letter,numb_quest, answer;
    letter = numb_quest/answer;
    letter=grade;
    cout << grade;
    return grade;
}
int enterTotalQuestions(tot_quest)
{
   cout << "total number of questios: " ;
   cin >> tot_quest;
   return enterTotalQuestions;
}
int main()
{
    int quest_corr;
    cout << "how many questions are correct " ;
    cin >> quest_corr;
    int enterTotalQuestions(int tot_quest)();
    double calculateGrade(double grade)();
    return 0;
}

These are the first lines of the function definitions:

double calculateGrade(grade)
int enterTotalQuestions(tot_quest)

In the above, grade and tot_quest need to be preceded by whatever type they are.

Since you define calculateGrade() and enterTotalQuestions() before main() instead of after, then the following lines can be deleted.

double calculateGrade(double);
int enterTotalQuestions(int);

If the function defintions were placed after main() then the above two lines are needed.

In main() these:

int enterTotalQuestions(int tot_quest)();
double calculateGrade(double grade)();

are just plain wrong. The empty () at the end aren't called for. If those empty () weren't there, then the two lines would represent redeclaration of the functions when what you want is to call the functions. To make them function calls remove all keywords that represent a type on both of the two lines.

As it stands, you would be ignoring the return values of both function calls. You should evaluate whether that is what you want/need to do, or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.