Can someone please explain why the .exe program is not running despite the turbo c++ compiler not showing any error?
the compiler doesnt show any error
#include <stdio.h>
#include <conio.h>
int main ()
{
int choice;
float num, result;
printf("Select your choice\n");
printf("Press 1 for conversion from milligrams to grams\n");
printf("Press 2 for conversion from decigrams to grams\n");
printf("Press 3 for conversion from centigrams to grams\n");
printf("Press 4 for conversion from kilograms to grams\n");
printf("Press 5 for conversion from ounce to grams\n");
printf("Press 6 for conversion from pounds to grams\n");
printf("Press 7 for conversion from ton to grams\n");
switch(choice) //i thought the switch statement would be appropriate
{
case 1:
printf("Enter the weight in milligrams for conversion\n");
scanf("%f", &num);
result=num*0.001;
printf("The weight in after conversion is %f g", result);
getch();
getch();
break;
case 2:
printf("Enter the weight in decigrams for conversion\n");
scanf("%f", &num);
result=num*0.1;
printf("The weight in after conversion is %f g", result);
getch();
getch();
break;
case 3:
printf("Enter the weight in centigrams for conversion\n");
scanf("%f", &num);
result=num*0.01;
printf("The weight in after conversion is %f g", result);
getch();
getch();
break;
case 4:
printf("Enter the weight in kilograms for conversion\n");
scanf("%f", &num);
result=num*1000.0;
printf("The weight in after conversion is %f g", result);
getch();
getch();
break;
case 5:
printf("Enter the weight in ounce for conversion\n");
scanf("%f", &num);
result=num*28.3495;
printf("The weight in after conversion is %f g", result);
getch();
getch();
break;
case 6:
printf("Enter the weight in pounds for conversion\n");
scanf("%f", &num);
result=num*453.592;
printf("The weight in after conversion is %f g", result);
getch();
getch();
break;
case 7:
printf("Enter the weight in ton for conversion\n");
scanf("%f", &num);
result=num*907185.00;
printf("The weight in after conversion is %f g", result);
getch();
getch();
break;
default:
printf("invalid choice\n");
break;
}
return 0;
}
i am relatively new to c to i dont know what is my mistake
Your variable choice is uninitialized and never written to. After prompting the user for input, you need to actually scan for the entered value.
You should use scanf before the switch.
It doesn't work because you did not assign any value to 'choice'.
use only one scanf and use it before switch.
Get the input for the user choice first before checking it in switch statement. Hope this helps
#include <stdio.h>
#include <conio.h>
int main ()
{
int choice;
float num, result;
printf("Select your choice\n");
printf("Press 1 for conversion from milligrams to grams\n");
printf("Press 2 for conversion from decigrams to grams\n");
printf("Press 3 for conversion from centigrams to grams\n");
printf("Press 4 for conversion from kilograms to grams\n");
printf("Press 5 for conversion from ounce to grams\n");
printf("Press 6 for conversion from pounds to grams\n");
printf("Press 7 for conversion from ton to grams\n");
scanf("%d",&choice); //Added this line
switch(choice) //i thought the switch statement would be appropriate
{
case 1:
printf("Enter the weight in milligrams for conversion\n");
scanf("%f", &num);
result=num*0.001;
printf("The weight in after conversion is %f g", result);
getch();
break;
case 2:
printf("Enter the weight in decigrams for conversion\n");
scanf("%f", &num);
result=num*0.1;
printf("The weight in after conversion is %f g", result);
getch();
break;
case 3:
printf("Enter the weight in centigrams for conversion\n");
scanf("%f", &num);
result=num*0.01;
printf("The weight in after conversion is %f g", result);
getch();
break;
case 4:
printf("Enter the weight in kilograms for conversion\n");
scanf("%f", &num);
result=num*1000.0;
printf("The weight in after conversion is %f g", result);
getch();
break;
case 5:
printf("Enter the weight in ounce for conversion\n");
scanf("%f", &num);
result=num*28.3495;
printf("The weight in after conversion is %f g", result);
getch();
break;
case 6:
printf("Enter the weight in pounds for conversion\n");
scanf("%f", &num);
result=num*453.592;
printf("The weight in after conversion is %f g", result);
getch();
break;
case 7:
printf("Enter the weight in ton for conversion\n");
scanf("%f", &num);
result=num*907185.00;
printf("The weight in after conversion is %f g", result);
getch();
break;
default:
printf("invalid choice\n");
break;
}
return 0;
}
Plus I didn't understand the used format specifier scanf("%f", &num); in your case, I have used scanf("%f",&num);
Related
Using just the switch statement works totally fine. My problem comes when inserting the do-while loop. On the first try, the program works smoothly, but in the second and following loops, after asking the user if he wants to try again by typing 1, the program "jumps" the part where the user writes its input (scanf("%d", &oper)) and executes the rest of the program considering the previously typed 1 as the input and messing up everything.
I´ve tried looking for the answer on similar questions and videos, but I´m quite new and I just can´t grasp it. I have another program with a similar problem. I will really appreciate your help :)
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
do{
printf("Press 0 to stop and 1 to continue");
scanf("%d", &select);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;}
}while(select == 1);
} ```
I think you want something like bellow code.
Try this:
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
do{
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;
}
printf("Press 0 to stop and 1 to continue: ");
scanf("%d", &select);
} while(select == 1);
printf("Finish");
}
change the following code bellow:
while(select != 0);
Try this:
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
do{
printf("Press 0 to stop and 1 to continue");
scanf("%d", &select);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;
}
}while(select != 0);
}
I'm getting an error which I am not able to resolve. I've gone through my code thoroughly with no success. What am I doing wrong? See code below.
Compiler error:
In function 'main':
ou1.c:49:1: error: expected 'while' before 'printf'
printf("End of program!\n");
^
My code:
#include <stdio.h>
int main(void){
int choice;
float price, sum, SUMusd;
float rate =1;
printf("Your shopping assistant");
do{
printf("1. Set exchange rate in usd (currency rate:%f)\n", rate);
printf("2. Read prices in the foreign currency\n");
printf("3. End\n");
printf("\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Give exchange rate: \n");
scanf("%f", &rate);
break;
case 2:
do{
printf("Give price(finsh with < 0)\n");
scanf("%f", &price);
sum =+ price;
}while(price <= 0);
SUMusd = sum*rate;
printf("Sum in foreign currency: %f", sum);
printf("Sum in USD:%f", SUMusd);
break;
default:
printf("Invalid choice\n");
break;
}while(choice != 3);
}
printf("End of program!\n");
return 0;
}
The curly braces of the switch statement need to be closed before the while loop termination.
printf("Invalid choice\n");
break;
}
}while(choice != 3);
printf("End of program!\n");
Corrected full code sample
#include <stdio.h>
int main(void){
int choice;
float price, sum, SUMusd;
float rate =1;
printf("Your shopping assistant");
do{
printf("1. Set exchange rate in usd (currency rate:%f)\n", rate);
printf("2. Read prices in the foreign currency\n");
printf("3. End\n");
printf("\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Give exchange rate: \n");
scanf("%f", &rate);
break;
case 2:
do{
printf("Give price(finsh with < 0)\n");
scanf("%f", &price);
sum =+ price;
}while(price <= 0);
SUMusd = sum*rate;
printf("Sum in foreign currency: %f", sum);
printf("Sum in USD:%f", SUMusd);
break;
default:
printf("Invalid choice\n");
break;
}
}while(choice != 3);
printf("End of program!\n");
return 0;
}
I'm trying get the variables: float managerTotal, hourlyTotal, commissionTotal, and pieceworkerTotal from their respective functions to print in the switch statement case: 'q' in the choiceInput function. Yesterday I had lots and lots of errors and now I have them passing through but when I enter an input for the variables it doesn't pass the value through, they only return as 0. Please can someone help explain what I'm doing wrong. Thank you.
Here is the code:
#include<stdio.h>
#include<conio.h>
void welcome() {
printf("-----------------------------------------------\n");
printf("Welcome to the Factory A Payroll\n");
}
void choosePayroll() {
printf("-----------------------------------------------\n");
printf("Press '1' to access the Manager's payroll\n");
printf("Press '2' to access the Hourly worker's payroll\n");
printf("Press '3' to access Commission worker's payroll\n");
printf("Press '4' to access Pieceworkers's payroll\n");
printf("Press 'Q' to access Manager's payroll\n");
printf("-----------------------------------------------\n");
choiceInput();
}
float managerIntro() {
float managerTotal;
char charReturn = 0;
printf("-----------------------------------------------\n");
printf("You have selected 'Manager's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter your fixed weekly salary: $");
scanf_s("%f", &managerTotal);
printf("-----------------------------------------------\n");
printf("You have entered $%.2f as the weekly fixed salary\n", managerTotal);
printf("-----------------------------------------------\n");
printf("Press 'R' to return back to the main menu\n\n");
scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return managerTotal;
}
float hourlyIntro() {
char charReturn = 0;
float hourlyWage = 0;
float hoursWorked = 0;
float overTimeHours = 0;
float normalPay = 0;
float overTimePay = 0;
float hourlyTotal;
printf("-----------------------------------------------\n");
printf("You have selected 'Hourly worker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the hourly wage for the worker: $");
scanf_s("%f", &hourlyWage);
printf("-----------------------------------------------\n");
printf("You have entered $%.2f as the hourly wage.", hourlyWage);
printf("\n-----------------------------------------------");
printf("\nPlease enter how many hours the worker has worked this week: ");
scanf_s("%f", &hoursWorked);
printf("\n-----------------------------------------------");
if (hoursWorked > 40) {
overTimeHours = hoursWorked - 40;
hoursWorked = 40;
}
normalPay = hourlyWage * hoursWorked;
overTimePay = (hourlyWage * 1.5) * overTimeHours;
hourlyTotal = normalPay + overTimeHours;
printf("\nNormal weekly pay : $%.2f\nOvertime weekly pay: $%.2f\n\nTotal weekly pay: $%.2f\n\n", normalPay, overTimePay, hourlyTotal);
printf("Press 'R' to return back to the main menu\n");
scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return hourlyTotal;
}
float commissionIntro() {
char charReturn = 0;
float basicSalary = 250;
float itemAPrice = 0;
float itemBPrice = 0;
float itemCPrice = 0;
float itemAQuantity = 0;
float itemBQuantity = 0;
float itemCQuantity = 0;
float itemACommission = 0;
float itemBCommission = 0;
float itemCCommission = 0;
float totalCommission = 0;
float commissionTotal = 0;
printf("-----------------------------------------------\n");
printf("You have selected 'Commission worker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the price of Item A: $");
scanf_s("%f", &itemAPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item A sold this week: ");
scanf_s("%f", &itemAQuantity);
printf("-----------------------------------------------\n");
printf("Please enter the price of Item B: $");
scanf_s("%f", &itemBPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item B sold this week: ");
scanf_s("%f", &itemBQuantity);
printf("-----------------------------------------------\n");
printf("Please enter the price of Item C: $");
scanf_s("%f", &itemCPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item C sold this week: ");
scanf_s("%f", &itemCQuantity);
printf("\n-----------------------------------------------\n");
itemACommission = itemAQuantity * (itemAPrice * 0.057);
itemBCommission = itemBQuantity * (itemBPrice * 0.064);
itemCCommission = itemCQuantity * (itemCPrice * 0.072);
totalCommission = itemACommission + itemBCommission + itemCCommission;
commissionTotal = totalCommission + basicSalary;
printf("Commission for item A: $%.2f\n", itemACommission);
printf("Commission for item B: $%.2f\n", itemBCommission);
printf("Commission for item C: $%.2f\n", itemCCommission);
printf("Total commission: $%.2f\n", totalCommission);
printf("Total pay: $%.2f\n", commissionTotal);
printf("-----------------------------------------------\n");
printf("Press 'R' to return back to the main menu\n\n");;
scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return commissionTotal;
}
float pieceworkerIntro() {
char charReturn;
float quantityItem1;
float quantityItem2;
float quantityItem3;
float priceItem1 = 22.50;
float priceItem2 = 24.50;
float priceItem3 = 26;
float payItem1;
float payItem2;
float payItem3;
float pieceworkerTotal;
printf("-----------------------------------------------\n");
printf("You have selected 'Pieceworker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 1 produced this week: ");
scanf_s("%f", &quantityItem1);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 2 produced this week: ");
scanf_s("%f", &quantityItem2);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 3 produced this week: ");
scanf_s("%f", &quantityItem3);
printf("-----------------------------------------------\n");
payItem1 = quantityItem1 * priceItem1;
payItem2 = quantityItem2 * priceItem2;
payItem3 = quantityItem3 * priceItem3;
pieceworkerTotal = payItem1 + payItem2 + payItem3;
printf("\nItem 1 pay: $%.2f\nItem 2 pay: $%.2f\nItem 3 pay: $%.2f\n\nTotal pay: $%.2f", payItem1, payItem2, payItem3);
printf("Press 'R' to return back to the main menu\n\n");
scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
}
void returnMenu() {
}
float choiceInput(float *managerTotal, float *hourlyTotal, float
*commissionTotal, float *pieceworkerTotal) {
char userChoice = 0;
scanf_s(" %c", &userChoice);
do {
switch (userChoice) {
case '1':
managerIntro();
break;
case '2':
hourlyIntro();
break;
case '3':
commissionIntro();
break;
case '4':
pieceworkerIntro();
break;
case 'q':
printf("Manager total is: $%.2f", &managerTotal);
printf("Hourly total is: $%.2f", &hourlyTotal);
printf("Commission total is: $%.2f", &commissionTotal);
printf("Pieceworker total is: $%.2f", &pieceworkerTotal);
exit(0);
default:
printf("You have entered an invalid key, please try again\n");
getchar();
getchar();
choosePayroll();
break;
}
} while (userChoice != 'Q');
}
int main(void) {
welcome();
choosePayroll();
return 0;
}
Note: I apologise it's longish code, but the reason I've kept it that way is because the last time I made it shorter it didn't end up working for my actual code, only the short code I posted as an example.
I made change in your code. Check it, if this change is not as per your requirement let me know.
#include <stdio.h>
float choiceInput();
void welcome();
void choosePayroll();
float managerIntro();
float hourlyIntro();
float commissionIntro();
float pieceworkerIntro();
float choiceInput();
int main()
{
welcome();
choosePayroll();
return 0;
}
void welcome()
{
printf("-----------------------------------------------\n");
printf("Welcome to the Factory A Payroll\n");
}
void choosePayroll()
{
printf("-----------------------------------------------\n");
printf("Press '1' to access the Manager's payroll\n");
printf("Press '2' to access the Hourly worker's payroll\n");
printf("Press '3' to access Commission worker's payroll\n");
printf("Press '4' to access Pieceworkers's payroll\n");
printf("Press 'Q' to access Manager's payroll\n");
printf("-----------------------------------------------\n");
choiceInput();
}
float managerIntro()
{
float managerTotal;
char charReturn = 0;
printf("-----------------------------------------------\n");
printf("You have selected 'Manager's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter your fixed weekly salary: $");
scanf("%f", &managerTotal);
printf("-----------------------------------------------\n");
printf("You have entered $%.2f as the weekly fixed salary\n", managerTotal);
printf("-----------------------------------------------\n");
printf("Press 'R' to return back to the main menu\n\n");
scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return managerTotal;
}
float hourlyIntro()
{
char charReturn = 0;
float hourlyWage = 0;
float hoursWorked = 0;
float overTimeHours = 0;
float normalPay = 0;
float overTimePay = 0;
float hourlyTotal;
printf("-----------------------------------------------\n");
printf("You have selected 'Hourly worker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the hourly wage for the worker: $");
scanf("%f", &hourlyWage);
printf("-----------------------------------------------\n");
printf("You have entered $%.2f as the hourly wage.", hourlyWage);
printf("\n-----------------------------------------------");
printf("\nPlease enter how many hours the worker has worked this week: ");
scanf("%f", &hoursWorked);
printf("\n-----------------------------------------------");
if (hoursWorked > 40) {
overTimeHours = hoursWorked - 40;
hoursWorked = 40;
}
normalPay = hourlyWage * hoursWorked;
overTimePay = (hourlyWage * 1.5) * overTimeHours;
hourlyTotal = normalPay + overTimeHours;
printf("\nNormal weekly pay : $%.2f\nOvertime weekly pay: $%.2f\n\nTotal weekly pay: $%.2f\n\n", normalPay, overTimePay, hourlyTotal);
printf("Press 'R' to return back to the main menu\n");
scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return hourlyTotal;
}
float commissionIntro() {
char charReturn = 0;
float basicSalary = 250;
float itemAPrice = 0;
float itemBPrice = 0;
float itemCPrice = 0;
float itemAQuantity = 0;
float itemBQuantity = 0;
float itemCQuantity = 0;
float itemACommission = 0;
float itemBCommission = 0;
float itemCCommission = 0;
float totalCommission = 0;
float commissionTotal = 0;
printf("-----------------------------------------------\n");
printf("You have selected 'Commission worker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the price of Item A: $");
scanf("%f", &itemAPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item A sold this week: ");
scanf("%f", &itemAQuantity);
printf("-----------------------------------------------\n");
printf("Please enter the price of Item B: $");
scanf("%f", &itemBPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item B sold this week: ");
scanf("%f", &itemBQuantity);
printf("-----------------------------------------------\n");
printf("Please enter the price of Item C: $");
scanf("%f", &itemCPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item C sold this week: ");
scanf("%f", &itemCQuantity);
printf("\n-----------------------------------------------\n");
itemACommission = itemAQuantity * (itemAPrice * 0.057);
itemBCommission = itemBQuantity * (itemBPrice * 0.064);
itemCCommission = itemCQuantity * (itemCPrice * 0.072);
totalCommission = itemACommission + itemBCommission + itemCCommission;
commissionTotal = totalCommission + basicSalary;
printf("Commission for item A: $%.2f\n", itemACommission);
printf("Commission for item B: $%.2f\n", itemBCommission);
printf("Commission for item C: $%.2f\n", itemCCommission);
printf("Total commission: $%.2f\n", totalCommission);
printf("Total pay: $%.2f\n", commissionTotal);
printf("-----------------------------------------------\n");
printf("Press 'R' to return back to the main menu\n\n");;
scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return commissionTotal;
}
float pieceworkerIntro() {
char charReturn;
float quantityItem1;
float quantityItem2;
float quantityItem3;
float priceItem1 = 22.50;
float priceItem2 = 24.50;
float priceItem3 = 26;
float payItem1;
float payItem2;
float payItem3;
float pieceworkerTotal;
printf("-----------------------------------------------\n");
printf("You have selected 'Pieceworker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 1 produced this week: ");
scanf("%f", &quantityItem1);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 2 produced this week: ");
scanf("%f", &quantityItem2);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 3 produced this week: ");
scanf("%f", &quantityItem3);
printf("-----------------------------------------------\n");
payItem1 = quantityItem1 * priceItem1;
payItem2 = quantityItem2 * priceItem2;
payItem3 = quantityItem3 * priceItem3;
pieceworkerTotal = payItem1 + payItem2 + payItem3;
printf("\nItem 1 pay: $%.2f\nItem 2 pay: $%.2f\nItem 3 pay: $%.2f\n\nTotal pay: $%.2f", payItem1, payItem2, payItem3);
printf("Press 'R' to return back to the main menu\n\n");
scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
}
float choiceInput()
{
char userChoice;
float managerTotal;
float hourlyTotal;
float commissionTotal;
float pieceworkerTotal;
scanf(" %c", &userChoice);
do {
switch (userChoice) {
case '1':
managerTotal = managerIntro();
break;
case '2':
hourlyTotal = hourlyIntro();
break;
case '3':
commissionTotal = commissionIntro();
break;
case '4':
pieceworkerTotal = pieceworkerIntro();
break;
case 'q':
printf("Manager total is: $%.2f", managerTotal);
printf("Hourly total is: $%.2f", hourlyTotal);
printf("Commission total is: $%.2f", commissionTotal);
printf("Pieceworker total is: $%.2f", pieceworkerTotal);
return 0;
default:
printf("You have entered an invalid key, please try again\n");
getchar();
getchar();
choosePayroll();
break;
}
} while (userChoice != 'Q');
}
I am trying to make a program within code::blocks that will allow me to select multiple unit conversions to do. However, whenever I build and compile and reach the point in the program where it scans in my input for the variable "choice", code::blocks displays a window immediately afterwards that says that my .exe has stopped working and I can't figure out why this is the case. I am using the GNU GCC Compiler. Any help would be enormously appreciated.
#include <stdio.h>
int main()
{
float fahrenheit;
float celsius;
float pound;
float kilogram;
float inch;
float centimeter;
float mph;
float kph;
int foc;
int pok;
int ioc;
int mok;
int choice;
printf("\n1. Temperature Conversion\n");
printf("\n2. Weight Conversion\n");
printf("\n3. Length Conversion\n");
printf("\n4. Speed Conversion\n");
printf("\n5. Exit Program\n");
printf("\n");
printf("\nEnter the number of the program you would like to run :\n");
printf("\n");
scanf("%d", choice);
//Temperature Conversion
if(choice == 1) {
printf("\n1. Convert from Celsius to Fahrenheit\n");
printf("\n2. Convert from Fahrenheit to Celsius\n");
printf("\nEnter the number that corresponds with the conversion you would like to do:\n");
printf("\n");
scanf("%d", &foc);
if(foc == 1) {
//option 1
printf("\nEnter your temperature in Celsius : ");
scanf("%f", &celsius);
fahrenheit = 32 + (celsius * 1.8);
printf("\nYour temperature in Fahrenheit : %f ", fahrenheit);
}
else {
//option 2
printf("\nEnter your temperature in Fahrenheit : ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 0.55555555;
printf("\nYour temperature in Celsius : %f ", celsius);
}
}
//Weight Conversion
else if(choice == 2) {
printf("\n1. Convert from Pound to Kilogram ");
printf("\n2. Convert from Kilogram to Pound ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &pok);
if(pok == 1) {
//option 1
printf("\nEnter your weight in pounds : ");
scanf("%f", £);
kilogram = (2.20462 * pound);
printf("\nYour weight in kilograms : %f ", kilogram);
}
else {
//option 2
printf("\nEnter your weight in kilograms : ");
scanf("%f", &kilogram);
pound = (kilogram/2.20462);
printf("\nYour weight in pounds : %f ", celsius);
}
}
//Length Conversion
else if(choice == 3) {
printf("\n1. Convert from inches to centimeters ");
printf("\n2. Convert from centimeters to inches ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &ioc);
if(ioc == 1) {
//option 1
printf("\nEnter your length in inches : ");
scanf("%f", &inch);
centimeter = (inch/2.54);
printf("\nYour length in centimeters : %f ", centimeter);
}
else {
//option 2
printf("\nEnter your length in centimeters : ");
scanf("%f", ¢imeter);
inch = (centimeter*2.54);
printf("\nYour length in inches : %f ", inch);
}
}
//Speed Conversion
else if(choice == 4) {
printf("\n1. Convert from mph to kph ");
printf("\n2. Convert from kph to mph ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &mok);
if(mok == 1) {
//option 1
printf("\nEnter your speed in mph : ");
scanf("%f", &mph);
kph = (mph/1.60934);
printf("\nYour speed in kilometers: %f ", kph);
}
else {
//option 2
printf("\nEnter your speed in kph : ");
scanf("%f", &kph);
mph = (1.60934*kph);
printf("\nYour length in inches : %f ", mph);
}
}
else if(choice == 5) {
printf("\nProgram has ended. Thanks for your time!");
}
else {
printf("\nThat is not a valid program number. ");
}
}
You have to pass the pointer to the choice variable (i.e. &choice) to your scanf() call, just as you've done for your other scanf() uses.
I.e. instead of
scanf("%d", choice);
use
scanf("%d", &choice);
scanf() reads a value from the user and the address of the variable must be represented in the syntax
if not represented the program takes the garbage value so
I am new at C programming, so I am trying to make the user to calculate certain things using switch statement, the problem is that the result doesn't show on the program, for example if I enter 1 and enter the 50*6.63, the result is empty.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float Str;
float bonusArmor;
float Haste;
float Crit;
float Multi;
float Vera;
float Mas;
float result;
int option;
printf("Press 1 for Strength\n");
printf("Press 2 for Bonus Armor\n");
printf("Press 3 for Haste\n");
printf("Press 4 for Critical Strike\n");
printf("Press 5 for Multistrike\n");
printf("Press 6 for Versaility\n");
printf("Press 7 for Mastery\n");
scanf("%d", &option);
switch(option){
case 1:
printf("Enter Strength:\n");
scanf("%f", &Str);
result = (6.63*Str);
printf("Total is: ", result);
break;
case 2:
printf("Enter Bonus Armor:\n");
scanf("%f", &bonusArmor);
result = 6.30*bonusArmor;
printf("Total is: ", result);
break;
case 3:
printf("Enter Haste:\n");
scanf("%f", &Haste);
result = 3.66*Haste;
printf("Total is: ", result);
break;
case 4:
printf("Enter Critical Strike:\n");
scanf("%f", &Str);
result = 3.57*Str;
printf("Total is: ", result);
break;
case 5:
printf("Enter Multistrike:\n");
scanf("%f", &Str);
result = 3.18*Str;
printf("Total is: ", result);
break;
case 6:
printf("Enter Versatility:\n");
scanf("%f", &Vera);
result = 2.63*Vera;
printf("Total is: ", result);
break;
case 7:
printf("Enter Mastery:\n");
scanf("%f", &Mas);
result = 2.49*Mas;
printf("Total is: ", result);
break;
default:
printf ("Invalid input");
}
return 0;
}
You need to print floats like:
printf("Result is: %.2f", result);
Look here for a bit more information
You must specify a conversion specification to print a value:
printf("float: %f\n", floatValue);
The newline ensures the output appears when you print.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float Str;
float bonusArmor;
float Haste;
float Crit;
float Multi;
float Vera;
float Mas;
float result;
int option = 0;
printf("Press 1 for Strength\n");
printf("Press 2 for Bonus Armor\n");
printf("Press 3 for Haste\n");
printf("Press 4 for Critical Strike\n");
printf("Press 5 for Multistrike\n");
printf("Press 6 for Versatility\n");
printf("Press 7 for Mastery\n");
scanf("%d", &option);
switch(option){
case 1:
printf("Enter Strength:\n");
scanf("%f", &Str);
result = (6.63*Str);
printf("Total is: %f\n", result);
break;
case 2:
printf("Enter Bonus Armor:\n");
scanf("%f", &bonusArmor);
result = 6.30*bonusArmor;
printf("Total is: %f\n", result);
break;
case 3:
printf("Enter Haste:\n");
scanf("%f", &Haste);
result = 3.66*Haste;
printf("Total is: %f\n", result);
break;
case 4:
printf("Enter Critical Strike:\n");
scanf("%f", &Str);
result = 3.57*Str;
printf("Total is: %f\n", result);
break;
case 5:
printf("Enter Multistrike:\n");
scanf("%f", &Str);
result = 3.18*Str;
printf("Total is: %f\n", result);
break;
case 6:
printf("Enter Versatility:\n");
scanf("%f", &Vera);
result = 2.63*Vera;
printf("Total is: %f\n", result);
break;
case 7:
printf("Enter Mastery:\n");
scanf("%f", &Mas);
result = 2.49*Mas;
printf("Total is: %f\n", result);
break;
default:
printf("Invalid input\n");
break;
}
return 0;
}