Student Programmer needs

Dec 4, 2013 at 12:43am
I am working on a program that displays a table showing the fahrenheit temp and and the Calsius temp with a function called celsius. This is what I have and I am getting this error:
1. Error 1 error C4700: uninitialized local variable 'num1' used

As far as I can tell i have initialized all my variables.

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
 using namespace std;

// Function Prototypes
double celsius(double num1, double num2);

int main()
{
	// Variables.
	char again;
    double  num1, num3, num2 = 0;
 do   
{
	cout  << "Celsius Conversion Table" << endl;
	cout << " Farenheit    Celsius " << endl;
	cout << "-----------------------" << endl;

	num3 = celsius(num1, num2);
	
	for (num2 = 0; num2 < 21; num2++)
	{cout << setw(5) << num2 << setw(15) << num3 << endl;
	}


  // Asks the user if they want to try again.
	cout << "Would you like to try again? (y or n)";
	cin >> again;
	 }while (again == 'Y' || again == 'y');

  system("pause");
  return 0;
}
	
/***************************************************************
*                          Celsius Function                    *
*This function accepts a Fahrenheit temp as an arguement and   *
*returns the temp in celsius.                                  *
***************************************************************/
double celsius(double num1, double num2)
{
	return num1 = 5/9*(num2-32) ;
}
//
Dec 4, 2013 at 1:01am
double num1, num3, num2 = 0;
only initialized num2 to 0. Use this instead:
1
2
double  num1, num3, num2;
num1 = num2 = num3 = 0;

or this if you prefer:
double num1=0, num3=0, num2 = 0;
Dec 4, 2013 at 2:02am
that gives me a -0 for all.
Dec 4, 2013 at 2:15am
That's because 5/9 = 0. Because both 5 and 9 are integers, it gives you an integer result. Write one as a floating pointer number and the calculation should work.
Dec 4, 2013 at 2:17am
I changed somethings around but all I get it -0 in the celcius column.
New Code
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
// Function Prototypes
double celsius(double num1, double num2);

int main()
{
	// Variables.
	char again;
    double  num1 = 5/9, num3, num2 = 0;
 do   
{
	cout  << "Celsius Conversion Table" << endl;
	cout << " Farenheit    Celsius " << endl;
	cout << "-----------------------" << endl;

	num3 = celsius(num1, num2);
	
	for (num2 = 0; num2 < 21; num2++)
	{cout << setw(5) << num2 << setw(15) << num3 << endl;
	}


  // Asks the user if they want to try again.
	cout << "Would you like to try again? (y or n)";
	cin >> again;
	 }while (again == 'Y' || again == 'y');

  system("pause");
  return 0;
}
	
/***************************************************************
*                          Celsius Function                    *
*This function accepts a Fahrenheit temp as an arguement and   *
*returns the temp in celsius.                                  *
***************************************************************/
double celsius(double num1, double num2)
{
	return  num1*(num2-32) ;
}
Dec 4, 2013 at 2:30am
Function celsius() accepts two parameters and returns another value as the result. Slow down a little and consider what it is that you would like this function to do. I think it should be a case of one value as input, a little manipulation, and then one value output. So that will make things a lot easier. You can dispense with one of the parameters. Remove it completely.

As for the uninitialised variable, look at it this way. The function celcius() is your eager servant. It awaits your command. All it asks is that you should feed it a value, and it will happily convert it to another scale, and return the result.

All you have to do is to ensure that some sort of useful input value is passed to the function when it is called. Merely setting the variables to zero isn't sufficient. What you need to do here is to pass a list of values, one at a time to the function and then handle whatever value it should return.
Dec 4, 2013 at 2:44am
Your program has multiple issues. First, why is celsius() taking two double arguments instead of one as per the description? Second is the previously mentioned integer division 5/9, which is truncated to zero. Write it as 5.0/9.0 (or 5/9.0 or 5.0/9) to prevent integer division. Third, num3 is only assigned a value outside of the for() loop on line 17, which is not what you want to do. Either assign it values inside the for() loop or dispense with num3 altogether and use cout to display the return value of the celsius() function.
Dec 4, 2013 at 6:11am
This is what I have right now it counts the Farhenheit correctly it does calculate the first Celsius correctly and then repeats the same number for the Celsius all the way through.

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
using namespace std;

// Function Prototypes
double celsius(double num1, double num2);

int main()
{
	// Variables.
	char again;
    double  num1 = 5.0/9.0, num3, num2 = 0;
 do   
{
	cout  << "Celsius Conversion Table" << endl;
	cout << " Farenheit    Celsius " << endl;
	cout << "-----------------------" << endl;

	num3 = celsius(num1, num2);
	
	for (num2 = 0; num2 < 21; num2++)
	{cout << setw(5) << num2 << setw(15) << num3 << endl;
	}


  // Asks the user if they want to try again.
	cout << "Would you like to try again? (y or n)";
	cin >> again;
	 }while (again == 'Y' || again == 'y');

  system("pause");
  return 0;
}
	
/***************************************************************
*                          Celsius Function                    *
*This function accepts a Fahrenheit temp as an arguement and   *
*returns the temp in celsius.                                  *
***************************************************************/
double celsius(double num1, double num2)
{
	return  num1*(num2-32) ;
}
Last edited on Dec 4, 2013 at 6:20am
Dec 4, 2013 at 3:36pm
For the for() loop on line 19, what value does num3 have? How would you go about changing its value for each iteration of the loop? Hint: all you have to do is move a line of code currently outside the for() loop to inside the for() loop.
Dec 4, 2013 at 4:21pm
That's what I was thinking last night before i went to bed, but not sure what to do. Can you give me a little more info cause I am at a loss and this was dues yesterday.
Last edited on Dec 4, 2013 at 4:28pm
Dec 4, 2013 at 4:38pm
Move line 17 into the for() loop, above the cout statement. Note that you'll only convert the same values (0-20 degrees F) to Celsius over and over until the user quits but if you're required to do more you have to do that on your own.
Dec 4, 2013 at 5:52pm
There's a design problem with function celsius().
Look at the comment in the code which describes the function:
1
2
3
4
5
/***************************************************************
*                          Celsius Function                    *
*This function accepts a Fahrenheit temp as an arguement and   *
*returns the temp in celsius.                                  *
***************************************************************/

Based upon that description I would expect the function header to look something like this:
double celsius(double num)
Whereas in fact it looks like this:
double celsius(double num1, double num2)

Now sometimes the correct response would be to alter the comment so that it correctly describes the function, maybe something like this:
1
2
3
* This function accepts a Fahrenheit temp and *
* some other number as arguments              *
* and returns the temp in celsius.            *  

But I consider that would be to move in the wrong direction. At the moment, the function has some of its innards exposed and floating about outside. The correct approach is to keep the inner mechanisms of the function self-contained, and limit the interface with the outside world to the bare essentials which are required for it to fulfil its purpose.

In other words, change the function from this:
1
2
3
4
double celsius(double num1, double num2)
{
    return  num1*(num2-32) ;
}

to this:
1
2
3
4
5
double celsius(double num)
{
    double  factor = 5.0/9.0;
    return  factor * (num-32);
}


I hope you can see why this is a better approach. It makes the function self-contained, and its usage simpler. Now there is no longer any need for the calling function to have additional special knowledge of what extra parameter it is supposed to pass.
Dec 4, 2013 at 10:01pm
ok I changed it again. Look at line 18 it says that num is not defined. What am i doing or not doing this thing is giving me a headache.
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
 

// Function Prototypes
double celsius(double num);

int main()
{
	// Variables.
	char again;
    double  num, num2 = 0;
 do   
{uninitialized local variable 'num' used
	cout  << "Celsius Conversion Table" << endl;
	cout << " Farenheit    Celsius " << endl;
	cout << "-----------------------" << endl;
	
	for (num2 = 0; num2 < 21; num2++)
	{cout << setw(5) << num2 << setw(15) << celsius(num) << endl;// it tells me here that num //is undefined, I thought it was done in already.
	}


  // Asks the user if they want to try again.
	cout << "Would you like to try again? (y or n)";
	cin >> again;
	 }while (again == 'Y' || again == 'y');

  system("pause");
  return 0;
}
/***************************************************************
*                          Celsius Function                    *
*This function accepts a Fahrenheit temp as an arguement and   *
*returns the temp in celsius.                                  *
***************************************************************/
double celsius(double num)
{
      double factor = 5.0/9.0;
    return  factor * (num-32);
}
Dec 4, 2013 at 10:10pm
Change the for loop like this:
1
2
3
4
    for (num2 = 0; num2 < 21; num2++)
    {
        cout << setw(5) << num2 << setw(15) << celsius(num2) << endl;
    } 

All I did was change num to num2 here.

Notice, the name of the variable used doesn't have to be the same as that in the function definition.
Last edited on Dec 4, 2013 at 10:11pm
Dec 5, 2013 at 12:29am
Chervil,
you are the best thank you so much. We have to combine the two programs into one separated by system("cls"); but when I do it tells me there are errors separately they work fine but together. I get three errors
1. 'calculateRetail' : local function definitions are illegal (It was fine before combining the programs.)
2. IntelliSense: expected a ';' (I know this is wrong)
3. IntelliSense: identifier "num2" is undefined ( it is!)

Knowing me its a curly brace or a semicolon that I am missing or have to much of please take a look at it.
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

/****************************************************************
* The first program is the Mark-up which uses a function that   * 
* receives the whole sale cost and markup percentage as         *
* arguments, and returns the retail price of the item.          *
****************************************************************/

// Function prototype for program 1
double calculateRetail( double num1, double num2);

// Function Prototypes for program 2
double celsius(double num);

int main()
{
	double whole_sale, percent, markup;
	char again;
	do
	{

	cout << "Whole sale price :$ ";
	cin >> whole_sale;

	cout << "Markup percentage :";
	cin >> percent;
	cout << endl;

	system("pause");
	system("cls");

	markup = calculateRetail(whole_sale, percent);

	//Display area.
	cout << "Total price of the item after markup is :$ " <<  markup << endl;

	 // Asks the user if they want to try again.
	cout << "Would you like to try again? (y or n)";
	cin >> again;
	 }while (again == 'Y' || again == 'y');

	system("pause");
	system("cls");

	/*************************************************************
	*				Double Calculate Retail                      *
	*	   This function returns the markup input by the user    *
	*************************************************************/

	double calculateRetail(double num1, double num2)
	{
		return (num1 *num2) + num1;
	}


	/************************************************************
	*                     Celsius Temp Table                    *
	* This program displays the temp in farhenheit and converts *
	* it to celsius using a function.                           *
	************************************************************/

	// Variables.
	
    double  num2 = 0;
	
 do   
{
	cout  << "Celsius Conversion Table" << endl;
	cout << " Farenheit    Celsius " << endl;
	cout << "-----------------------" << endl;
	
	for (num2 = 0; num2 < 21; num2++)
	{cout << setw(5) << num2 << setw(15) << fixed << showpoint << setprecision(2) << celsius(num2) << endl;
	}


  // Asks the user if they want to try again.
	cout << "Would you like to try again? (y or n)";
	cin >> again;
	 }while (again == 'Y' || again == 'y');

  system("pause");
  return 0;
}
/***************************************************************
*                          Celsius Function                    *
*This function accepts a Fahrenheit temp as an arguement and   *
*returns the temp in celsius.                                  *
***************************************************************/
double celsius(double num)
{
      double factor = 5.0/9.0;
    return  factor * (num-32);
}
Last edited on Dec 5, 2013 at 1:00am
Dec 5, 2013 at 1:21am
You have to move the definition for calculateRetail() out of main(), I'd put it right above the definition for celsius(). Once you do that the other errors might go away.
Dec 5, 2013 at 8:19am
That was it thank you.
Topic archived. No new replies allowed.