Output always 0 in function

Hi I'm new to this site. You guys seem helpful though. I am taking a beginners class and this stuff doesn't click with me too well. I followed my logic step by step with commenting out and outputting everything and everything works well except in my final function when I output, everything becomes 0. Any help would be greatly appreciated.


/*
File: convert2metric.cpp
Created by: ???
Creation Date: ???
Synopsis:
This program reads in a length yards, feet and inches,
and converts to meters and centimeters.
*/

#include <cmath>
#include <cstdlib>
#include <iostream>

using namespace std;

// FUNCTION PROTOTYPE FOR read_us_length

int read_us_length(int & yards, int & feet, int & inches);

// FUNCTION PROTOTYPE FOR convert2inches

int convert2inches(int yards, int feet, int inches);

// FUNCTION PROTOTYPE FOR convert2metric

int convert2metric(int & total_inches, int meters, int centimeters);

// FUNCTION PROTOTYPE FOR write_metric_length

void write_metric_length(int meters, int centimetersB);

int main()
{
int yards, feet, inches; // length in yards, feet and inches
int total_inches; // total length in inches
int meters, centimeters; // length in meters and centimeters

read_us_length(yards, feet, inches);
total_inches = convert2inches(yards, feet, inches);
convert2metric(total_inches, meters, centimeters);
write_metric_length(meters, centimeters);

return 0;
}

// DEFINE FUNCTION read_us_length HERE:

int read_us_length(int & yards, int & feet, int & inches)
{
cout << "Enter number of yards: ";
cin >> yards;

if (yards < 0)
{
cout << "Illegal negative value " << yards << " for yards." << endl;
exit(0);
}

cout << "Enter number of feet: ";
cin >> feet;

if (feet < 0)
{
cout << "Illegal negative value " << feet << " for feet." << endl;
exit(1);
}

cout << "Enter number of inches: ";
cin >> inches;

if (inches < 0)
{
cout << "Illegal negative value " << inches << " for inches." << endl;
exit(2);
}
}
// DEFINE FUNCTION convert2inches HERE:

int convert2inches(int yards, int feet, int inches)
{
int total_inches;
total_inches = yards * 36.0 + feet * 12.0 + inches;

return(total_inches);
}

// DEFINE FUNCTION convert2metric HERE

int convert2metric(int & total_inches, int meters, int centimeters)
{
int centimetersB;

centimeters = total_inches * 2.54;
meters = centimeters / 100.0;
centimetersB = centimeters % 100;
}

// DEFINE FUNCTION write_metric_length HERE:

void write_metric_length(int meters, int centimetersB)
{
cout << "" << meters << " meters, " << centimetersB << " centimeters." << endl;
}
closed account (D80DSL3A)
I think your main problem is with the convert2metric() function. Pass meters and centimeters by reference so the values get changed by the function. Also, work with just centimeters in the function, not the local centimetersB.

Another issue is that two of the functions are declared as returning an integer value but there are no return statements in them. Are you getting no warnings about this? These functions should have a void return type though as no use is made of any returned value in main() anyways.

EDIT: I continue to be amazed at the frequency of the ninja'd phenomenon!
3-1/2 hours with no replies then 2 in the same minute. Wow!
Last edited on
There is no return statement in the convert2metric function, otherwise the parameters meters & centimeters are not passed as references - which is what you really need as returning an int isn't sufficient.

Can you please always use code tags - select you code then press the <> button on the right under the format menu.

This helps us because it shows line numbers & preserves formatting - making it easier to read.

Topic archived. No new replies allowed.