c++ program including functions

this program suppose calculates the average speed of the yachts in knots and km/h. but its not working can anyone help me figure it out what is it that I'm doing wrong here! Thanks.
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
#include<iostream>
using namespace std;

//Function prototypes 
void announce();
int getStart(float startHours, float startMinutes);
int getDistance();
void getCommand();
int getFinish(float finishHours, float finishMinutes);


int main()
{
      // Announce the program.
       announce();
	
    //Display menu and Prompt for a command.
	char cmd;
	
	cout <<  void getCommand();
	
	switch(cmd){
		
	case 'd':
	case 'D':
	cout << int getDistance();
	
	break;
	
	case 's':
	case 'S':
	cout << int getStart( float startHours, float startMinutes);
	
	break;
	
	case 'f':
	case 'F':
	cout << int getFinish( float finishHours, float finishMinutes);
	cout << float conv2knots/km(float speed);
	
	
	break;
	
	case 'q':
	case 'Q':
	cout << "quiting the program"<< endl;
	
	break;
	
	default:
	cout << "Error - Wrong Choice"<< endl;
}
    
	
	
	
	
}

//--------------------------------------------------------------------------------------------------------------
// "announce " this function announces the program and prints out the instructions for the user  at the 
// start of new race.
//----------------------------------------------------------------------------------------------------------------
void announce(){
    cout<<" YachtRace : A yacht race program calculates the average speed of the \n"
		"yacths in a race over the distance of 100 nautical miles."<<endl;
}
   
// Gets the start time convert it into minutes.
int getStart(float startHours, float startMinutes){
	cout << "Enter a new start time (08:00 - 17:00):";
	cin >> startHours;
	
	startHours = startHours / 100;
	startHours = startHours % 100;
	
	startMinutes = startHours / 60;
	startMinutes = startHours % 60;
	
	cout << startMinutes << "minutes" << endl;
	return startMinutes;
	
	
}

//  Prompt for and get the distance.
int getDistance ( ){
	cout << " Enter the distance in NM: ";
	cin >> distance;
	return distance;
}

// Display the command menu.
void getCommand ( ){
	
	cout<< "Command menu:\n";
	cout<< "D - Distance:Enter a distance (in nautica miles, max 100)\n";
	cout<< "S - Start Time: enter a new start race time( 8:00:00 - 17:00:00)\n";
	cout<< "F - Finish Time: Enter the yacht finish time and display the speed\n";
	cout<< "Q - Quits the program\n";
	
}

// promt for and get the finish time change it into minutes.
int getFinish (float finishHours, float finishMinutes){
	cout << "Enter  a finish time start time + 24 hours: ";
	cin >> finishHours;
	
	finishHours = finishHours / 100;
	finishHours = finishHours % 100;
	
	finishMinutes = finishHours / 60;
	finishMinutes = finishHours % 60;
	cout << finishMinutes << "minutes" << endl;
	return  finishMinutes;
}

// calculates the average speed in knots and km.
float conv2knots/km(float speed){
	float finishMinutes;
	float startMinutes;
	float distance;
	float speed;
	
	speed = distance/( finishMinutes - startMinutes);//average speed in knots.
	speed = speed * 60;
	
	velocity = 7.5 * 1.852;// average speed in km/h.
	
	return speed, velocity;
}
Yea posting your entire program, and then asking us what the problem is will likely not get a very fast answer. Be specific, what problem are you having? Post JUST the section involved with the problem unless we ask for more.
cout << void getCommand();

Start by fixing this and all the others like it.
Last edited on
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
#include<iostream>
using namespace std;

//Function prototypes 
void announce();
float getStart(float startHours, float startMinutes);
int getDistance(float distance);
void getCommand();
float getFinish(float finishHours, float finishMinutes);
float conv2knotsKm(float speed);



int main()
{
      // Announce the program.
       announce();
	
	// define variables 
	float startHours, startMinutes, distance, finishHours, finishMinutes,speed;
	
    //Display menu and Prompt for a command.
	char cmd;
	
	getCommand();
cin >> cmd;

	
	switch(cmd){
		
	case 'd':
	case 'D':
	cout <<  getDistance(distance);
	
	break;
	
	case 's':
	case 'S':
	cout <<  getStart(  startHours,  startMinutes);
	
	break;
	
	case 'f':
	case 'F':
	cout <<  getFinish( finishHours,  finishMinutes);
	cout <<  conv2knotsKm( speed);
	
	
	break;
	
	case 'q':
	case 'Q':
	cout << "quiting the program"<< endl;
	
	break;
	
	default:
	cout << "Error - Wrong Choice"<< endl;
}
}

//--------------------------------------------------------------------------------------------------------------
// "announce " this function announces the program and prints out the instructions for the user  at the 
// start of new race.
//----------------------------------------------------------------------------------------------------------------
void announce(){
    cout<<" YachtRace : A yacht race program calculates the average speed of the \n"
		"yacths in a race over the distance of 100 nautical miles."<<endl;
}
   
// Gets the start time convert it into minutes.
float getStart(float startHours, float startMinutes){
	cout << "Enter a new start time (08:00 - 17:00):";
	cin >> startHours;
	
	//startHours = startHours / 100;
	startHours = (int)startHours % 100;
	
	//startMinutes = startHours / 60;
	startMinutes = (int)startHours % 60;
	
	cout << startMinutes << "minutes" << endl;
	return startMinutes;
	
	
}

//  Prompt for and get the distance.
int getDistance (float distance){
	cout << " Enter the distance in NM: ";
	cin >> distance;
	return distance;
}

// Display the command menu.
void getCommand ( ){
	
	cout<< "Command menu:\n";
	cout<< "D - Distance:Enter a distance (in nautica miles, max 100)\n";
	cout<< "S - Start Time: enter a new start race time( 8:00:00 - 17:00:00)\n";
	cout<< "F - Finish Time: Enter the yacht finish time and display the speed\n";
	cout<< "Q - Quits the program\n";
	
}

// promt for and get the finish time change it into minutes.
float getFinish (float finishHours, float finishMinutes){
	cout << "Enter  a finish time start time + 24 hours: ";
	cin >> finishHours;
	
	//finishHours = finishHours / 100;
	finishHours = (int)finishHours % 100;
	
	//finishMinutes = finishHours / 60;
	finishMinutes = (int)finishHours % 60;
	cout << finishMinutes << "minutes" << endl;
	return  finishMinutes;
}

// calculates the average speed in knots and km.
float conv2knotsKm(float speed){
	float finishMinutes;
	float startMinutes;
	float distance;
	float velocity;
	
	speed = distance/( finishMinutes - startMinutes);//average speed in knots.
	speed = speed * 60;
	
	velocity = 7.5 * 1.852;// average speed in km/h.
	
	return speed, velocity;
}


okay there are no problems with syntax. it is supposed to ask me to enter the command until I press 'Q'. but its exiting early.
okay there are no problems with syntax.

The fact that it compiles into some kind of Franken-program is no assurance of correctness.

What do you think this does?
cout << getDistance(distance);
What exactly are you trying to output with that? Did you perhaps mean this?
getDistance(distance);

it is supposed to ask me to enter the command until I press 'Q'.
There is nothing in your code to make it repeat that switch statement. It will enter it once, and then leave it, and then the program ends.
Last edited on
I don't know if this is going to be your own code, or if you're making it for a business, but if it's business code you may want to correct the typo here:
1
2
 cout<<" YachtRace : A yacht race program calculates the average speed of the \n"
		"yacths in a race over the distance of 100 nautical miles."<<endl;


I know that's kind of unhelpful, but these are important in a business or school scenario where you're either paid or graded based on perfection

Also, encase your switch statement in an infinite loop, and then just return 0 for your exit command (because main is an int). I don't know if that will work, haven't used switch in a while. It's possible you'll need to break out of the loop before returning 0. Regardless, there MUST be a loop there to make the menu repetitive.

Also, your functions are a little inconsistent. Some of them include cout's and some don't. If your function contains a cout statement with the same variable you're returning, you do not need to output the function, otherwise it'd print the var twice. If there is no output, then you should cout << function; Perhaps remain consistent so you don't have to remember which is which.
Last edited on
thanks
Topic archived. No new replies allowed.