Function returning a string literal

Ok, so its more EECS 168. I pretty much have most of this program written, the only problem i am having is getting one of my functions to return a string literal.

Here is the assignemnet:
https://wiki.ittc.ku.edu/ittc/EECS168:Homework2
(I find it much easier to just link this rather than spam this post with a huge long assignment description.)

Here is my code so far:
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <string>
using namespace std;

double average_standard(double stand_total, double days_worked_par);
double average_one(double one_total, double days_worked_par);
double average_two(double two_total, double days_worked_par);
double average_deluxe(double deluxe_total, double days_worked_par);
double banana_points_func(double stand_average, double one_average, double two_average, double delux_average, double wasted);
string star_rating(double points);


int main()
{

double days_worked;
double stan_choc_numb, one_topping_numb, two_topping_numb, deluxe_numb;
double stan_choc_total = 0;
double one_topping_total = 0;
double two_topping_total= 0;
double delux_total = 0;
double wasted_bans = 0;
int n;
char choice;

do
{
//User input for number of days worked
cout << "Enter the number of days the employee worked: ";
cin >> days_worked;
cout << "\nEnter the amount of each typer for each day worked.\n";

for (n = 1; n <= days_worked; n++)
{
	//Getting the number of bananas sold for each day
	cout << "Day " << n << endl;
	cout << "Standard Chocolate Covered: ";
	cin >> stan_choc_numb;
	cout << "One-Topping: ";
	cin >> one_topping_numb;
	cout << "Two-Topping: ";
	cin >> two_topping_numb;
	cout << "The Deluxe: ";
	cin >> deluxe_numb;
	//Adding the days numbers to the running total
	stan_choc_total = stan_choc_total + stan_choc_numb;
	one_topping_total = one_topping_total + one_topping_total;
	two_topping_total = two_topping_total + two_topping_numb;
	delux_total = delux_total + deluxe_numb;
}

//Getting number of wasted bananas
cout << "Enter the number of wasted bananas: ";
cin >> wasted_bans;


//Calling functions and setting them to variables
double standard_choc_average = average_standard(stan_choc_total, days_worked);
double one_topping_average = average_one(one_topping_total, days_worked);
double two_topping_average = average_two(two_topping_total, days_worked);
double delux_average = average_deluxe(delux_total, days_worked);
double banana_points = banana_points_func(standard_choc_average, one_topping_average, two_topping_average, delux_average, wasted_bans);


//Outputting results to user
cout << "\nAverage Sold Per Day:\n";
cout << "Standard Chocolate Covered: " << standard_choc_average << endl;
cout << "One-Topping: " << one_topping_average << endl;
cout << "Two-Topping: " << two_topping_average << endl;
cout << "The Delux: " << delux_average << endl << endl;
cout << "Banana Points: " << banana_points << endl << endl;

string star_rating(banana_points);

cout << "\n\nIs there another employee to calculate? (y/n): ";
cin >> choice;

}while ((choice != n) || (choice != N))

return 0;

}

double average_standard(double stand_total, double days_worked_par)
{
	double average = stand_total / days_worked_par;
	return (average);
}

double average_one(double one_total, double days_worked_par)
{
	double average = one_total / days_worked_par;
	return (average);
}


double average_two(double two_total, double days_worked_par)
{
	double average = two_total / days_worked_par;
	return (average);
}

double average_deluxe(double deluxe_total, double days_worked_par)
{
	double average = deluxe_total / days_worked_par;
	return (average);
}

double banana_points_func(double stand_average, double one_average, double two_average, double delux_average, double wasted)
{
	double point_total = ((stand_average) + (2 * one_average) + (3 * two_average) + (5 * delux_average) - (4 * wasted));
	return (point_total);
}


string star_rating(double points)
{
	if (points >= 125)
		return "This employee has earned a Five Star rating.";
	
	if else ((points >= 75) && (points < 125))
		return "This employee has earned a Four Star rating.";	

	if else ((points >= 50) && (points < 75))
		return "This employee has earned a Three Star rating.";

	if else ((points >= 25) && (points < 50))
		return "This employee has earned a Two Star rating.";
	
	if else ((points >= 10) && (points < 25))
		return "This employee has earned a One Star rating.";
	
	else
		return "This employee should be fired.";
}



The last function (string star_rating) is the one i am having issues with. I feel like once i get that working then the rest will be smooth sailing. I am not too worried about the rest of the program right now. If you see any logic errors then i will probably be able to pick those out once i get this darn thing to compile. I dont really know the syntax for the string function so that is my main concern right now. Any help is much appreciated. If you have any questions or i forgotten to mention any details, please let me know.

Thanks,
LiverpoolFTW
string star_rating(banana_points);

Line 73, that is not how you call a function. (Which is what I assume you are trying to do there.) Re-check your tutorial/book/whatever.
I knew that line was not going to be right. I just stuck that in there before i put my code up to show that i needed my string returned from the function to be put there. I think i could use a void function to get this final function done but i am worried that our professor wants us to return a string literal.

I cant really trouble shoot anything so far because the only error i get is in my final function.

The error i get every time is:

banana.cpp: In function ‘std::string star_rating(double)’:
banana.cpp:129: error: expected ‘(’ before ‘else’
banana.cpp:132: error: expected ‘(’ before ‘else’
banana.cpp:135: error: expected ‘(’ before ‘else’
banana.cpp:138: error: expected ‘(’ before ‘else’
banana.cpp:141: error: ‘else’ without a previous ‘if’

I have looked online and through the book for a long time before posting, but i can't find the proper syntax for this final function.
In the implementation of function string star_rating(double points)

The if else chain or ladder has the following format:

1
2
3
4
5
6
7
8
9
if ( condition1 )
   statement1;
else if ( condition2 )
   statement2;
else if ( condition3 )
   statement3;
 .
 .
 .


I have not looked for other possible errors.
Wow, i cant believe i did that haha..... i guess thats what i get for doing programming at 1 in the morning,

EDIT: after fixing that simple mistake, the program compiled and that allowed me to test with line 73 ad get that to work (i just set it to a string variable x and then cout << x). I had one or two logical errors that i needed to fix. Works great now. Thank you all for your help. Always useful to have fresh eyes find my stupid mistakes :)
Last edited on
Topic archived. No new replies allowed.