Need help with C program
#1

This is the code:

Код:
#include <stdio.h>
#include <conio.h>
 
 main ()
{

float a, b, c, d, e, total;
total = a + b + c + d + e;
char mark[50] = "marks obtained in";		
char str[30] = "\t MARKSHEET";
char name[30];
    printf("Enter name: ");
	scanf("%s", name);

// getting marks from the user //
printf("%s Computer:  ", mark, a);
scanf("%.2f", &a);
printf("%s Physics:  ", mark, b);
scanf("%.2f", &b);
printf("%s English:  ", mark, c);
scanf("%.2f", &c);
printf("%s Urdu:  ", mark, d);
scanf("%.2f", &d);
printf("%s Maths:  ", mark, e);
scanf("%.2f", &e);

	
}
Now after I compile and run, it asks for the name and only the first marks for computer and after that, it just prints out others in one line then closes

Reply
#2

Quote:
Originally Posted by CopKing123
Посмотреть сообщение
#include <stdio.h>
#include <conio.h>

main ()
{

float a, b, c, d, e, total;
total = a + b + c + d + e;
This is shit, you declare some uninitialized variables and then add them up?
You don't know the value, it could be anything, so "total" will contain unknown value too

Quote:
Originally Posted by CopKing123
Посмотреть сообщение
// getting marks from the user //
printf("%s Computer: ", mark, a);
scanf("%.2f", &a);
Why are you giving 2 params to the printf call? It will only print "mark", 'a' is unknown and not used anywhere.
Also, don't use %.2f when loading value using scanf, use %f - %.2f should be used when printing it. (Btw, shouldn't you use %.02f?)

Try this:

Код:
#include <stdio.h>
#include <conio.h>
 
 main ()
{

float a, b, c, d, e, total;
char mark[50] = "marks obtained in";		
char str[30] = "\t MARKSHEET";
char name[30];
printf("Enter name: ");
scanf("%s", name);

//if you want to read name and surname, try this:
//scanf("%[^\n]",name); // - this reads everything until \n (newline) character is encountered, therefore "Adolf Hitler" would work too. Your code would only read "Adolf"

// getting marks from the user //
printf("%s Computer:  ", mark);
scanf("%f", &a);
printf("%s Physics:  ", mark);
scanf("%f", &b);
printf("%s English:  ", mark);
scanf("%f", &c);
printf("%s Urdu:  ", mark);
scanf("%f", &d);
printf("%s Maths:  ", mark);
scanf("%f", &e);

printf("Student: %s\n",name);
printf("Computer: %.02f | Physics: %.02f | English %.02f | Urdu: %.02f | Maths: %.02f\n",a,b,c,d,e);
total = a+b+c+d+e;
printf("Total: %.02f\n",total);
}
Reply
#3

Ah, thank you for the explanation!
It's working flawlessly.
I apologize for the mistakes since I'm new to C programming

One more thing, I need to print out a percentage too.. where do I explain the formula?
Reply
#4

Try this:
printf("Computer percentage: %.02f %%\n",(a/total)*100.0);

(%% prints only '%', but it is a special character so you have to write it twice in printf etc)
Reply
#5

No, I meant percentage from total marks.
Reply
#6

Do your homework yourself
Reply
#7

ah.. how can I forget the kid that shows up in the middle of a conversation with his irrelevant reply just to seek attention...

anways, appreciate the help kurence
Reply
#8

What do you use to compile your program? I never found a free C compiler
What Kurence gave is right: note/max_note * 100 or I don't understand what you are trying to do?
Reply
#9

@Dayron try MinGW. It is included in CodeBlocks. Also you can try to download a Comunity Edition of Visual Studio (is free).
Reply
#10

Quote:
Originally Posted by ]Kurence[
Посмотреть сообщение
This is shit, you declare some uninitialized variables and then add them up?
You don't know the value, it could be anything, so "total" will contain unknown value too



Why are you giving 2 params to the printf call? It will only print "mark", 'a' is unknown and not used anywhere.
Also, don't use %.2f when loading value using scanf, use %f - %.2f should be used when printing it. (Btw, shouldn't you use %.02f?)

Try this:

Код:
#include <stdio.h>
#include <conio.h>
 
 main ()
{

float a, b, c, d, e, total;
char mark[50] = "marks obtained in";		
char str[30] = "\t MARKSHEET";
char name[30];
printf("Enter name: ");
scanf("%s", name);

//if you want to read name and surname, try this:
//scanf("%[^\n]",name); // - this reads everything until \n (newline) character is encountered, therefore "Adolf Hitler" would work too. Your code would only read "Adolf"

// getting marks from the user //
printf("%s Computer:  ", mark);
scanf("%f", &a);
printf("%s Physics:  ", mark);
scanf("%f", &b);
printf("%s English:  ", mark);
scanf("%f", &c);
printf("%s Urdu:  ", mark);
scanf("%f", &d);
printf("%s Maths:  ", mark);
scanf("%f", &e);

printf("Student: %s\n",name);
printf("Computer: %.02f | Physics: %.02f | English %.02f | Urdu: %.02f | Maths: %.02f\n",a,b,c,d,e);
total = a+b+c+d+e;
printf("Total: %.02f\n",total);
}
They often teach crap like that to get a grasp on basic programming. I took C and it was like that. I actually lost marks for doing it better because I didn't follow the directions. So it's probably not fair to criticize it if thats how they're being taught.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)