# Variable Being Used Without Being Initialized

Page 1 of 1

## 6 Replies - 1582 Views - Last Post: 20 November 2012 - 05:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=300872&amp;s=bf58db44cc8af22c3930b130f02633bd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 GrowingDark

Reputation: 3
• Posts: 44
• 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);

}

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

• Saucy!

Reputation: 6237
• Posts: 24,002
• 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.

### #3 GrowingDark

Reputation: 3
• Posts: 44
• 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)
```

### #4 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• 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

### #5 GrowingDark

Reputation: 3
• Posts: 44
• 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

### #6 Mrk

Reputation: 29
• 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);

```

### #7 Xupicor

• Nasal Demon

Reputation: 380
• Posts: 973
• 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.