Handling user's input - c

when I input a floating point number (eg. 48.3) the result displayed is 48.00 instead of 48.30 and whenever I try to input string with empty space the program ends immediately. I need help, how to fix this problem?
int integer;
char a[50];
float fnum;
char b[50];
printf("Please enter an integer : ");
scanf("%s",&a);
integer = atoi(a);
printf("\nPlease enter a floating-point number : ");
scanf("%s", &b);
fnum = atoi(b);
printf("Output : \n");
printf("%i + %.2f = %.2f \n", integer,fnum,(integer+fnum));
printf("%i - %.2f = %.2f \n", integer,fnum,(integer-fnum));
printf("%i * %.2f = %.2f \n", integer,fnum,(integer*fnum));

You're converting string b into an integer by calling atoi. You want to convert it to a floating point number, so use atof:
fnum = atof(b);

The atoi returns an int.
The atof returns a float.
int integer;
char a[50];
float fnum;
char b[50];
printf("Please enter an integer : ");
scanf("%s",&a);
integer = atoi(a);
printf("\nPlease enter a floating-point number : ");
scanf("%s", &b);
fnum = atof(b);
printf("Output : \n");
printf("%d + %.2f = %.2f \n", integer,fnum,(integer+fnum));
printf("%d - %.2f = %.2f \n", integer,fnum,(integer-fnum));
printf("%d * %.2f = %.2f \n", integer,fnum,(integer*fnum));

Related

The input that i give to my %f does not save

The gradeOne and secondGrade does not save the number that i give. So i cant have the average , because, always end in a division of (0 + 0)/2, can anyone help me?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int students;
char name[50];
double gradeOne, secondGrade, classAverage = 0, average = 0;
for(students = 1;students <= 15; students++){
printf("Tell me Your name: \n");
fflush(stdin);
scanf("%[^\n]s", &name);
//saves name
printf("Tell me your first grade: \n");
scanf("%f", &gradeOne);
printf("Tell me your second grade: \n");
scanf("%f", &secondGrade);
//saves grade
printf("\n========================================\n");
printf("Student: %s\n", name);
printf("First Grade: %3.2f \nSecond Grade: %3.2f \n", gradeOne, secondGrade);
printf("%f", average);
printf("\n========================================\n");
average = (gradeOne + secondGrade)/2;
//creates average
printf("Average of the %d student: %3.2f",students, average);
classAverage += average;
//creates class average
}
classAverage = classAverage / (students - 1);
printf("The class average was: %3.2f", classAverage);
return 0;
}
%f in scanf() is for reading float. You should use %lf to read double.
Note that you should use %f in printf() for printing double. Newer specification allows %lf for printf(), but %f should be better for compatibility.

I cannot enter and display char datatype in Visual Studio

I am writing a program to enter and display datatypes in C language.
I can enter and display int,float,long double,but cannot enter and display char.
int a;
float b;
long double c;
char d;
printf("\nEnter the integer value");
scanf_s("%d", &a);
printf("\nThe integer value is %d", a);
printf("\nEnter the float value");
scanf_s("%f", &b);
printf("\nThe float value is %f", b);
printf("\nEnter the long double value");
scanf_s("%lf", &c);
printf("\nThe double value is %lf", c);
printf("\nEnter the char value");
scanf_s("%c", &d,1);
printf("\nThe char value is %c", d);

uninitialized local variable used in C language

so i have been at this for a bit and i am a little stumped i am getting "uninitialized local variable gallons used". in my take_Input function on conversion(gallons);
i am aware that that means that the value for gallons is not being recognized.
why is the gallons to liters function not placing a value for gallons so that the conversion function has that value. any help appreciated thanks...
code:
double take_Input(void)
{
double a, b, c, d;
double gallons;
printf("please enter how many liters of A: ");
scanf("%lf", &a);
printf("please enter how many gallons of B: ");
scanf("%lf", &b);
printf("please enter how many liters of C: ");
scanf("%lf", &c);
printf("please enter how many gallons of D: ");
scanf("%lf", &d);
gallons_To_Liters(a,b,c,d);
conversions(gallons);
return(0);
}
double gallons_To_Liters(double a, double b, double c,double d)
{
double gallons, liters;
liters = a + c;
gallons = b + d;
gallons = (liters * 3.79) + gallons;
return(0);
}
double conversions(double gallons)
{
double totalGallons = gallons;
double quarts = totalGallons * 4;
double pints = totalGallons * 8;
double cups = totalGallons * 16;
double fluid_ounces = totalGallons * 128;
double tablespoons = totalGallons * 256;
double teaspoons = totalGallons * 768;
// output statements.
printf("the amount of gallons is: %.2f \n", totalGallons);
printf("the amount of quarts is: %.2f \n", quarts);
printf("the amount of pints is: %.2f \n", pints);
printf("the amount of cups is: %.2f \n", cups);
printf("the amount of fluid ounces is: %.2f \n", fluid_ounces);
printf("the amount of tablespoons is: %.2f \n", tablespoons);
printf("the amount of teaspoons is: %.2f \n", teaspoons);
return (0);
}
Your gallons_To_Liters function sets the local variable gallons, but does nothing with it. You need to return this value from the function.
Then in the calling function, you need to assign the return value of gallons_To_Liters to the gallons variable in that function.
double take_Input(void)
{
....
gallons = gallons_To_Liters(a,b,c,d);
....
}
double gallons_To_Liters(double a, double b, double c,double d)
{
double gallons, liters;
liters = a + c;
gallons = b + d;
gallons = (liters * 3.79) + gallons;
return gallons;
}
You need to keep in mind that variables in different functions are distinct from each other, even if their names are the same.
Also, for the take_Input and conversion functions, their return values are not used for anything, so change the functions to have a return type of void and remove the return statements from those functions.

Write a program that prints sum, product and quotient of two input integers in C

I'm new to the language and all this overflow problems and integer types are getting on my nerves. here is what I have but when I run it I get,
-bash: syntax error near unexpected token `newline'
The code:
#include <stdio.h>
int main(void)
{
int one, two, s, q, m;
s = one+two
q = one/two
m = one*two
printf("Enter first positive integer: ");
scanf("%d", &one);
printf("Enter second positive integer: ");
scanf("%d", &two);
printf("The addition of %d and %d is %d", one, two, s);
printf("The integer division of %d divided by %d is %d", one, two, q);
printf("the multiplication of %d and %d is %d", &one, &two, m);
return 0;
}
Thank you
You should perform the calculations after you've got the input.
printf("Enter first positive integer: ");
scanf("%d", &one);
printf("Enter second positive integer: ");
scanf("%d", &two);
s = one+two;
q = one/two;
m = one*two;
try this:
#include <stdio.h>
int main(void)
{
int one, two;
printf("Enter first positive integer: ");
scanf("%d", &one);
printf("Enter second positive integer: ");
scanf("%d", &two);
printf("The addition of %d and %d is %d", one, two, (one+two));
printf("The integer division of %d divided by %d is %d", one, two, (one/two));
printf("the multiplication of %d and %d is %d", &one, &two, (one*two));
return 0;
}
You are missing the semicolons after
s = one+two
q = one/two
m = one*two
Plus you should perform the calculations after you read the input but that's a different problem.
These lines makes the problem:
s = one+two
q = one/two
m = one*two
You missed the ; (semicolon)
Change it like:
s = one+two;
q = one/two;
m = one*two;
Also read the input from user first before doing the operations.

Triying to make an plus calculater

void main ()
{
int sayi;
int ikisayi;
printf("first number");
scanf("%d",&sayi);
printf("second number");
scanf ("%d", &ikisayi);
snc = ikisayi + sayi;
printf(\n Total= %f \n , snc );
}
What is wrong with that can you help me?
You need to declare int snc;, and the format specifier in printf should be %d. In addition, the return type of main should be int, you should include stdio.h and you should have double quotes around your format string in printf.
#include <stdio.h>
int main() {
int a, b, c;
printf("first number: ");
scanf("%d", &a);
printf("second number: ");
scanf("%d", &b);
c = a + b;
printf("total: %d\n", c);
return 0;
}
Also, printf("\n Total= %d \n" , snc );

Resources