Hi, I've created a program meant for submission for my final project but when i ran it, it shows that the variable being used without being initialized for quite a few time, i need it urgently but i am unable to solve it. Any advice? My program is below.
#include<stdio.h>
#include<conio.h>
void Kahui(float dollar, char choice);
void Qixiang(float hours, float rate);
void main(void)
{
float dollars;
float hours, rate;
char answer, choice;
printf("Welcome to menu selection!");
printf("\nPlease choose one option\nA.Kahui's programme.\nB.Qixiang's programme.\nC.Quit\n");
printf("Enter your choice: ");
scanf("%c", &answer);
switch(answer)
{
case 'A' :
Kahui(dollars, choice);
break;
case 'B' :
Qixiang(hours, rate);
break;
case 'C' :printf("You've selected to quit this programe\n");
printf("Press any key to continue :)");
break;
default: printf("Invalid choice, choice must be A,B or C");
}
if(choice=='1'){malaysia=dollars*2.4; printf("Money in Malaysia: %0.1f\n",malaysia);}
else if(choice=='2') { china=dollars*4.82; printf("Money in China:%0.1f\n", china);}
else if (choice=='4'){ korea=dollars*888.96; printf("Money in Korea:%0.1f\n,",korea);}
else if (choice=='5'){ taiwan=dollars*23.64; printf("Money in Taiwan:%0.1f\n,",taiwan);}
else if (choice=='6'){ hongkong=dollars*6.12; printf("Money in HongKong:%0.1f\n,",hongkong);}
else if (choice=='7'){ US=dollars*0.79; printf("Money in US:%0.1f\n,",US);}
else if (choice=='3'){ japan=dollars/880.60; printf("Money in Japan:%0.1f\n",japan);}
else if (choice=='8'){ southafrica=dollars*7.73; printf("Money in South Africa:%0.1f\n",southafrica);}
else if (choice=='9'){ thailand=dollars*23.80; printf("Money in Thailand:%0.1f\n",thailand);}
else if (choice=='a'){ russia=dollars*24.87; printf("Money in Russia:%0.1f\n",russia);}
else if (choice=='b'){ philippine=dollars*33.41; printf("Money in Philippine:%0.1f\n",philippine);}
else if (choice=='c'){ india=dollars*44.26; printf("Money in India:%0.1f\n",india);}
else if (choice=='d'){ newzealand=dollars*0.97; printf("Money in Newzealand:%0.1f\n",newzealand);}
else if (choice=='e'){ indonesia=dollars*7730.28; printf("Money in Indonesia:%0.1f\n",indonesia);}
else if (choice=='f'){ vietnam=dollars*16522.22; printf("Money in Vietnam:%0.1f\n",vietnam);}
else if (choice=='g'){ sierraleone=dollars*3408.31; printf("Money in Sierra Leone:%0.1f:",sierraleone);}
else { printf("Invalid choice ! ");}
}
void Qixiang(float hours, float rate)
{
float pay, overtime, total;
printf("You've selected Qixiang's programme--Calculate your pay!\n");
printf("Enter the number of hours worked in this week:");
scanf("%f", &hours);
printf("Enter the pay rate:");
scanf("%f", &rate);
if (hours>30)
{
pay = rate*30;
overtime = 1.5*rate*(hours-30);
total = pay + overtime;
printf("First 30-hours pay is %.2lf\n", pay);
printf("your Overtime pay for is $%.2f\n", overtime);
printf("Total pay for this week is $%.2f",total);
}
else
{
pay = hours*rate;
printf("No overtime pay.\n");
printf("Total pay for the week is $%.2f", pay);
printf("\nYou need to work above 30 hours to get overtime pay !! ");
}
}
Anyway... this error is pretty self explanitory. If you don't initialize your variables, they have garbage. Therefore if you attempt to print/read/use them before assigning them a value, you will get garbage.
Are you just getting an "uninitialized variable" warning or are you having it disturb the flow of your program?
You're main problem in the flow of this program is that you define hours/rate/dollars in main, don't initialize them, pass them to Kahui/Qixiang as arguments, write to them in those functions, and then use them.
Here are two suggested options to improve your design and to avoid the warnings:
1. Remove the parameters from Kahui/Qixiang. Make them void Kahui() functions, and delete hours/rate/dollars from main().
2. scanf into hours/rate/dollars in the main instead of in the functions. Then you are passing the initialized values into the functions.