Help Please: I keep getting errors for this program and i dont qutie understand why. Help would be much appreciated :)

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
// Converting Ft and In: This program will convert user input from feet and inches to
// meters and centimeters.
#include "stdafx.h"
#include <iostream>
using namespace std;

void input( int& feet, int& inches);
void output(int meters, int cetimeters);
void convert(int feet, int inches, int& meters, int&centimeters);

const double meterperfoot = 0.3048;
const int centimeterspermeter = 100;
const int inchesperfoot = 12;
int main()
{
{int feet; 
int inches;
int meters;
int centimeters;
char menu;
do 
{
	cout << "Continue? (Y/N)";
	cin >> menu;
	input( feet, inches);
	convert(feet, inches, meters, centimeters);
	output( meters, centimeters);
} 
while (menu == 'Y' || menu == 'y');

input(feet,inches);
cout << "Type the feet for the length:";
cin >> feet;
cout << "Type the inches for the length:";
cin >> inches;


convert( feet,inches,meters,centimeters);
inches += 12 * feet;
centimeters = inches * 2.54;
meters = centimeters/100;
centimeters -= meters*100;

output(meters,centimeters);
cout << "The converted length is:" << meters << "meters and" << centimeters << "centimeters" << endl;


 
	return 0;
}
Last edited on
Please use code tags - Alot easier to read for us
The thing that pops up most is that you have prototypes for the functions but you havent actually implemented them ( I think thats the right word )
Also tell us what error you're getting
The error im getting is saying The variable centimeters is being used without being initialized. The thing is that i thought i was implementing them.
In the following section of code, correct me if I'm wrong -- it looks as though you mean to declare these bits of code as your function definitions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
input(feet,inches);
cout << "Type the feet for the length:";
cin >> feet;
cout << "Type the inches for the length:";
cin >> inches;


convert( feet,inches,meters,centimeters);
inches += 12 * feet;
centimeters = inches * 2.54;
meters = centimeters/100;
centimeters -= meters*100;

output(meters,centimeters);
cout << "The converted length is:" << meters << "meters and" << centimeters << "centimeters" << endl;


If so, then you don't have the syntax quite right. The following line means to *call* the named function:

 
convert( feet,inches,meters,centimeters);


The syntax for a function *definition* (i.e., what the function actually does) is as follows:

1
2
3
type name(parameters) {
    // Stuff to do
}


Where "type" is the return type of the function", "name" is its name, "parameters" are the types and names of the parameters (note that these names are only local to the function -- the caller can and may very well call them something different -- they just need to match in type!), and "Stuff to do" is what you want the function to do. Note the lack of semicolon after the function name+parameters, and the braces around the function content. Look at the way "main" is defined -- it's a function just like any other (except that it is special as the initialization point of your program), and it is structured correctly. Additionally, you'll want to move the function definitions outside of main -- you can't nest a function definition inside of another one -- except in some atrocities, like JavaScript. :)

I'm not sure what the problem is with your "centimeters" variable, but I would suggest correcting this and seeing where that gets you.

Hope that helps :)
Are you meaning like this? If so i keep getting the same error..


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
//  Converting Ft and In: This program will convert user input from feet and inches to
// meters and centimeters.
#include "stdafx.h"
#include <iostream>
using namespace std;


void output(int meters, int centimeters);
void convert(int feet, int inches, int& meters, int& centimeters);

const double meterperfoot = 0.3048;
const int centimeterspermeter = 100;
const int inchesperfoot = 12;
int main()
{
{int feet; 
int inches;
int meters;
int centimeters;
char menu;
do 
{
	cout << "Continue? (Y/N)";
	convert(feet, inches, meters, centimeters);
	output( meters, centimeters);
} 
while (menu == 'Y' || menu == 'y');
 

cout << "Type the feet for the length:";
cin >> feet;
cout << "Type the inches for the length:";
cin >> inches;


void convert ();
{inches= 12 * feet + inches;
centimeters = inches * 2.54;
meters = centimeters/100;
centimeters = meters-centimeters*100;}

void output ();
 {cout << "The converted length is:" << meters << "meters and" << centimeters << "centimeters" << endl;}


	return 0;
}


Last edited on
I think you need to have a slight refresher on functions.

If your function prototype is void output(int meters, int centimeters); then your definition would look something like this:
1
2
3
4
void output( int meters, int centimeters )
{
   cout << "The converted length is: " << meters << " meters and " << centimeters << " centimeters.\n";
}


You need to drop the terminator that you have and make sure your definition parameters match your declaration parameters.
Last edited on
The first problem is the opening brace on line 16 that's never closed.

Your function must be outside main() and match the prototypes
I think you need to have a slight refresher on functions.


I agree with iHutch. Taking a closer look at your code, I would say that you don't understand the purpose/significance of semicolons and braces are in C/C++, and hence where they should be used. I would suggest carefully studying the following tutorial, especially the first section where it explains the basics of defining and calling a function, and if you have any specific questions, we can help with that:

http://www.cplusplus.com/doc/tutorial/functions/
Is this any better then before..Now im getting an error about inches not being called.

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
//  Converting Ft and In: This program will convert user input from feet and inches to
// meters and centimeters.
#include "stdafx.h"
#include <iostream>
using namespace std;


void output(int meters, int centimeters)
	{cout << "The converted length is:" << meters << " meters and" << centimeters << " centimeters";

 }
void convert(int feet, int inches, int& meters, int& centimeters)
{
inches= 12 * feet + inches;
centimeters = inches * 2.54;
meters = centimeters/100;
centimeters = meters-centimeters*100;
}
const double meterperfoot = 0.3048;
const int centimeterspermeter = 100;
const int inchesperfoot = 12;
int main()
{
int feet; 
int inches;
int meters;
int centimeters;

cout << "Type the feet for the length:";
cin >> feet;
cout << "Type the inches for the length:";
cin >> inches;

void convert (int feet, int inches, int& meters, int& centimeters);

void output (int meters, int centimeters);

char menu;
do 
{
	cout << "Continue? (Y/N)";
	convert(feet, inches, meters, centimeters);
	output( meters, centimeters);
} 
while (menu == 'Y' || menu == 'y');
 
	return 0;
}
Last edited on
Thanks for the help you guys :) I think i got it now~
Topic archived. No new replies allowed.