Move the indicated code into functions

Im not sure what Im doing wrong. How would I fix my code?

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
Original code
  // Move the indicated code into functions.

#include <iostream>
using namespace std;

int main()
{
    
    double jedi_level;
    
    int age;
    int weight;
    int mcc;
    
    // TODO - write a void function to print out to console this welcome message
    cout << "Welcome to my fabulous Jedi power level calculator!" << endl
         << "This program will take your age, weight, and" << endl
         << "midichlorean count and return your Jedi power level!" 
         << endl << endl;
    
    
    // TODO - write a function that will prompt the user for his/her age,
    // weight, and midicholrean count. Then calculate and return their 
    // jedi level (returns a double). Remember to assign the retuned value
    // to the variable 'jedi_level'.
    cout << "please enter your age : ";
    cin >> age;
    cout << "please enter your weight : ";
    cin >> weight;
    cout << "please enter your midicholrean count : ";
    cin >> mcc;
    jedi_level = static_cast <double>(mcc * age) / (weight * weight);

    // this should remain inside your main function
    cout << "Your Jedi Level is : " << jedi_level;
    
    return 0; 
}






My Code.
[code]
  #include <iostream>
using namespace std;

double jedi_level;

void displaymessage()
{
    cout << "Welcome to my fabulous Jedi power level calculator!" << endl
    << "This program will take your age, weight, and" << endl
    << "midichlorean count and return your Jedi power level!"
    << endl << endl;
    
}

int main()
{
    
    double jedi_level;
    
    int age;
    int weight;
    int mcc;
    
    void();
    jedi_level = static_cast <double> (int age, int weight, int mcc);
    // this should remain inside your main function
    cout << "Your Jedi Level is : " << jedi_level;
    return 0;
}

    // TODO - write a function that will prompt the user for his/her age,
    // weight, and midicholrean count. Then calculate and return their
    // jedi level (returns a double). Remember to assign the retuned value
    // to the variable 'jedi_level'.

void getJediLevel(int age, int weight, int mcc)
{
    cout << "please enter your age : ";
    cin >> age;
    cout << "please enter your weight : ";
    cin >> weight;
    cout << "please enter your midicholrean count : ";
    cin >> mcc;
    jedi_level = static_cast <double>(mcc * age) / (weight * weight);
    
    // this should remain inside your main function
    cout << "Your Jedi Level is : " << jedi_level;
    
}

Last edited on
Line 51: Bad idea to make this global. Get rid of it.

Line 53: You never call displaymessage().

Line 65: This additional declaration of jedi_level hides the global.

Line 71: What is this?

Line 72,91: You can't cast 3 ints to a double.
Line 72: Shouldn't this be where you call getJediLeve?

Line 79,83: Instruction says to RETURN a double. You can't do that with a void function.

Line 83: You have no function prototype for this function.

Line 83: Why are age, weight and mcc pass as arguments? You're not passing in any values.

Line 93-94: Instructions say to leave this line in main. Why is it here?

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
#include <iostream>
using namespace std;

double getJediLevel();

void displaymessage()
{	cout << "Welcome to my fabulous Jedi power level calculator!" << endl
		<< "This program will take your age, weight, and" << endl
		<< "midichlorean count and return your Jedi power level!"
		<< endl << endl;
}

int main()
{	double jedi_level;

	displaymessage();
	jedi_level = getJediLevel(); 
	// this should remain inside your main function
	cout << "Your Jedi Level is : " << jedi_level;
	return 0;
}

double getJediLevel()
{
	int age, weight, mcc;
	double level;

	cout << "please enter your age : ";
	cin >> age;
	cout << "please enter your weight : ";
	cin >> weight;
	cout << "please enter your midicholrean count : ";
	cin >> mcc;
	level = static_cast<double> (mcc * age) / (weight * weight);
	return level;
}

Last edited on
@AbstractionAnon thanks
Topic archived. No new replies allowed.