Distance Converter

In this program I am attempting to read in 2 distances in MMM::FFFF::II format... convert to inches ... add together... then convert back and to MMM::FFFF::II format. i think the problem is that the variable types need something during the conversion. cause i am going from int to long. hmm... not sure though.

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Compiler directives

#include<iostream>

using namespace std;

struct Distance
{
	int Miles;
	int Feet;
	int Inches;
};

//prototypes 
void Input_Dist(Distance &dist1); //Dist is our reference variable
Distance Input_Dist();  //inputs the second distance
int Validate_Dist(Distance);
void Display_Dist(Distance);     //Print and returns nothing
long Dist_To_Inches(Distance, Distance);
Distance inches_To_Dist(long);
void Print_Zero(int,int);



int main (){
	Distance dist1, dist2, distResult;
	long int totalInches;
	
	cout << "This program will read in two distances in MMM::FFFF::II format" << endl;
	cout << "then output the sum of both in MMM::FFFF::II format" << endl;
	cout << endl;
	
	for(int i=0; i<3; i++){
		Input_Dist(dist1);  //pass by reference
		cout<< endl;
		dist2 = Input_Dist();
		//totalInches = dist1.Inches + dist2.Inches;
		//totalInches = Dist_To_Inches(dist1, dist2);
		//distResult = inches_To_Dist(totalInches);
		
		if (Validate_Dist(dist1) && Validate_Dist(dist2) && Validate_Dist(distResult))
		{
			cout << "\nDistance 1: "; Display_Dist(dist1);
			cout << "\nDistance 2: "; Display_Dist(dist2);
			cout << "\nResult: "; Display_Dist(distResult);
			cout << "Invalid distance was entered";
			cout << endl;
			continue;
		}
		else
		{
			
			//totalInches = Dist_To_Inches(dist1, dist2);
			//distResult = inches_To_Dist(totalInches);
			//totalInches = dist1.Inches + dist2.Inches;
			//totalInches = Dist_To_Inches(dist1, dist2);
			//distResult = inches_To_Dist(totalInches);
			
			
			cout << "\nDistance 1: "; Display_Dist(dist1);
			cout << "\nDistance 2: "; Display_Dist(dist2);
			//totalInches = Dist_To_Inches(dist1, dist2);
			totalInches = dist1.Inches + dist2.Inches;
			distResult = inches_To_Dist(totalInches);
			
			cout << "\nResult: "; Display_Dist(distResult);
			cout << endl;
		
		}
	}

	cout << endl;	

return 0;	
}

///////////////////////////////////////////////////////////////               Inputs the first distance value (miles, feet, and
void Input_Dist(Distance &dist1)  //Dist is now a reference                  // inches) and returns it using a reference parameter 
{																
	//cout promt user M,F,I; make clear for user
	cout << "Enter the first distance in MMM::FFFF::II format: ";   
	cin >> dist1.Miles >> dist1.Feet >> dist1.Inches;
	
}	
////////////////////////////////////////////////////////////                  // Inputs the second distance value and returns it using 
Distance Input_Dist()                                                         // ‘return’ statement
{
	Distance dist2;
	cout << "Enter the second distance in MMM::FFFF::II format: ";
	cin >> dist2.Miles >> dist2.Feet >> dist2.Inches;
	return dist2;
	
}
//////////////////////////////////////////////////////////////                // Accepts a distance value and returns a code indicating
int Validate_Dist(Distance d)                                                  //valid or invalid data
{
	int valDist;
	if (d.Miles < 999 || d.Feet < 5279 || d.Inches < 12){
		valDist = 0;
	}
	else{
		valDist = 1;
	}
	return valDist;
}
///////////////////////////////////////////////////////////////               //Accepts a distance value (miles, feet, inches) for
void Display_Dist(Distance dd)                                                 // printing and returns nothing
{
	cout << dd.Miles << "::" << dd.Feet << "::"<< dd.Inches << endl;
}
///////////////////////////////////////////////////////////////                    //Accepts miles, feet, inches and returns the
long Dist_To_Inches(Distance dist1, Distance dist2)                                // total inches
{
	Distance d3;             //define a new structure for sum
	long int totalInches;
	
	d3.Inches = dist1.Inches + dist2.Inches;
	d3.Feet = (12*(dist1.Feet + dist2.Feet));
	d3.Miles = (63360*(dist1.Miles + dist2.Miles));
	totalInches = d3.Inches + d3.Feet + d3.Miles;
	return totalInches;
}
////////////////////////////////////////////////////////////////                  //Accepts total inches and returns a distance value
Distance inches_To_Dist(long int totalInches)                                        // in miles, feet and inches                       
{                                                                               
	Distance distResult;
	distResult.Miles;
	distResult.Feet;
	distResult.Inches;
	
	
	if(totalInches > 63360){
		//distResult.Inches -= static_cast<int>(distResult.Miles/63360);    
		//totalInches -= 63360;
		distResult.Miles++;
	}
	if(totalInches > 12){
		//distResult.Inches -= static_cast<int>(distResult.Feet/12);
		//totalInches -= 12;
		distResult.Feet++;
	}
		
	return distResult;
}
///////////////////////////////////////////////////////////////
void Print_Zero(int,int)
{
	//  Prints leading zeros if necessary.  
}
//////////////////////////////////////////////////////////////

/*
Distance inches_To_Dist(long totalInches)
{
	Distance distResult;
	distResult.Miles;
	distResult.Feet;
	distResult.Inches;
	if(distResult.totalInches >= 63360){
		distResult.totalInches -= 63360;
		distResult.Miles++;
	}
	if(distResult.totalInches >= 12){
		distResult.totalInches -= 12;
		distResult.Feet++;
	}
		
	return distResult;
}

*/
1
2
3
4
5
6
7
void Input_Dist(Distance &dist1)  //Dist is now a reference                  // inches) and returns it using a reference parameter 
{																
	//cout promt user M,F,I; make clear for user
	cout << "Enter the first distance in MMM::FFFF::II format: ";   
	cin >> dist1.Miles >> dist1.Feet >> dist1.Inches;
	
}	

After reading the first integer MMM the input buffer will be left pointing at the two separating colons ::
That means that the next integer will try to read in the colons rather than the next number FFF.
You need to skip over the colons, perhaps using cin.ignore(2).
In line 41, if (Validate_Dist(dist1) && Validate_Dist(dist2) && Validate_Dist(distResult)) is Validate_Dist(distResult) necessary? What value is distResult?

Also I think Validate_Dist() should be

1
2
3
4
5
6
7
8
9
10
11
int Validate_Dist(Distance d)                                                  //valid or invalid data
{
	int valDist;
	if (d.Miles < 999 && d.Feet < 5279 && d.Inches < 12){/// use and since all checks must be true
		valDist = 1; //return true if valid
	}
	else{
		valDist = 0; // return false if invalid
	}
	return valDist;
}

Last edited on
Yes, I think you are right and also that you are making it more complex than necessary using the struct mechanism. Have a look at the following which is a bit more straight forward and castes from doubles to ints (which may annoy the experts)
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
include<iostream>
#include<cmath>//not req
#include<iomanip>//not req
using namespace std;
int const mile=63360;//inches
int const feet=12;//inches
int main()
{
    double m_1,m_2,f_1,f_2,i_1,i_2;
    double result=0.0,sum=0.0;
    double feet=0.0;
    double inches=0.0;
    double foot=0.0;
    cout<<"\n\n";
    cout<<"this program enters two distances in mile,feet and inch format\n"
    <<"sums and then prints them in the same format\n";
    cout<<"enter distance_1 in mmm,ffff and ii format\n";
    cin>>m_1>>f_1>>i_1;
    cout<<"enter distance_2 in mmm,ffff and ii format\n";
    cin>>m_2>>f_2>>i_2;
    sum=((m_1+m_2)*mile) + ((f_1+f_2)*feet) + (i_1+i_2);//total inches
    sum/=mile;//decimal miles
    //cout<<"\nsum "<<sum;//program check point
    result=int(sum);//caste to int
    feet=(sum-result)*5280;
    foot=int(feet);//caste to int
    inches=(feet-foot)*12;
    cout<<"\nmiles are "<<result;
    cout<<"  feet are "<<foot;
    cout<<"  inches are "<<inches;
}

Functions probably should be written to take the calculations out of main()....but didn't have time.
If you want to stick with your original code you don't need a variable of Distance type in Dist_To_Inches perhaps.

1
2
3
4
5
6
7
8
long Dist_To_Inches(Distance dist1, Distance dist2)                                // total inches
{
	long int totalInches=0;
	totalInches = dist1.Inches + dist2.Inches;
	totalInches += (12*(dist1.Feet + dist2.Feet));
	totalInches += (63360*(dist1.Miles + dist2.Miles));
	return totalInches;
}

I think your on to something here... i tried the modified code, however, this has shown me that there is an issue with "totalInches"... the compiler gives me a warning that totalInches is never used. I really think there is something going on with storing in "totalInches"... but i do see that your right about the extra variable... good advice!!!
Also I tried erasing the "::" from the program, i didn't recognize a change. maybe i don't know about that. I took out distResult from the validate function as well... thanks for that suggestion! didn't need that..
my goal is for an output like this,

Enter Distance 1: 19 43 11
Enter Distance 2: 7 24 5

Distance 1: 019::0043::11
Distance 2: 007::0024::05

Result: 026::0068::04
You need to rework this function it doesn't calculate the answer properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Distance inches_To_Dist(long int totalInches)                                        // in miles, feet and inches                       
{                                                                               
	Distance distResult;
	distResult.Miles; // unnecessary
	distResult.Feet; // unnecessary
	distResult.Inches; // unnecesary
	
	
	if(totalInches > 63360){
		//distResult.Inches -= static_cast<int>(distResult.Miles/63360);    
		//totalInches -= 63360;
		distResult.Miles++;
	}
	if(totalInches > 12){
		//distResult.Inches -= static_cast<int>(distResult.Feet/12);
		//totalInches -= 12;
		distResult.Feet++;
	}
		
	return distResult;
}
the buffer gives me a warning that totalInches is not used... is using totalInches out of the question or is there something i don't know about converting that variable??
I don't know which totalInches the compiler is referring to. The one in main, the one in Dist_To_Inches or the one in inches_To_Dist?

Maybe post your latest version of the code.
I think your program will work fine using totalInches once the algorithms are correct.
I get the warning message for totalInches all 3... The one in main, the one in Dist_To_Inches and the one in inches_To_Dist. I'm going to re-examine my prototypes cause i am wondering if the arguments there are causing the issue.

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

struct Distance
{
	int Miles;
	int Feet;
	int Inches;
};

//prototypes 
void Input_Dist(Distance &dist1); //Dist is our reference variable
Distance Input_Dist();  //inputs the second distance
int Validate_Dist(Distance);
void Display_Dist(Distance);     //Print and returns nothing
long Dist_To_Inches(Distance, Distance);
Distance inches_To_Dist(long int Distance);
void Print_Zero(int,int);



int main (){
	Distance dist1, dist2, distResult;
	long int totalInches;
	
	cout << "This program will read in two distances in MMM::FFFF::II format" << endl;
	cout << "then output the sum of both in MMM::FFFF::II format" << endl;
	cout << endl;
	
	for(int i=0; i<1; i++){
		Input_Dist(dist1);  //pass by reference
		cout<< endl;
		dist2 = Input_Dist();
		
		if (Validate_Dist(dist1) && Validate_Dist(dist2))
		{
			cout << "\nDistance 1: "; Display_Dist(dist1);
			cout << "\nDistance 2: "; Display_Dist(dist2);
			cout << "\nResult: "; Display_Dist(distResult);
			cout << "Invalid distance was entered";
			cout << endl;
			continue;
		}
		else
		{
			cout << "\nDistance 1: "; Display_Dist(dist1);
			cout << "\nDistance 2: "; Display_Dist(dist2);
			totalInches = Dist_To_Inches(dist1, dist2);
			totalInches = dist1.Inches + dist2.Inches;
			distResult = inches_To_Dist(totalInches);
			
			cout << "\nResult: "; Display_Dist(distResult);
			cout << endl;
		}
	}

	cout << endl;	

return 0;	
}

///////////////////////////////////////////////////////////////               Inputs the first distance value (miles, feet, and
void Input_Dist(Distance &dist1)  //Dist is now a reference                  // inches) and returns it using a reference parameter 
{																
	//cout promt user M,F,I; make clear for user
	cout << "Enter the first distance in MMM::FFFF::II format: ";   
	cin >> dist1.Miles >> dist1.Feet >> dist1.Inches;
	
}	
////////////////////////////////////////////////////////////                  // Inputs the second distance value and returns it using 
Distance Input_Dist()                                                         // ‘return’ statement
{
	Distance dist2;
	cout << "Enter the second distance in MMM::FFFF::II format: ";
	cin >> dist2.Miles >> dist2.Feet >> dist2.Inches;
	return dist2;
	
}
//////////////////////////////////////////////////////////////                // Accepts a distance value and returns a code indicating
int Validate_Dist(Distance d)                                                  //valid or invalid data
{
	int valDist;
	if (d.Miles > 999 || d.Feet > 5278 || d.Inches > 12){
		valDist = 1;//return true if valid
	}
	else{
		valDist = 0;// return false if invalid
	}
	return valDist;
}
///////////////////////////////////////////////////////////////               //Accepts a distance value (miles, feet, inches) for
void Display_Dist(Distance dd)                                                 // printing and returns nothing
{
	cout << dd.Miles <<"::" << dd.Feet << "::"<<dd.Inches << endl;
}
///////////////////////////////////////////////////////////////                    //Accepts miles, feet, inches and returns the
long Dist_To_Inches(Distance dist1, Distance dist2)                                // total inches
{
	//Distance d3;             //define a new structure for sum
	long int totalInches = 0;
	totalInches = dist1.Inches + dist2.Inches;
	totalInches += (12*(dist1.Feet + dist2.Feet));
	totalInches += (63360*(dist1.Miles + dist2.Miles));
	return totalInches;
}
////////////////////////////////////////////////////////////////                  //Accepts total inches and returns a distance value
Distance inches_To_Dist(long int totalInches)                                        // in miles, feet and inches                       
{                                                                               
	Distance distResult;
	//distResult.Miles =0;
	//distResult.Feet =0;
	//distResult.Inches=0;
	
	
	if(totalInches > 63360){
		//distResult.Inches -= static_cast<int>(distResult.Miles/63360);    
		totalInches -= 63360;
		++distResult.Miles;
	}
	if(totalInches > 12){
		//distResult.Inches -= static_cast<int>(distResult.Feet/12);
		totalInches -= 12;
		++distResult.Feet;
	}
		
	return distResult;
}
///////////////////////////////////////////////////////////////
void Print_Zero(int,int)
{
	//  Prints leading zeros if necessary.  Statement: cout << setw(10) << setfill('/') << setprecesion(2) << 3.141;
}
//////////////////////////////////////////////////////////////
I don't know enough C++ to know if this is bad or not (is it casting struct to long int?)

Distance inches_To_Dist(long int Distance);

but maybe change to

Distance inches_To_Dist(long int);

Since the local distResult in inches_To_Dist contains garbage you should uncomment these.
1
2
3
	//distResult.Miles =0;
	//distResult.Feet =0;
	//distResult.Inches=0; 


The rest of the function is possibly only incrementing whatever garbage was in distResult if you don't.
In my test, distResult was the same memory previously used by the local dist2 of Input_Dist() and so you are incrementing once the values of dist2 if the if conditions were satisfied.




Last edited on
Topic archived. No new replies allowed.