i am trying to write code for my function project but i get errors can somone tell me if i am on the write track at least

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//fn prototype for passing num of students in class 
// *** void DisplayStudent(double num, double answer); ???
void DisplayStudent(double num);
// *** void DisplayName(string& courseName,double answer);
void DisplayName(string& courseName,string& answer);
void DisplayCredit(double& credit,double& answer1);
// *** int main ()
int main() 
{
//*** double num,answer;
double num;
string answer;
string courseName;
double credit;
cout << "Enter the total number of students in each class \n";
cin >> num;
//first call then bounces too loop

//function call
// *** DisplayStudent(answer,num);
DisplayStudent(num);
DisplayName(courseName,answer);

cout<<"the amount of credits is   "<<&credit<<endl;
cout << "The course you are taking is   " << courseName<<endl;
return 0;
}
//function goes here
// *** void DisplayStudent(double num,double answer)
void DisplayStudent(double num)




{ 
// *** cout<<" the total number of students=" << answer;
cout<<" the total number of students=" << num<<endl;
}

void DisplayName(string &courseName,string& answer ) 
{
cout << "what course please enter without spaces" << answer<<endl;
cin >> courseName;
}

the functions should include pass by reference for course name and number of credits
and if possible include a function on how to caculate a gpa

Recommended Answers

All 10 Replies

Its almost right.
line 28: cout << &credit
whats the use of printing the variables address

Hi.
Although the function "DisplayCredit" is decleared , the function "DisplayCredit" is never used.
Additionally,in the function " DisplayName",answer is not displayed.
Because answer is NULL.
Thanks!

how do i fix this then so that the functions that i declared work properly

how do i fix this then so that the functions that i declared work properly

Actually, I thought, some code will be added, and didnt probe into details.

I dont know, what you want to do with the data you get from the user & the objects you've declared. Explain it a little more

it is supposed to be a assingment dealing with functions
these are the fucntions that i am suppposed to create
fn= function

number of students in the class (value-ret fn)

Information for the 3 courses you took: (pass by reference fn)

The course name (without spaces)
The number of credits
Grade value (decimal number)
Calculate the total GPA for each student. (use a fn)

Calculate the letter grade using the ranges defined below. (use a fn)


but i am not sure if my first 2 functions which i have the code for posted the one regarding student number and the course name is done correctly or not and am geting build errors

how do i fix this then so that the functions that i declared work properly

Well...
I think the function which set the value of 'credit',and 'answer' ,and get the value of them is needed.
Additionally, I bother one point about following code.

cin>>num;

If you input invalid value of num, you can't input value from console while error occurs.
for example
input the value of num is 'a' or ' A'
so you need to describe like following
cin>>num;
while(!cin){
cin.clear();
cin.ignore(INT_MAX,'\n');
cout<<" You input invalid value."<<endl;
cout<<"input value again:";
cin>>num;
}
In this case,you need to include header file "limits.h" to point out 'INT_MAX'.
Thanks!

i apreciate that help but that extra step isnt necessary as for my project on the code above do my functions work the way they are supposed to ?
one by pass by refrence which is course
and the other which is num students a value returning function

also when i post this code it doesnt compile what is going wrong?

i apreciate that help but that extra step isnt necessary as for my project on the code above do my functions work the way they are supposed to ?
one by pass by refrence which is course
and the other which is num students a value returning function

also when i post this code it doesnt compile what is going wrong?

Seems to me this program should compile. Whether it runs well is a separate issue. See posts 2 and 3 about why it may not run as it should. If it's not at least compiling, please give the exact errors, along with the corresponding line numbers. If it compiles, but doesn't link properly, that could be something different. If it compiles, then you get a run-time error when you run it, that's also different. Please repost with a thorough explanation of the errors you get, along with any changes you've made since post 1, if any. Thanks.

sorry maybe i should be more specific the program runs but there is an error in credit as it does not allow me to input a number to be passed by reference

also can somone evaluate if my functions are working the way they should be by pass by reference and value returning ?

// i have not made any changes to the original code post as post 2 was very vauge as to what i had to exactly change

sorry maybe i should be more specific the program runs but there is an error in credit as it does not allow me to input a number to be passed by reference

also can somone evaluate if my functions are working the way they should be by pass by reference and value returning ?

// i have not made any changes to the original code post as post 2 was very vauge as to what i had to exactly change

Looks to me like the parameter passing is fine. Regarding post 2 and the change to be made there, you declare the variable credit as a double in line 18. When you do that, C++ sets aside 8 bytes of memory for that variable. Let's say it stores it at memory location 0xdddddddd. Let's say you store the value 5.5 in the variable credit and you try to display it. The way you have line 28 written, what will be displayed is 0xdddddddd, not 5.5. The & operator tells C++ that you want to display the address, not the value. If you want to display the value, which is 5.5, on line 28, take out the & on that line.

DisplayName passes two variables by reference, but only has one cin statement, so the way you have it, there is not reason to pass answer by reference since you never change its value. I'm not sure whether you need to pass answer at all. If all the function does is ask for a name and pass that name back to the main() function, it seems to me that answer is not needed. Pass the variable courseName, as you do, prompt the user for the course name, as you do, have the cin statement, as you do, then end the function, I'd get rid of the answer variable altogether in that function (and in main ()) since I don't see any need for it.

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.