howthe main () function that will request input for value in inch from the user

[code]
float convert_cm (float inch)
{
float cm;

cm = inch * 2.54f;
cout << "Inch : " << inch << endl << endl;

return cm;
}
[code]
The above function is to convert the value in the parameter to centimeter and returns the converted value.

Write the main () function that will request input for value in inch from the user. The program will call the above function to convert the input to centimeter and displays the value.
You already hav written the correct thing but....
1
2
3
4
cm = inch * 2.54f;
cout << "Inch : " << inch << endl << endl;

return cm;


The cout is not required.....just return :D
See notes below, please make sure you read the tutorials. I would hate to think I did your homework for you, as that wouldn't be helping you at all.


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
// Solution
#include <iostream>

using namespace std;

float convert_cm(float inch)
{
  float cm;
  cm = inch * 2.54f;
  cout << "Inch : " << inch << endl << endl;
  cout << "CM : " << cm << endl << endl;
  return cm;
}

int main()
{
  std::cout << "Value in inches >> ";
  double inches = 0.0;
  std::cin >> inches;

  double cm = convert_cm(inches);
  std::cout << inches << "inches == " << cm << "cm." << std::endl;
  return (0);
}

//Notes:

#if 0

#include <iostream>

// http://www.cplusplus.com/doc/tutorial/functions/
double convert_cm(double inches) // Prefer "double" to "float"
{
  // Prefer concise usage over gratuituous variables
  return (inches * 2.54);
}

// http://www.cplusplus.com/doc/tutorial/program_structure/
int main()
{
  // http://www.cplusplus.com/doc/tutorial/basic_io/
  std::cout << "Value in inches >>";
  double inches = 0.0;
  std::cin >> inches;
  std::cout << inches << "inches == " << convert_cm(inches) << "cm." << std::endl;
  return (0);
}

#endif 
Topic archived. No new replies allowed.