Compiler error which I am unable to locate - c

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;
}

Related

Do while loop in a switch statement

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);
}

How to count the total ABCDE in C

I'm trying to make a C program that could read the students' scores until the user enter end of file (EOF) and determine whether their grade is A, B, C, D, or E. I'm having troubles in counting the total of A's, B's, C's, D's, and E's. The total is always 0 (zero).
This is what I've tried
#include<stdio.h>
int main()
{
int num_stu, score, counter, total,grade;
int aCount = 0, bCount=0, cCount=0,
dCount=0, eCount=0;
total = 0;
counter = 1;
printf("Enter how many number of students: ");
scanf("%d",&num_stu);
while (counter <=num_stu){
printf("Enter student score: ");
scanf("%d",&score);
if(score>=80)
printf("Student's grade is A\n");
else
if(score>=70)
printf("Student's grade is B\n");
else
if(score>=60)
printf("Student's grade is C\n");
else
if(score>=50)
printf("Student's grade is D\n");
else
printf("Student's grade is E\n");
counter=counter+1;
}
while ((grade=getchar()) !=EOF) {
switch (grade){
case 'A':case'a':
++aCount;
break;
case'B': case'b':
++bCount;
break;
case'C': case'c':
+cCount;
break;
case'D':case'd':
++dCount;
break;
case'E':case'e':
++eCount;
break;
case'\n': case' ':
break;
default:
printf("Incorrect letter grade entered.");
printf("Enter a new grade.\n");
break;
}
}
printf("\n Totals for each letter grade are: \n");
printf("A: %d\n",aCount);
printf("B: %d\n",bCount);
printf("C: %d\n",cCount);
printf("D: %d\n",dCount);
printf("E: %d\n",eCount);
return 0; }
Is there anything that I did wrong? Thanks in advance!
You need to get rid of '\n' every time you press enter. So to get rid of these Put
one getchar() before the while loop and one get char inside the while loop.
#include<stdio.h>
int main()
{
int num_stu, score, counter, total,grade;
int aCount = 0, bCount=0, cCount=0,
dCount=0, eCount=0;
total = 0;
counter = 1;
printf("Enter how many number of students: ");
scanf("%d",&num_stu);
while (counter <=num_stu){
printf("Enter student score: ");
scanf("%d",&score);
if(score>=80)
printf("Student's grade is A\n");
else
if(score>=70)
printf("Student's grade is B\n");
else
if(score>=60)
printf("Student's grade is C\n");
else
if(score>=50)
printf("Student's grade is D\n");
else
printf("Student's grade is E\n");
counter=counter+1;
}
getchar();
while ((grade=getchar()) !=EOF) {
getchar();
switch (grade){
case 'A':case'a':
++aCount;
break;
case'B': case'b':
++bCount;
break;
case'C': case'c':
+cCount;
break;
case'D':case'd':
++dCount;
break;
case'E':case'e':
++eCount;
break;
case'\n': case' ':
break;
default:
printf("Incorrect letter grade entered.");
printf("Enter a new grade.\n");
break;
}
}
printf("\nTotals for each letter grade are: \n");
printf("A: %d\n",aCount);
printf("B: %d\n",bCount);
printf("C: %d\n",cCount);
printf("D: %d\n",dCount);
printf("E: %d\n",eCount);
return 0;
}

Problem: Scholarship Endowment Fund Part 2 (fund2.c)

Problem: Scholarship Endowment Fund Part 2 (fund2.c)
How do I return back to main menu and add a counter for the total number of donations and investment made, here's what i got?
Then, your program should allow the user the following options:
Make a donation
Make an investment
Print balance of fund
Quit
#include <stdio.h>
#include <stdlib.h>
//main functions
int main() {
// variables
int donation, investment, fund, total, tdonations, tinvestments;
// prompt user
printf("Welcome!\n");
printf("What is the initial balance of the fund\n");
scanf("%d", &fund);
int ans;
printf("What would you like to do?\n");
printf("\t1- Make a Donation\n");
printf("\t2- Make an investment\n");
printf("\t3- Print balance of fund\n");
printf("\t4- Quit\n");
scanf("%d", &ans);
if (ans == 1) {
printf("How much would you like to donate?\n");
scanf("%d", &donation);
}
if (ans == 2) {
printf("How much would you like to invest?\n");
scanf("%d", &investment);
return main();
}
if (ans == 3) {
total = donation + fund - investment;
if (total < fund) {
printf("You cannot make an investment of that amount\n");
return main();
}
else {
printf("The current balance is %d\n", total);
printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);
}
}
if (ans == 4) {
printf("Type 4 to quit\n");
}
else {
printf("Not a valid option.\n");
}
//switch
switch (ans) {
case 1:
printf("How much would you like to donate?\n");
scanf("%d", &donation);
return main();
case 2:
printf("How much would you like to invest\n");
scanf("%d", &investment);
return main();
case 3:
printf("The current balance is %d\n", total);
printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);
return main();
case 4:
break;
}
return 0;
}
You should put in a loop the part that you want to be repeated, using a varaible to decide when to stop.
here is an example, also in the switch costruct you should usa a default option, maybe to warn the user that the option they inserted is not valid.
int main() {
// variables
int donation, investment, fund, total, tdonations, tinvestments;
// prompt user
printf("Welcome!\n");
printf("What is the initial balance of the fund\n");
scanf("%d", &fund);
//loop until the user decide so
int exit = 0;
while(exit == 0){
int ans;
printf("What would you like to do?\n");
printf("\t1- Make a Donation\n");
printf("\t2- Make an investment\n");
printf("\t3- Print balance of fund\n");
printf("\t4- Quit\n");
scanf("%d", &ans);
//switch
switch (ans) {
case 1:
printf("How much would you like to donate?\n");
scanf("%d", &donation);
break;
case 2:
printf("How much would you like to invest\n");
scanf("%d", &investment);
break;
case 3:
total = donation + fund - investment;
if (total < fund) {
printf("You cannot make an investment of that amount\n");
}
else {
printf("The current balance is %d\n", total);
printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);
}
break;
case 4:
exit = 1;
break;
default:
printf("Incorrect option\n");
break;
}
}
return 0;
}

Check for bad practices/improvements

I'm relatively new to coding yet not completely inexperienced. Working on a school assignment regarding financial calculators. Would be great if any of you could take a look at my code for bad practices/possible improvements etc.
I did add a 'animated' startup (with a lot of printf to show "financial calculator" but couldn't fit it in, is it a good idea to do something like that?)
option_2 and option_4 are left as placeholders for the time being while my group works on other 2 calculators.
//ASSIGNMENT_041118
//libraries
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <signal.h>
//files
//colours
#define ANSI_COLOR_BLACK "\x1b[30m"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_WHITE "\x1b[37m"
#define ANSI_COLOR_BBLACK "\x1b[90m"
#define ANSI_COLOR_BRED "\x1b[91m"
#define ANSI_COLOR_BGREEN "\x1b[92m"
#define ANSI_COLOR_BYELLOW "\x1b[93m"
#define ANSI_COLOR_BBLUE "\x1b[94m"
#define ANSI_COLOR_BMAGENTA "\x1b[95m"
#define ANSI_COLOR_BCYAN "\x1b[96m"
#define ANSI_COLOR_BWHITE "\x1b[97m"
#define ANSI_COLOR_RESET "\x1b[0m"
//function prototype declaration
int main_menu();
int login();
float compound();
float compound_calc(float n, float initial_savings, float rate, float time);
float option_2();
float mortgage();
float option_4();
int main()
{
system("color 0F");
login();
return (0);
}
int login()
{
char username[20];
char password[20];
int l_option;
printf("=========================================================================================================\n");
printf("Enter your username (case sensitive): ");
scanf("%s", &username);
printf("\nEnter your password (case sensitive): ");
scanf("%s", &password);
if (strcmp(username, "user") == 0)
{
if (strcmp(password, "0000") == 0)
{
system("cls");
return main_menu();
}
else
{
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"ERROR - WRONG PASSWORD\n"ANSI_COLOR_RESET);
do {
printf("---------------------------------------------------------------------------------------------------------\n");
printf("[1]Retry\n[0]Exit\n");
printf("=========================================================================================================\n");
scanf("%d", &l_option);
switch (l_option)
{
case 1:
system("cls");
return login();
break;
case 0:
system("cls");
printf("=========================================================================================================\n");
printf("EXIT\n");
printf("=========================================================================================================\n");
exit(0);
break;
default:
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED "INVALID OPTION - CHOOSE AGAIN\n" ANSI_COLOR_RESET);
break;
}
} while (l_option != 1 && l_option != 0);
}
}
else
{
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"ERROR - USER DOESN'T EXIST\n"ANSI_COLOR_RESET);
do {
printf("---------------------------------------------------------------------------------------------------------\n");
printf("[1]Retry\n[0]Exit\n");
printf("=========================================================================================================\n");
scanf("%d", &l_option);
switch (l_option)
{
case 1:
system("cls");
return login();
break;
case 0:
system("cls");
printf("=========================================================================================================\n");
printf("EXIT\n");
printf("=========================================================================================================\n");
exit(0);
break;
default:
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED "INVALID OPTION - CHOOSE AGAIN\n" ANSI_COLOR_RESET);
break;
}
} while (l_option != 1 && l_option != 0);
}
}
int main_menu()
{
int option_main;
do {
printf("=========================================================================================================\n");
printf("Choose an option:\n"); //tells user to select an option
printf("---------------------------------------------------------------------------------------------------------\n");
printf(ANSI_COLOR_BGREEN"[1] <Savings Calculator - Compounded Interest> \n"ANSI_COLOR_RESET); //displays options
printf(ANSI_COLOR_BMAGENTA"[2] <place_holder_2> \n"ANSI_COLOR_RESET);
printf(ANSI_COLOR_BYELLOW"[3] <Mortage calculator> \n"ANSI_COLOR_RESET);
printf(ANSI_COLOR_BCYAN"[4] <place_holder_3> \n"ANSI_COLOR_RESET);
printf("\n[0] <Exit system>\n");
printf("=========================================================================================================\n");
scanf("%d", &option_main); //accepts input for function selection
printf("=========================================================================================================\n");
system("cls");
switch (option_main) //switch case for main option
{
case 1:
{
compound();
break;
}
case 2:/*option_2*/
{
option_2();
break;
}
case 3:/*option_3*/
{
mortgage();
break;
}
case 4:/*option_4*/
{
option_4();
break;
}
case 0:/*exit*/
{
printf("=========================================================================================================\n");
printf("<EXIT>\n");
printf("=========================================================================================================\n");
exit(0);
break;
}
default:
{
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"INVALID OPTION - CHOOSE AGAIN\n"ANSI_COLOR_RESET);
break;
}
}
} while (option_main != 1 && option_main != 2 && option_main != 3 && option_main != 4 && option_main != 0);
return (0);
}
float compound() //compound_interest_calculation_self_defined function
{
int compound_type, ci_option;
float n, initial_savings, rate, time, final_savings;
do {
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BGREEN"<Savings Calculator - Compounded Interest>\n"ANSI_COLOR_RESET); //displays option selected (Savings Calculator - Compounded Interest)
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Please enter compound type:\n"); //Prompts user to choose
printf("[1] Monthly\n[2] Quarterly\n[3] Semiannually\n[4] Annually\n\n[9]Return to menu\n[0] Exit\n");//displays options
printf("=========================================================================================================\n");
scanf("%d", &compound_type); //accepts input for option selection
printf("=========================================================================================================\n");
system("cls");
switch (compound_type) //internal switch case for compounded interest calculator
{
case 1: /*monthly*/
n = 12;
printf("=========================================================================================================\n");
printf("<Monthly compound>\n");
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Please enter initial amount of savings: RM "); //prompts user for initial saving amount (principle)
scanf("%f", &initial_savings); //accepts input for initial savings
/*printf("Please enter monthly deposit: RM "); //prompts user input for monthly deposit
scanf("%f", &monthly_deposit); */ //accepts input for monthly deposit
printf("Please enter interest rate (decimal): "); //prompts user input for interest rate
scanf("%f", &rate); //accepts input for interest rate
printf("Please enter time (year): "); //prompts input for saving duration
scanf("%f", &time); //accepts input for saving duration
final_savings = compound_calc(n, initial_savings, rate, time);
printf("\nThe final savings is: RM %.2f \n", final_savings); //dispays final savings amount
printf("=========================================================================================================\n");
break;
case 2: /*quarterly*/
n = 4;
printf("=========================================================================================================\n");
printf("<Quarterly compound>\n");
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Please enter initial amount of savings: RM ");
scanf("%f", &initial_savings);
/*printf("Please enter monthly deposit: RM "); //temporarily removed from calculation
scanf("%f", &monthly_deposit);*/
printf("Please enter interest rate (decimal): ");
scanf("%f", &rate);
printf("Please enter time (year): ");
scanf("%f", &time);
final_savings = compound_calc(n, initial_savings, rate, time);
printf("\nThe final savings is: RM %.2f \n", final_savings);
printf("=========================================================================================================\n");
break;
case 3: /*semiannually*/
n = 2;
printf("=========================================================================================================\n");
printf("<Semiannual compound>\n");
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Please enter initial amount of savings: RM ");
scanf("%f", &initial_savings);
/*printf("Please enter monthly deposit: RM "); //temporarily removed from calculation
scanf("%f", &monthly_deposit);*/
printf("Please enter interest rate (decimal): ");
scanf("%f", &rate);
printf("Please enter time (year): ");
scanf("%f", &time);
final_savings = compound_calc(n, initial_savings, rate, time);
printf("\nThe final savings is: RM %.2f \n", final_savings);
printf("=========================================================================================================\n");
break;
case 4: /*annually*/
n = 1;
printf("=========================================================================================================\n");
printf("<Annual compound>\n");
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Please enter initial amount of savings: RM ");
scanf("%f", &initial_savings);
/*printf("Please enter monthly deposit: RM "); //temporarily removed from calculation
scanf("%f", &monthly_deposit);*/
printf("Please enter interest rate (decimal): ");
scanf("%f", &rate);
printf("Please enter time (year): ");
scanf("%f", &time);
final_savings = compound_calc(n, initial_savings, rate, time);
printf("\nThe final savings is: RM %.2f\n", final_savings);
printf("=========================================================================================================\n");
break;
case 9: /*return to menu*/
return main_menu();
break;
case 0: /*exit*/
printf("=========================================================================================================\n");
printf("<EXIT> \n");
printf("=========================================================================================================\n");
exit(0);
default:/*any other input*/
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"INVALID OPTION - CHOOSE AGAIN\n"ANSI_COLOR_RESET);//When user inputs invalid option for compound type
}
} while (compound_type != 0 && compound_type != 1 && compound_type != 2 && compound_type != 3 && compound_type != 4 && compound_type != 9);
do
{
printf("[1]Back to compound interest calculator\n[9]Return to menu\n[0] Exit\n");
printf("=========================================================================================================\n");
scanf("%d", &ci_option);
switch (ci_option)
{
case 1:
system("cls");
return compound();
break;
case 9:
system("cls");
return main_menu();
break;
case 0:
system("cls");
printf("=========================================================================================================\n");
printf("<EXIT>\n");
printf("=========================================================================================================\n");
exit(0);
break;
default:/*any other input*/
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"INVALID OPTION - CHOOSE AGAIN\n"ANSI_COLOR_RESET);
printf("=========================================================================================================\n");
}
} while (ci_option != 1 && ci_option != 9 && ci_option != 0);
return(0);
}
float compound_calc(float n, float initial_savings, float rate, float time)
{
float CI;
CI = (initial_savings*(pow((1 + rate / n), (n * time))));
return (CI);
}
float option_2()
{
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BMAGENTA"<place_holder_2> selected \n"ANSI_COLOR_RESET);
printf("=========================================================================================================\n");
return(0);
}
float mortgage()
{
float house_price, down_percentage, interest_rate, monthly_payment, r, p;//variables declaration
int loan_period, n,m_option;
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BYELLOW"<Home mortgage calculator>\n"ANSI_COLOR_RESET); //displays option selected (Savings Calculator - Compounded Interest)
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Insert property price: RM ");//prompt input
scanf("%f", &house_price);//get input
printf("Insert down percentage (decimal): ");
scanf("%f", &down_percentage);
printf("Insert loan period (Years): ");
scanf("%d", &loan_period);
printf("Insert interest rate (decimal):");
scanf("%f", &interest_rate);
r = interest_rate / 12;
n = loan_period * 12;
p = house_price - house_price *down_percentage;
monthly_payment = p *(r*pow((1 + r), n)) / (pow((1 + r), n) - 1);//calculate monthly payment //NOTE:MOVE TO SELF DEFINED FUNCTION
printf("\nYour monthly payment is %.2f \n", monthly_payment);//display output
do
{
printf("=========================================================================================================\n");
printf("[3]Recalculate\n[9]Return to menu\n[0] Exit\n");
printf("=========================================================================================================\n");
scanf("%d", &m_option);
switch (m_option)
{
case 3:
system("cls");
return mortgage();
break;
case 9:
system("cls");
return main_menu();
break;
case 0:
system("cls");
printf("=========================================================================================================\n");
printf("<EXIT>\n");
printf("=========================================================================================================\n");
exit(0);
break;
default:/*any other input*/
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"ERROR: INVALID OPTION \n"ANSI_COLOR_RESET);
printf("=========================================================================================================\n");
}
} while (m_option != 3 && m_option != 9 && m_option != 0);
return (0);
}
float option_4()
{
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BCYAN"<place_holder_4> selected \n"ANSI_COLOR_RESET);
printf("=========================================================================================================\n");
return(0);
}
Replace your "scanf();" to "fgets();"
char *fgets(char *str, int n, FILE *stream)
Example:
#include <stdio.h>
#include <string.h>
char Name[50];
int main(){
printf("What is your name?\n> ");
fgets(Name,50,stdin); //Gets user input from stdin (not from file)
/* But the problem is that fgets also includes '\n' so we put: */
Name[strcspn(Name,"\r\n")]=0; //Removes '\n'
printf("Your name is: <%s>\n",Name);
return 0;
}

C program output result isn't showing

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;
}

Resources