SA-MP Forums Archive
Need help with C program - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: Other (https://sampforum.blast.hk/forumdisplay.php?fid=7)
+--- Forum: Everything and Nothing (https://sampforum.blast.hk/forumdisplay.php?fid=23)
+--- Thread: Need help with C program (/showthread.php?tid=642221)



Need help with C program - CopKing123 - 26.09.2017

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




Re: Need help with C program - ]Kurence[ - 26.09.2017

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



Re: Need help with C program - CopKing123 - 26.09.2017

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?


Re: Need help with C program - ]Kurence[ - 26.09.2017

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)


Re: Need help with C program - CopKing123 - 26.09.2017

No, I meant percentage from total marks.


Re: Need help with C program - Gammix - 26.09.2017

Do your homework yourself


Re: Need help with C program - CopKing123 - 26.09.2017

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


Re: Need help with C program - Dayrion - 26.09.2017

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?


Re: Need help with C program - 10MIN - 26.09.2017

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


Re: Need help with C program - TakeiT - 28.09.2017

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.