Here, i am writing a c programming code i need help form you guys! I want to modify my program and want to use this in a loop i.e if a user enters "y" & if "n" will be entered it must end the program, I am trying this with switch case, I tried my best to end the program, it is returning to end but i don't know how to use "y" value for running the program again!
#include<conio.h>
#include<stdio.h>
int main()
{
char n, A, B;
float kmTOm = 0.621371;
float inchesTOfoot = 0.0833333;
float cmTOinches = 0.393701;
float poundtokg = 0.453592;
float inchesTOmeter = 0.0254;
float a, b;
while (1)
{
printf("Enter the input character. q to quit\n1. kms to miles\n2. inches to foot\n3. cms to inches\n4. pound to kgs\n5. inches to meters\n");
scanf(" %c", &n);
switch (n)
{
case 'q':
printf("Quitting the program...");
goto end;
break;
case '1':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * kmTOm;
printf("%.2f Kms is equal to %.2f Miles\n\n\n", a, b);
break;
case '2':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOfoot;
printf("%f Inches is equal to %f Foot\n", a, b);
break;
case '3':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * cmTOinches;
printf("%f Cms is equal to %f Inches\n", a, b);
break;
case '4':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * poundtokg;
printf("%f Pounds is equal to Kgs %f \n", a, b);
break;
case '5':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOmeter;
printf("%f inches is equal to %f meters \n", a, b);
break;
default:
printf("In default now");
break;
}
}
end:
return 0;
}
I want a kindful modification from you developers to help me as I am a beginner and learing from a month, I except that the actual output must come like this ----o/p- do you want to quit press "q" ,do you want to run again press "y"
If you change the while(1) to a variable, then you can test after every loop whether it's time to exit.
The changes I made to your code all have comments below:
#include<conio.h>
#include<stdio.h>
int main()
{
char n, A, B;
float kmTOm = 0.621371;
float inchesTOfoot = 0.0833333;
float cmTOinches = 0.393701;
float poundtokg = 0.453592;
float inchesTOmeter = 0.0254;
float a, b;
n = 'y'; //set n to make sure while repeats
while (n != 'n') //change while to test "n" instead of 1, this way the value of n can be changed and the loop can be exited
{
printf("Enter the input character. q to quit\n1. kms to miles\n2. inches to foot\n3. cms to inches\n4. po
und to kgs\n5. inches to meters\n");
scanf(" %c", &n);
switch (n)
{
case 'q':
printf("Quitting the program...");
return(0); //exit entire program
break;
case '1':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * kmTOm;
printf("%.2f Kms is equal to %.2f Miles\n\n\n", a, b);
break;
case '2':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOfoot;
printf("%f Inches is equal to %f Foot\n", a, b);
break;
case '3':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * cmTOinches;
printf("%f Cms is equal to %f Inches\n", a, b);
break;
case '4':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * poundtokg;
printf("%f Pounds is equal to Kgs %f \n", a, b);
break;
case '5':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOmeter;
printf("%f inches is equal to %f meters \n", a, b);
break;
default:
printf("In default now");
break;
}
//Ask the user if they want to run again. The code will exit if the user types 'n', it will repeat if they type anything else (including 'y').
printf("\nRun again? y/n:");
scanf(" %c",&n);
}
return 0;
}
Related
I want this program to repeat every time when users wants to continue.
I want user to choose "yes" to continue the program using other options but it's not executable. I used 'if else' statement for that but I am not getting any applicable or satisfying results. Can someone help me on this matter?
#include <stdio.h>
int main() {
float a, b;
int p;
char q;
printf("Enter the first number:");
scanf("%f", &a);
printf("Enter the second number:");
scanf("%f", &b);
printf("Choose the option:\n1.Sum\n \n2.Subtraction\n \n3.Multiplication\n \n4.Divison\n \nyour choice=");
scanf("%d", &p);
switch (p)
{
case 1:
printf("The sum of above numbers is: %f", a + b);
break;
case 2:
printf("The subtraction of above numbers is:%f", a - b);
break;
case 3:
printf("The multiplication of above numbers is:%f", a * b);
break;
case 4:
printf("The division of above numbers is:%f", a / b);
break;
}
printf("\n\nDo u want to continue:(y for yes and n for no) :");
scanf("%c", &q);
if (q == 'y')
return main();
else
return 0;
}
You need to use a loop for this. Explanations in the comments:
int main() {
float a, b;
int p;
char q;
do // use a loop here
{
printf("Enter the first number:");
scanf("%f", &a);
printf("Enter the second number:");
scanf("%f", &b);
printf("Choose the option:\n1.Sum\n \n2.Subtraction\n \n3.Multiplication\n \n4.Divison\n \nyour choice=");
scanf("%d", &p);
switch (p)
{
case 1:
printf("The sum of above numbers is: %f", a + b);
break;
case 2:
printf("The subtraction of above numbers is:%f", a - b);
break;
case 3:
printf("The multiplication of above numbers is:%f", a * b);
break;
case 4:
printf("The division of above numbers is:%f", a / b);
break;
}
printf("\n\nDo you want to continue: (y for yes and n for no) :");
scanf(" %c", &q); // use " %c" instead of "%c", the white space
// will absorb any blank space (including newlines)
} while (q == 'y'); // continue loop if user has entered 'y'
return 0;
}
This question already has answers here:
pow() isn't defined [closed]
(4 answers)
Closed 3 years ago.
I have reviewed multiple questions and answers for powers in C, or 4^2. I have wrote it out in different ways and still can't it right. As of now I get the message:
/tmp/cc9yBiPo.o: In function `main':
Functions2.c:(.text+0x2b8): undefined reference to `pow'
collect2: error: ld returned 1 exit status.
Here is my code so far, like I said I have tried different peoples answers but I am more confused more than anything.
#include <stdio.h>
#include <math.h>
int main()
{
int choice;
int a, b, c;
float d;
do{
printf("\t Menu \n");
printf("1. Addition \n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Modulo \n");
printf("6. Eponents \n");
printf("7. Exit \n");
printf("Please choose a menu selection: \n");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("Enter two numbers to add: \n");
scanf("%d%d", &a, &b);
c = a + b;
printf("The sum of the two numbers = %d \n", c);
}break;
case 2:
{
printf("Enter two numbers to subtract: \n");
scanf("%d&d", &a, &b);
c = a - b;
printf("The difference between the two number = %d \n", c);
}break;
case 3:
{
printf("Enter two numbers to multiply: \n");
scanf("%d%d", &a, &b);
c = a * b;
printf("The product of the two numbers = %d \n", c);
}break;
case 4:
{
printf("Enter two numbers to divide: \n");
scanf("%d%d", &a, &b);
d = (float) a/b;
printf("The quotient of the two numbers = %f \n", d);
}break;
case 5:
{
printf("Enter two numbers to get a modulo: \n");
scanf("%d%d", &a, &b);
c = a % b;
printf("The modulus of the two numbers = %d \n", c);
}break;
case 6:
{
printf("Enter a base: \n");
scanf("%d", &a);
printf("Enter an exponent: \n");
scanf("%d", &b);
c = pow(a, b);
printf("The exponent of the two numbers = %d", c);
}break;
case 7:
{
printf("Thank you, you will now exit.");
}break;
default:
printf("error \n");
}
} while (choice != 7);
return 0;
}
You have to link with the math library(libm).
Add -lm flag to your complication command to link with it.
Check this question.
You have to change the data types of base & exponent to get correct result.
The syntax for the pow function in the C Language is:
double pow(double x, double y);
Again, change the data type from int to float in case of 4.
#include <stdio.h>
#include <math.h>
int main()
{
double g,h,i;
int choice;
int a, b, c;
float d,e,f;
do{
printf("\t Menu \n");
printf("1. Addition \n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Modulo \n");
printf("6. Eponents \n");
printf("7. Exit \n");
printf("Please choose a menu selection: \n");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("Enter two numbers to add: \n");
scanf("%d%d", &a, &b);
c = a + b;
printf("The sum of the two numbers = %d \n", c);
}break;
case 2:
{
printf("Enter two numbers to subtract: \n");
scanf("%d&d", &a, &b);
c = a - b;
printf("The difference between the two number = %d \n", c);
}break;
case 3:
{
printf("Enter two numbers to multiply: \n");
scanf("%d%d", &a, &b);
c = a * b;
printf("The product of the two numbers = %d \n", c);
}break;
case 4:
{
printf("Enter two numbers to divide: \n");
scanf("%f%f", &e, &f);
d = e/f;
printf("The quotient of the two numbers = %f \n", d);
}break;
case 5:
{
printf("Enter two numbers to get a modulo: \n");
scanf("%d%d", &a, &b);
c = a % b;
printf("The modulus of the two numbers = %d \n", c);
}break;
case 6:
{
printf("Enter a base: \n");
scanf("%lf", &h);
printf("Enter an exponent: \n");
scanf("%lf", &i);
g = pow(h, i);
printf("The exponent of the two numbers = %lf", g);
}break;
case 7:
{
printf("Thank you, you will now exit.");
}break;
default:
printf("error \n");
}
} while (choice != 7);
return 0;
}
Hope this will help you.
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;
}
i m new in programing.
i've written a simple program.
i want to repeat the program again and again and it can only exit when user wants to exit.
here is my program
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
int num1, num2, a, m, s, choice;
float d;
printf("\nEnter The First Number: ");
scanf("%d", &num1);
printf("\nEnter The Second Number: ");
scanf("%d", &num2);
a=num1+num2;
m=num1*num2;
s=num1-num2;
d=(float)(num1/num2);
printf("\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type S : ");
scanf(" %c", &ch);
switch(ch)
{
case 'A': printf("\nThe Addition Of The Number Is= %d", a);
break;
case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m);
break;
case 'S': printf("\nThe Substraction Of THe Numbers Is= %d", s);
break;
case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d);
break;
default : printf("\nInvalid Entry");
break;
}
printf("\nPress Any Key To Exit");
getch();
return 0;
}
and here is the output
"Enter The First Number: 10
Enter The Second Number: 10
Enter Your Choice
For Addition Type A
For Multipication Type M
For Division Type D
For Substraction Type S : A
The Addition Of The Number Is= 20
Press Any Key To Exit"
I want a line before the line Press Any Key To Exit
"If You Want To Calculate Again Press Y
or
Press Any Key To Exit"
when press Y then the program should start from the beginning.
How can i do this???
wrap the code inside a do{} while() ?
char answer;
do{
printf("\nEnter The First Number: ");
scanf("%d", &num1);
printf("\nEnter The Second Number: ");
scanf("%d", &num2);
a=num1+num2;
m=num1*num2;
s=num1-num2;
d=(float)(num1/num2);
printf("\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type S : ");
scanf(" %c", &ch);
switch(ch)
{
case 'A': printf("\nThe Addition Of The Number Is= %d", a);
break;
case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m);
break;
case 'S': printf("\nThe Substraction Of THe Numbers Is= %d", s);
break;
case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d);
break;
default : printf("\nInvalid Entry");
break;
}
printf("\nPress Y to continue. Press any Key To Exit");
scanf(" %c",&answer); // dont forget type &
}
while(answer == 'y' || answer == 'Y');
Declare a variable, let's say answer, which will store the user answer when you ask for "Press Y to continue. Press any Key To Exit". Check to see what value has that variable. If is 'y' or 'Y', the loop will repeat. If the user pressed other key, the loop is over.
You can also use recursion, which is often used in more functional oriented programming languages.
Pseudo-code:
myfunction = do
...
b <- somethingtodo
...
if b
then myfunction
else return ()
Relative to Jens's solution, it would look like:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main (void)
{
char choice;
int num1, num2, cont;
printf("Enter the first number: ");
scanf("%d", &num1);
getchar ();
printf("Enter the second number: ");
scanf("%d", &num2);
getchar ();
printf("A - addition\n");
printf("S - subtraction\n");
printf("M - multiplication\n");
printf("D - division\n");
printf("[ASMD]? ");
choice = (char)toupper(getchar());
getchar ();
printf("\n");
switch(choice)
{
case 'A':
printf("%d + %d = %d", num1, num2, num1 + num2);
break;
case 'S':
printf("%d - %d = %d", num1, num2, num1 - num2);
break;
case 'M':
printf("%d * %d = %d", num1, num2, num1 * num2);
break;
case 'D':
if (num2 == 0)
fprintf(stderr, "The divisor can not be zero");
else
{
printf("%d / %d = %f", num1, num2, (double)num1 / num2);
}
break;
default :
fprintf(stderr, "Invalid entry");
break;
}
printf("\n");
for (;;)
{
printf("Continue [YN]? ");
cont = toupper(getchar());
getchar ();
if (cont == 'Y')
return main(); // the difference.
else if (cont == 'N')
return EXIT_SUCCESS;
}
}
I would move the calculation stuff in it's own function and then use while() in main.
I have tried to fix other problems as well (this solution only uses standard C functions).
#include <stdio.h> // puts, printf, fprintf, scanf, getchar, stderr, EOF
#include <stdlib.h> // exit, EXIT_SUCCESS, EXIT_FAILURE
#include <ctype.h> // toupper
char fail_on_eof (int c)
{
if (c == EOF)
exit (EXIT_FAILURE);
// In case of fail_on_eof (scanf (...)) the return value of this this
// function is not useful
// scanf () returns the number of chars read or EOF
// getchar () returns a char or EOF
return (char) c;
}
void skip_to_next_line (void)
{
char c;
do
{
c = fail_on_eof (getchar ());
} while (c != '\n');
}
char read_upcase_char_line (char* prompt)
{
char c;
printf ("%s ", prompt);
c = fail_on_eof (toupper (getchar ()));
skip_to_next_line ();
return c;
}
int read_num_line (char* prompt)
{
int num;
printf ("%s ", prompt);
fail_on_eof (scanf ("%d", &num));
skip_to_next_line ();
return num;
}
int calculate (void)
{
char choice;
int num1, num2, cont;
num1 = read_num_line ("Enter the first number:");
num2 = read_num_line ("Enter the second number:");
puts("A - addition");
puts("S - subtraction");
puts("M - multiplication");
puts("D - division");
choice = read_upcase_char_line ("[ASMD]?");
puts("");
switch(choice)
{
case 'A':
printf("%d + %d = %d", num1, num2, num1 + num2);
break;
case 'S':
printf("%d - %d = %d", num1, num2, num1 - num2);
break;
case 'M':
printf("%d * %d = %d", num1, num2, num1 * num2);
break;
case 'D':
if (num2 == 0)
// Better use stderr for error messages
fprintf(stderr, "The divisor can not be zero");
else
{
printf("%d / %d = %f", num1, num2, (double)num1 / num2);
}
break;
default :
// Better use stderr for error messages
fprintf(stderr, "Invalid entry");
break;
}
printf("\n");
for (;;)
{
cont = read_upcase_char_line ("Continue [YN]?");
if (cont == 'Y')
return -1;
else if (cont == 'N')
return 0;
}
}
int main(void)
{
while (calculate ());
return EXIT_SUCCESS; // Use this constant to avoid platform specific issues with the return code
}
I made a simple calculator program using switch case, but the output is different than I expected.
int main(){
double a, b;
double sum = 0;
char o; //operator
printf("Enter operator\n");
scanf("%c", &o);
printf("Enter first operand\n");
scanf("%f", &a);
printf("Enter second operand\n");
scanf("%f", &b);
switch (o)
{
case '+':
sum = a + b;
break;
case '-':
sum = a - b;
break;
case '*':
sum = a * b;
break;
case '/':
sum = a / b;
break;
if (b == 0){ printf("Error"); }
break;
}
printf("The result is\n%10.10lf\n", sum);
getchar();
getchar();
}
The result of 'sum' is some huge astronomical numbers. Can someone tell why?
Try %lf instead of %f because that way you will have a and b as the type double rather than float.
a and b are both of type double. The correct format specifier for double in scanf is %lf, not %f (which is for float).
scanf("%lf", &a);
Note that in printf, %f is used or for double (the same for float because it's promoted to double). Since C99, %lf in printf is the same as %f.
you are trying to convert double to float which is not implicitly possible inside scanf or printf function. What scanf was doing, It was purging/reformatting the whole (double)input into an empty float value. Here is working one :
#include<stdio.h>
int main(){
double a, b;
double sum = 0;
char o; //operator
printf("Enter operator\n");
scanf("%c", &o);
printf("Enter first operand\n");
scanf("%lf", &a);
printf("Enter second operand\n");
scanf("%lf", &b);
switch (o)
{
case '+':
sum = a + b;
break;
case '-':
sum = a - b;
break;
case '*':
sum = a * b;
break;
case '/':
sum = a / b;
break;
if (b == 0){ printf("Error"); }
break;
}
printf("The result is\n %f\n", sum);
}
As everyone has pointed out, you need scanf ("%lf", &a); and same for b.
Now in your switch statement you wanted to prevent division by zero :
case '/':
sum = a / b;
break;
if (b == 0){ printf("Error"); }
break;
But what happens there is that you break; before getting to the condition. Suppose we removed that first break; the division by zero is still performed before you print the error message anyway.
One way to prevent division by zero would be :
case '/':
if (b)
sum = a / b;
else
printf("Error");
break;
where we check that b != 0 before we make the division and then we either divide or print the error message.