Error

This is my assignment: Write function doFrac that returns a string representing a common fraction given its numerator and denominator as input arguments and also returns the decimal value of the function. Here is my code. I am getting an error "uninitialized local variable 'denominator' used" on line 21 where my doFrac function is in main. Can anyone help me?

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
  // Lab 10.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <sstream>

using namespace std;

void doFrac(int, int, string&, float&);

int main()
{
	int numerator, denominator; // In
	string str; // Out
	float value; // Out

	cout << "Enter a fraction to convert to a decimal: ";
	cin >> numerator, denominator;
	
	doFrac(numerator, denominator, str, value);

	cout << "The decimal is: " << str << value << endl;
	
	int i;
	cin >> i;

    return 0;
}

// Function doFrac
void doFrac(int numerator, int denominator,  //In
	string& str, float& value) //Out
{
	// String stream variable
	stringstream ss;

	// Returns fraction value
	ss << numerator;
	str += ss.str();
	str += "/";
	ss << denominator;
	str += ss.str();

	// Return decimal value
	value = (float)numerator / denominator;

}
Last edited on
Check your syntax for reading multiple values from cin:
cin >> numerator >> denominator;
Use another >> instead of comma.

 
cin >> numerator >> denominator;

Thank you both! That fixed it all. I knew that, but I can't tell you how many times I missed that simple comma to change it to >> lol! Sometimes you just need a second pair of eyes to see these simple little mistakes. I can't thank you both enough!
Topic archived. No new replies allowed.