scanf("%hu %hu %hu\n", &i, &teams[i].problemas, &teams[i].tempo);
cant i do this??
scanf("%hu %hu %hu\n", &i, &teams[i].problemas, &teams[i].tempo);
cant i do this??
Please use [code][/code]Tags
> cant i do this??
Yes you can, but unless you post more information, we can't tell which of the many hundreds of ways it's possible to screw up on this you've made.
Post a whole (short) program showing the problem and say which OS/Compiler you're using.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
using GCCCode:#include <stdio.h> #include <stdlib.h> #include <string.h> struct equipa { unsigned short int id; unsigned short int problemas; unsigned short int tempo; } int main () { unsigned short int N; /* numero de equipas */ unsigned short int P; /* numero de problemas */ unsigned short int T; /* tempo em minutos do torneio */ unsigned short int R; /* numero de problemas resolvidos */ scanf("%hu %hu %hu %hu", &N, &P, &T, &R); assert(N<100); assert(P<100); assert(T<600); assert(N<10000); equipa teams[N]; unsigned short int i; for(int i2=0;i<N;i2++) { scanf("%hu %hu %hu\n", &i, &teams[i].problemas, &teams[i].tempo); teams[i].id=1; fflush(stdin); } equipa best[3]; best[0].problemas=0; best[0].tempo=T; best[1].problemas=0; best[1].tempo=T; best[2].problemas=0; best[2].tempo=T; for(int i3=0;i<N;i3++) { if(teams[i3].tempo<=T && teams[i3].problemas<=P) { if(teams[i3].problemas > best[0].problemas || (teams[i3].problemas == best[0].problemas && teams[i3].tempo < best[0].tempo)) { best[0].id=teams[i3].id; best[0].problemas=teams[i3].problemas; best[0].tempo=teams[i3].tempo; } else { if(teams[i3].problemas > best[1].problemas || (teams[i3].problemas == best[1].problemas && teams[i3].tempo < best[1].tempo)) { best[1].id=teams[i3].id; best[1].problemas=teams[i3].problemas; best[1].tempo=teams[i3].tempo; } else if(teams[i3].problemas > best[2].problemas || (teams[i3].problemas == best[2].problemas && teams[i3].tempo < best[2].tempo)) { best[2].id=teams[i3].id; best[2].problemas=teams[i3].problemas; best[2].tempo=teams[i3].tempo; } } } } printf("Primeiro classificado:\nNúmero da equipa: %hu\nProblemas resolvido: %hu\nTempo: %hu\n", best[0].id, best[0].problemas, best[0].tempo); printf("Segundo classificado:\nNúmero da equipa: %hu\nProblemas resolvido: %hu\nTempo: %hu\n", best[1].id, best[1].problemas, best[1].tempo); printf("Terceiro classificado:\nNúmero da equipa: %hu\nProblemas resolvido: %hu\nTempo: %hu\n", best[2].id, best[2].problemas, best[2].tempo); return 0; }
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct equipa { unsigned short int id; unsigned short int problemas; unsigned short int tempo; } equipa; /*!! Missing ; */ int main() { unsigned short int N; /* numero de equipas */ unsigned short int P; /* numero de problemas */ unsigned short int T; /* tempo em minutos do torneio */ unsigned short int R; /* numero de problemas resolvidos */ equipa teams[100]; /*!! Standard C doesn't have val-arrays (new C99 does) */ equipa best[3]; unsigned short int i; scanf("%hu %hu %hu %hu", &N, &P, &T, &R); #if 0 assert(N < 100); /*!! Don't use assert to validate user input */ assert(P < 100); assert(T < 600); assert(N < 10000); equipa teams[N]; /*!! Standard C doesn't have val-arrays (new C99 does) */ #endif for (int i2 = 0; i < N; i2++) { /* Standard C doesn't support this type of for loop */ scanf("%hu %hu %hu\n", &i, &teams[i].problemas, &teams[i].tempo); teams[i].id = 1; /* fflush(stdin); !! DO NOT use fflush(stdin) - see the FAQ */ } best[0].problemas = 0; best[0].tempo = T; best[1].problemas = 0; best[1].tempo = T; best[2].problemas = 0; best[2].tempo = T; for (int i3 = 0; i < N; i3++) { if (teams[i3].tempo <= T && teams[i3].problemas <= P) { if (teams[i3].problemas > best[0].problemas || (teams[i3].problemas == best[0].problemas && teams[i3].tempo < best[0].tempo)) { best[0].id = teams[i3].id; best[0].problemas = teams[i3].problemas; best[0].tempo = teams[i3].tempo; } else { if (teams[i3].problemas > best[1].problemas || (teams[i3].problemas == best[1].problemas && teams[i3].tempo < best[1].tempo)) { best[1].id = teams[i3].id; best[1].problemas = teams[i3].problemas; best[1].tempo = teams[i3].tempo; } else if (teams[i3].problemas > best[2].problemas || (teams[i3].problemas == best[2].problemas && teams[i3].tempo < best[2].tempo)) { best[2].id = teams[i3].id; best[2].problemas = teams[i3].problemas; best[2].tempo = teams[i3].tempo; } } } } printf("Primeiro classificado:\nNúmero da equipa: %hu\nProblemas resolvido: %hu\nTempo: %hu\n", best[0].id, best[0].problemas, best[0].tempo); printf("Segundo classificado:\nNúmero da equipa: %hu\nProblemas resolvido: %hu\nTempo: %hu\n", best[1].id, best[1].problemas, best[1].tempo); printf("Terceiro classificado:\nNúmero da equipa: %hu\nProblemas resolvido: %hu\nTempo: %hu\n", best[2].id, best[2].problemas, best[2].tempo); return 0; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
all of those problems are corrected if i compile as C99, the last standardization of C?
Only your for loops and val arrays
Missing ;, using assert() and fflush(stdin) are wrong period.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
why do u guys always use typedef when creating structs?
Because C isn't C++.
Valid in C++, not valid in C
If you want to sayCode:struct foo { int member; }; foo bar;
foo bar;
in C, then you need a typedef, otherwise you have to use a bit more verbage and say
struct foo bar;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
When i compile with Dev-Cpp (C99 i guess) it gives me an error in the for's :S why? i didnt understood what u said... shall i use a while instead?
You know, if you actually posted your errors it would help.
Quzah.
Hope is the first step on the road to disappointment.
'for' loop initial declaration used outside C99 mode
thats the error
the lines are just the lines where there are the for's
You've already been told why.
This makes it C99. If you aren't compiling in C99 mode, you cannot put the keyword int there. It has to be like this:Code:for (int i3 = 0; i < N; i3++) {
You aren't compiling in C99 mode, because if you were, it wouldn't tell you "outside of C99 mode". Also, in case you don't know, were you do do the first method, and compile in C99 mode, the variable "i3" ceases to exist when the loop ends.Code:int i3; ...stuff... for ( i3 = 0; i < N; i3++) {
Quzah.
Hope is the first step on the road to disappointment.
if im not compiling on C99 mode is there any way to compile on c99 mode using dev-cpp?
I bet if you read the documentation for it you could find out. Or if you can't find the documentation, I bet if you Googled it you could find out.
Quzah.
Hope is the first step on the road to disappointment.
as of now - no. Dev-Cpp only supports some c99 features, like most compilers. Your best bet would be to follow salem an Quzah's advice an make it C90, what most compilers support. For compiler specific info you can check this out.Originally Posted by j_spinto
Warning: Opinions subject to change without notice
The C Library Reference Guide
Understand the fundamentals
Then have some more fun