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.
Related
I want to take input from user using structure. So I'm using the code like below. But it's not printing the values which I'm entering. Can anyone help me?
#include <stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b1, b2;
printf("Enter the Name, Price and Pages of Book 1: ");
scanf("%c %f %d", &b1.name, &b1.price, &b1.pages);
printf("Enter the Name, Price and Pages of Book 2: ");
scanf("%c %f %d", &b2.name, &b2.price, &b2.pages);
printf("Here is the data you've entered: \n");
printf("Name: %c Price: %f Pages: %d\n", b1.name, b1.price, b1.pages);
printf("Name: %c Price: %f Pages: %d\n", b2.name, b2.price, b2.pages);
return 0;
}
But I'm not getting the output as desired. My
Output Image
Your question is similar to this one: scanf() leaves the newline character in the buffer
And can be corrected like this:
#include <stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
char buffer[255];
struct book b1, b2;
printf("Enter the Name, Price and Pages of Book 1: ");
scanf(" %c %f %d", &b1.name, &b1.price, &b1.pages);
printf("Enter the Name, Price and Pages of Book 2: ");
scanf(" %c %f %d", &b2.name, &b2.price, &b2.pages);
printf("Here is the data you've entered: \n");
printf("Name: %c Price: %f Pages: %d\n", b1.name, b1.price, b1.pages);
printf("Name: %c Price: %f Pages: %d\n", b2.name, b2.price, b2.pages);
return 0;
}
Note the leading space before the %c input.
I've been trying to do a exercise that I have to input a name and a score to 16 candidates. Then the program should return who's the winner (name and score). I'm new into programming and I'm struggling with this program. In my mind I have to get the highest number between these 16 inputs but i have no clue how to do this, that's the part that broke me. Tried a lot of things and nothing worked.
for(candidates = 16; candidates >= 0; candidates--){
printf("\n******************COMPETITION MISS EXTREMA******************\n");
printf("Input candidate name: ");
scanf("%s", name);
printf("Input candidate score:e %s: ", name);
scanf("%f", &score);
printf("Candidate: %s Score: %.2f", name, score);
}
return 0;
}
Create a maximumScore variable and each time you input a score, compare the score with the maximumScore. If the score is greater than maximumScore, update maximumScore with score and continue the process until you complete the execution of for loop.
Finally print the maximumScore to get the maximum number.
float maximumScore = 0.0;
for(candidates = 16; candidates >= 0; candidates--){
printf("\n******************COMPETITION MISS EXTREMA******************\n");
printf("Input candidate name: ");
scanf("%s", name);
printf("Input candidate score:e %s: ", name);
scanf("%f", &score);
printf("Candidate: %s Score: %.2f", name, score);
if (maximumScore < score) {
maximumScore = score;
}
}
printf("Maximum Score: %f", maximumScore);
return 0;
}
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));
#include<stdio.h>
#define UPPER 999999
#define LOWER 11111
int main(void)
{
// Local Declarations
double price = 89.99;
char grade = 'B';
int age = 97;
// Statements
printf("Homework 2:\n\nUsing printf\n");
printf(" age = %c, age\n");
printf("grade = %d, grade\n");
printf("price = %f, price\n\n");
printf("Using both printf and scanf\n");
printf("Enter a new value for age: ");
scanf("%d", &age);
printf("Enter a new value for grade: ");
scanf("%c", &grade);
printf("Enter a new value for price: ");
scanf("%lf", &price);
printf("Print the new values\n");
printf(" age = %d \n", age);
printf("grade = %c\n", grade);
printf("price = %lf\n\n", price);
print("\n\nPrinting two defined constants: "UPPER" and "LOWER"\n");
print("UPPER = %08d\n", UPPER);
print("LOWER = %08d\n", LOWER);
return 0;
} // end of main
Above is my programme and im supposed to fix it. I've been at it for almost 3 hours now still can figure out the problem. I've got a error and a few warnings.
warning: too few arguments for format
Several warnings for the statements in the mid body
error: expected ')' before numeric constant
this error is for printing two constants.
print("\n\nPrinting two defined constants: "UPPER_S" and "LOWER_S"\n");
would only work if UPPER_S and LOWER_S were #defined as:
#define UPPER_S "999999"
#define LOWER_S "11111"
Alternatively you could use the two following macros to "stringify" the numerical #defines:
#define _STRINGIFY(s) #s
#define STRINGIFY(s) _STRINGIFY(s)
and then do:
#define UPPER 999999
#define LOWER 11111
fputs("\n\nPrinting two defined constants: "STRINGIFY(UPPER)" and "STRINGIFY(LOWER)"\n", stdout);
I came here looking for the answer to this question and it seems most people just got hung up on syntax errors rather than caring what the actual question is. You can use printf with #defined values as though they are normal variables. However you must pay close attention to the type.
#define HEXNUM 0xA8
#define NUMBER 129
#define STRING "somestring"
#include <stdio.h>
int main(void) {
printf("hex number: %x\n", HEXNUM);
printf("number: %d\n", NUMBER);
printf("string: %s", STRING);
return 0;
}
Things like
printf("grade = %d, grade\n");
expect an argument for '%d'. It should be in the form of
printf("grade = %d\n", grade);
You need to put your printf args outside of quotes.
Current:
// Statements
printf("Homework 2:\n\nUsing printf\n");
printf(" age = %c, age\n");
printf("grade = %d, grade\n");
printf("price = %f, price\n\n");
Should be:
// Statements
printf("Homework 2:\n\nUsing printf\n");
printf(" age = %c\n", age);
printf("grade = %d\n", grade);
printf("price = %f\n\n", price);
One method is to assign the constant defined variable value to a local variable value. This way it will be useful in printf("%d", X); method.
However, you could use const int EXAMPLE=12, and then EXAMPLE could be used in the standard printf method, e.g. printf("%d\n", EXAMPLE);
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 );