I need some help with uninitialized local variables
Feb 8, 2013 at 9:24pm UTC
I'm working on a homework assignment for my college c++ class. I'm new to c++ and have only been programming in java in my first semester of college with no prior experience. In this program, I'm having a compiler error that says the local variable "lower" is no being initialized. I am almost sure it is, and I have tried everything I can within my scope of ideas and I have no clue what is wrong. The variable in question is in the function "myProd." Thank you so much for the help
NOTE: The professor is teaching us how to use functions, and specifically stated that we cannot use global 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 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#include<iostream>;
using std::cout;
using std::endl;
using std::cin;
int myPrompt(void )
{
bool variable = true ;
int input;
cout << "Please input a non negative integer: " ;
cin >> input;
while (variable == true )
{
if (input <=-1)
{
cout << "Your input of " << input << " is not a valid integer. Please input a non negative integer: " ;
cin >> input;
}
else
{
variable = false ;
}
}
return (input);
}
int mySum(int input2)
{
int temp = 0;
int temp2 = 0;
int value = input2;
for (int i = 0; i <= value; i++)
{
temp = i + temp2;
temp2 = temp;
}
return temp2;
}
int myProd(int input3)
{
int value2 = input3;
int upper = value2;
int temp4 = 1;
int lower = 1;
int temp3 = 1;
if (upper == 1)
{
return 1;
}
else
{
temp4 = 1 * (1 + 1);
lower = 1 + 2;
while (lower <= upper)
{
int temp3 = temp4 * lower;
int temp4 = temp3;
int lower = lower + 1;
}
value2 = temp3;
return value2;
}
}
int myMod(int input4)
{
int value3 = input4;
value3 = value3 % 7;
return value3;
}
int main()
{
int input5 = myPrompt();
while (input5 != -9999)
{
if (input5 <= 9)
{
cout << "The product from 1 to " << input5 << " is " << myProd(input5) << endl;
}
if ( (input5 >= 10) && (input5 <= 50) )
{
cout << "The sum from 1 to " << input5 << " is " << mySum(input5) << endl;
}
if (input5 >= 51)
{
cout << "The remainder of " << input5 << " / 7 is " << myMod(input5) << endl;
}
input5 = myPrompt();
}
}
Feb 8, 2013 at 9:29pm UTC
1 2 3
int temp3 = temp4 * lower;
int temp4 = temp3;
int lower = lower + 1;
You need to remove "int" from these lines. You are actually declaring new variables rather than modifying existing ones.
Feb 8, 2013 at 9:45pm UTC
That fixed it. Thanks a lot!
Topic archived. No new replies allowed.