Global Var & multifunction

well as my class makes there foray into Chapter 6 this assignment has me stuck

i can easily accomplish the task in one function

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
 #include <iostream>
#include <iomanip>


using namespace std;
//Global Constants 
const double gravity=9.8;
int main()
{

	double distance;				// Decalaring variables
	char again;	
	int Time;


	do {
			

		cout << "Enter time(seconds) " << ": ";			

		cin >> Time;
		
		distance = .5*(::gravity*(Time*Time));

		system("cls");					//clears the output screen
		cout << setw(18) << left<< "Time: "   << Time << " seconds" << endl;
		cout << setw(18) << left << "Distance: " << distance << " meters" << endl;

		
		cout << "Would you like to try another number? \n";
		cin >> again;
		again = toupper(again);
		while ((again != 'Y') && (again != 'N'))		//ERROR TRAP
		{
			cout << "ERROR: Enter Y/N: ";
			cin >> again;
			again = toupper(again);
		}

	} while (again == 'Y');		// Repeats loop
	system("pause");
	return 0;
}


but when i go to break it up into multiple functions as req by the assignment the variables are not transferring over.

the error codes im getting are:
C++ no instance of function template "std::distance" matches the required type(E0386)

'void DisplayOutput(double,double)': cannot convert argument 1 from 'iterator_traits<_Iter>::difference_type (__cdecl *)(_InIt,_InIt)' to 'double' A4 Falling distance *a4 falling distance\a4 falling distance\source.cpp (c2664)


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
  Assignment #4
The purpose of this assignment to is determine the distance an object falls in a given time 
*/

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//Global Constants 
const double gravity(9.8);

void getData(int &);
double fallingDistance(double);
void DisplayOutput(double,double);




int main()
{
	int ti;
	double distance;
	char again;
	do
	{
		getData(ti);
		DisplayOutput(distance, ti);
		

		cout << "Do you like to continue (y/n) ? ";
		cin >> again;
		again = toupper(again);
		while ((again != 'Y') && (again != 'N')) //ERROR TRAP
		{
			cout << "ERROR: Enter Y/N: ";
			cin >> again;
			again = toupper(again);
		}

	} while (again=='Y');
	// Repeat process
	
	system ("pause");
	return 0;
}

void getData(int &ti)
{
	cout << "enter the time in seconds";
	cin >> ti;
}

double fallingDistance(double distance)
{
	int ti;
	double gravity;

	getData(ti);
	
	distance = .5*(gravity*(ti*ti));
	return distance;
}


void DisplayOutput(double distance, double ti)
{
	// Display output
	system("cls");		//clears the output screen
	cout << "Income              : " << setw(8) << ti << "seconds" << endl;
	cout << "Distance                 : " << setw(8) << distance << " meters" << endl;
}
Last edited on
huzzah apparently all i needed was a good adult beverage now to fix the math...

so my new problem is that the distance always outputs as the below number regardless of input

Income              :       10seconds
Distance                 : -9.25596e+61 meters
Do you like to continue (y/n) ?



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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//Global Constants 
const double g=9.8;

void getData(int &);
double fallingDistance(double);
void DisplayOutput(double &,double);




int main()
{
	int time;
	double d1;
	char again;
	do
	{
		getData(time);
		DisplayOutput(d1,time);
		

		cout << "Do you like to continue (y/n) ? ";
		cin >> again;
		again = toupper(again);
		while ((again != 'Y') && (again != 'N')) //ERROR TRAP
		{
			cout << "ERROR: Enter Y/N: ";
			cin >> again;
			again = toupper(again);
		}

	} while (again=='Y');
	// Repeat process

	cin.get();
	cout << "Press ENTER to quit.";
	cin.get();
	return 0;
}

void getData(int &seconds)
{
	cout << "enter the time in seconds";
	cin >> seconds;
}

double fallingDistance(double dis)
{
	
	int ti;

	getData(ti);
	
	dis = .5*(g*(ti*ti));
	return dis;
}


void DisplayOutput(double &distance, double time)
{
	double getData(time);
	double fallingDistance(distance);
	// Display output
	system("cls");		//clears the output screen
	cout << "Income              : " << setw(8) << time << "seconds" << endl;
	cout << "Distance                 : " << setw(8) << distance << " meters" << endl;
}
Last edited on
any help here would be appreciated still stuck here
In fallingDistance(...) you do not modify the parameter (as in getData(...)). Instead you return the value which is better.

Line 67 does not call the function getData(...). Instead it creates a local variable with that name. The same applies to line 68: No function call but instead a local variable.

So change fallingDistance(...) to:
1
2
3
4
5
6
7
8
9
10
double fallingDistance(int ti double dis)
{
	
	int ti;

	getData(ti);
	
	dis = .5*(g*(ti*ti));
	return dis;
}
Then line 73 would be:

cout << "Distance : " << setw(8) << fallingDistance(time)distance << " meters" << endl;
Thanks coder77 i was able to fix it with that explanation
Topic archived. No new replies allowed.