6 Replies - 1020 Views - Last Post: 20 November 2012 - 05:46 AM Rate Topic: -----

#1 GrowingDark  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 09-March 11

Variable Being Used Without Being Initialized

Posted 19 November 2012 - 03:09 PM

Another hopefully quick question. I have to write a program to convert integer feet and inches into double total feet. I'm only supposed to use one input function to input both integers at the beginning. I'm probably doing it wrong because the way I've written it, it says the variable feet is being used without being initialized. So I guess my main question is how do you output two integers from a function into two variables in main?

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

int input();
double totalFeet(int feet, int inches);
void output(int feet, int inches, double totFeet);
char choice();

int main()
{
	int feet, inches;
	double totFeet;
	char restart;

	cout <<setiosflags(ios::fixed | ios::showpoint);
	cout <<setprecision(2);

	do
	{
		feet, inches = input();
		totFeet = totalFeet(feet, inches);
		output(feet, inches, totFeet);
		restart = choice();
		restart = tolower(restart);
	} while(restart == 'r');

	return 0;

}

int input()
{
	int feet, inches;

	cout << "\nPlease enter total feet: ";
	cin >> feet;
	cout << "Please enter total inches: ";
	cin >> inches;

	return feet, inches;
}

double totalFeet(int feet, int inches)
{
	double totFeet;

	totFeet = feet + (inches * .083333);

	return totFeet;
}

void output(int feet, int inches, double totFeet)
{
	cout << endl << endl << feet << " feet and " << inches << " inches are " << totFeet << " Total Feet.\n\n";
}

char choice()
{
	char choice;
	cout << "Enter 'r' to restart or any other key to quit: ";
	cin >> choice;

	return choice;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Variable Being Used Without Being Initialized

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,549
  • Joined: 23-August 08

Re: Variable Being Used Without Being Initialized

Posted 19 November 2012 - 03:25 PM

You can't return two values from a function like that. Read the tutorials in my signature.
Was This Post Helpful? 0
  • +
  • -

#3 GrowingDark  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 09-March 11

Re: Variable Being Used Without Being Initialized

Posted 19 November 2012 - 07:57 PM

Well, I couldn't find the answer in your tutorials, but I did figure it out. I used a void function.

void get_input(int& feet, int& inches)

Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Variable Being Used Without Being Initialized

Posted 19 November 2012 - 08:40 PM

See basically the idea of a function syntax is like this

return_type function name( parameters if any)
{
//code
}


So when you say

void get_input(int& feet, int& inches)


it means that, you are returning nothing, hence the term void.

Consider this code

int get_input(int& feet, int& inches)
{
//code which operates on above parameters and stores in a variable 'result' which is an integer
return result;
}


In the above code, since return type is int, it returns an integer value.

regards,
Raghav

This post has been edited by raghav.naganathan: 19 November 2012 - 08:41 PM

Was This Post Helpful? 0
  • +
  • -

#5 GrowingDark  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 09-March 11

Re: Variable Being Used Without Being Initialized

Posted 19 November 2012 - 09:41 PM

That's not quite right because I need to return two integers, not one result. In that void function I had the user input the the integers and it assigns them to whatever parameters I enter in main.

This is the final code I came up with:

void get_input(int& feet, int& inches)
{
	cout << "\nPlease enter total feet: ";
	cin >> feet;
	cout << "Please enter total inches: ";
	cin >> inches;
}


and the function call in Main:

get_input(feet, inches);


That way whatever the user inputs into the function is assigned to the variables I use in the function call.

This post has been edited by GrowingDark: 19 November 2012 - 09:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 Mrk  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 66
  • Joined: 03-December 09

Re: Variable Being Used Without Being Initialized

Posted 19 November 2012 - 10:20 PM

Yeah, that's the way to do it. You are using references. You declare feet and inches in main() and then your function can directly manipulate them. And just so you know, you could have written it like this:
void get_input(int& hello, int& goodbye)
{
	cout << "\nPlease enter total feet: ";
	cin >> hello;
	cout << "Please enter total inches: ";
	cin >> goodbye;
}


And in main():
get_input(feet, inches);


Was This Post Helpful? 0
  • +
  • -

#7 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Variable Being Used Without Being Initialized

Posted 20 November 2012 - 05:46 AM

You could also return a std::pair, std::tuple, std::vector, or your custom struct/class type, if you don't like the above solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1