Cant return two values

When I run the program qpl2 always equals zero but qpl1 shows up fine. I think it is skipping the if statements for c2grade altogether but I don't know why.
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
int getQualityPoints(int c1credits,char& c1grade,char& c2grade,int c2credits)
{
    
if(c1grade=='A')
  c1grade=4;
else if(c1grade=='B')
  c1grade=3;  
else if(c1grade=='C')
  c1grade=2;
else if(c1grade=='D')
  c1grade=1;
else
   c1grade=0;
  return c1grade;
  
if(c2grade=='A')
  c2grade=4;
else if(c2grade=='B')
  c2grade=3;  
else if(c2grade=='C')
  c2grade=2;
else if(c2grade=='D')
  c2grade=1;
else
  c2grade=0;
  return c2grade;
  
  
  
}
void computeGPA(int c1credits,char c1grade,int c2credits,char c2grade,int& qpl1,int& qpl2,double gpa)
{

qpl1=c1credits*getQualityPoints(c1credits,c1grade,c2grade,c2credits);
qpl2=c2credits*getQualityPoints(c1credits,c1grade,c2grade,c2credits);

}
void printReport(int c1credits,char c1grade,int c2credits,char c2grade,int qpl1,int qpl2,double gpa)
{
cout<<"quality points "<<qpl1<<" "<<qpl2<<endl;
}
Last edited on
It's not possible to return two values from a function.
However, you seem to have thought up getQualityPoints() in such a way that it can be easily sliced up into two functions without losing anything.
I'm still relatively new to C++, but I have yet to come across a function that allows more than one value to be returned during a single run through that function. Once the program hits a return line, it exits the function and returns to the main program wherever the function was called from.
My professor told me to call the function twice but that's exactly what I'm doing. I don't think he will allow me to break it into 2 functions either. Is there anyway to return two values on the same line?
If you absolutely have to, you could return an std::pair.
I have no idea how to use std::pair so I am going to just split it into 2 functions and explain to my professor why I did it. Thanks for all of the help!
Correct me if I am wrong, but it looks like both segments of the function are doing the same thing? If that is the case, you only need one of those segments (and you can cut out half of your function parameters as well).

Keep in mind that the function doesn't care about the rest of the program. The only things it is concerned about are the parameters that are passed to it and the value it returns (if applicable). It doesn't care where the parameters come from, or what you do with the returned value.
Last edited on
They are doing the same thing except to different grades. One half is changing grade1 into a number value and the other half is changing grade2. I cant return both. Are you saying there is a way that I can convert both grades to numbers in the same If statement?
No, he's saying that you can change the function to take in a grade and return its numerical value. You can call it once with passing in the first grade; then, you can call it again with passing in the second grade. No need to pass in the credits or the other grade.
Last edited on
This is the whole program. I don't know if this is asking for too much or not but what would that look like. I'm honestly drawing a blank.

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
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
void getGradeInfo(int& c1credits, char& c1grade, int& c2credits, char& c2grade);
int getQualityPoints(int c1credits,char& c1grade,char& c2grade,int c2credits);
void computeGPA(int c1credits,char c1grade,int c2credits,char c2grade,int& qpl1,int& qpl2,double gpa);   
void printReport(int c1credits,char c1grade,int c2credits,char c2grade,int qpl1,int qpl2,double gpa);   

int main()
{
    int c1credits, c2credits, qpl1, qpl2;
    double gpa;
    char c1grade, c2grade;
    
    getGradeInfo(c1credits, c1grade, c2credits, c2grade);
    computeGPA(c1credits, c1grade, c2credits, c2grade, qpl1, qpl2, gpa);
    printReport(c1credits, c1grade, c2credits, c2grade, qpl1, qpl2, gpa);
    
    system("PAUSE");
    return 0;
}//end of main

    
void getGradeInfo(int& c1credits, char& c1grade, int& c2credits, char& c2grade)
{
  cout<<"Enter the # of credits and your grade for class #1 "<<endl;
  cin>>c1credits>>c1grade;
  cout<<"Enter the # of credits and your grade for class #2 "<<endl;
  cin>>c2credits>>c2grade;

}
int getQualityPoints(int c1credits,char& c1grade,char& c2grade,int c2credits)
{
    
if(c1grade=='A')
  c1grade=4;
else if(c1grade=='B')
  c1grade=3;  
else if(c1grade=='C')
  c1grade=2;
else if(c1grade=='D')
  c1grade=1;
else
   c1grade=0;
  return c1grade;
  
if(c2grade=='A')
  c2grade=4;
else if(c2grade=='B')
  c2grade=3;  
else if(c2grade=='C')
  c2grade=2;
else if(c2grade=='D')
  c2grade=1;
else
  c2grade=0;
  return c2grade;
}
void computeGPA(int c1credits,char c1grade,int c2credits,char c2grade,int& qpl1,int& qpl2,double gpa)
{

qpl1=c1credits*getQualityPoints(c1credits,c1grade,c2grade,c2credits);
qpl2=c2credits*getQualityPoints((c1credits,c1grade,c2grade,c2credits);


}
void printReport(int c1credits,char c1grade,int c2credits,char c2grade,int qpl1,int qpl2,double gpa)
{
cout<<"quality points "<<qpl1<<" "<<qpl2<<endl;
}
Last edited on
The problem is that you're missing the point of functions.
This is how you're doing things:
1
2
3
4
5
6
7
8
9
float sum_of_squares(float x1,float y1,float x2,float y2,float x3,float y3){
    return x1*x1+y1*y1;
    return x2*x2+y2*y2;
    return x3*x3+y3*y3;
}
//...
std::cout <<sum_of_squares(2,3,0,0,0,0)<<std::endl;
std::cout <<sum_of_squares(0,0,4,5,0,0)<<std::endl;
std::cout <<sum_of_squares(0,0,0,0,6,7)<<std::endl;
Let's pretend that return doesn't return control to the calling function, and that somehow that function makes sense. Notice how those three lines in sum_of_squares() are pretty much the same, except with different variables being used? The beauty of functions is that you can put repetitive code in a single place, so when you have to modify it, you have to modify a single place.
This is a much better definition of sum_of_squares():
1
2
3
4
5
6
7
float sum_of_squares(float x,float y){
    return x*x+y*y;
}
//...
std::cout <<sum_of_squares(2,3)<<std::endl;
std::cout <<sum_of_squares(4,5)<<std::endl;
std::cout <<sum_of_squares(6,7)<<std::endl;
Now if you need to have sum_of_squares() return the sum of the cubes (for whatever odd reason), you only need to change one thing:
1
2
3
float sum_of_squares(float x,float y){
    return x*x*x+y*y*y;
}
What is getQualityPoints() supposed to do? it looks like it simply returns a number between 1 & 4 depending on the grade A-D. It that all it is supposed to do?
getQualityPoints() is supposed to just convert the letter grades that the user enters into its number value, A=4, B=3, etc and return it.
@DonCeaser

The I recommend you re-write it to accept just a letter grade and return that number:
1
2
3
4
int getQualityPoints(const char grade)
{
    // ...
}
Please forgive my ignorance but I really don't understand how I would get the function getQualityPoints() to convert the 2 letter grades inputted by a user if they are stored in the same place. If I called the function twice wouldn't it just return the same number twice? Would I still be able to call it the same way I have here?
qpl1=c1credits*getQualityPoints(c1credits,c1grade,c2grade,c2credits);
Last edited on
Call the function once passing c1grade and a second time passing c2grade:
1
2
3
4
int q1 = getQualityPoints(c1grade);
int q2 = getQualityPoints(c2grade);

// then do what you need to with the quality points 
Last edited on
thanks for all the help.
Topic archived. No new replies allowed.