scanf("%hu %hu %hu\n", &i, &teams[i].problemas, &teams[i].tempo);
cant i do this??
Printable View
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.
using GCC :)Code:#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;
}
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.
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;
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.
'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.
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.
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.Quote:
Originally Posted by j_spinto
do you know any C99 nice compiler?
> do you know any C99 nice compiler?
http://www.comeaucomputing.com/
Since you're not using anything which is genuinely useful in C99, you're much better off sticking to C89 mode for your C programs.
All you're doing with C99 at the moment is making your program needlessly less portable.
oh... is c99 less portable than c89?
its just because its more recent isnt it?
if so, is ansi more portable than c89?
yes, its becuase its more recent, also IMO some of the features of c99 just make the language more bloated and unnecessary.Quote:
Originally Posted by j_spinto
ainsi C is considered C89.
No. ANSI C is considered any version of C which conforms to the ANSI standard. IE: C89, C90, C99, etc. All of those standards are "ANSI C". They're just different revisions of it.
At any rate, "ANSI C" would be the latest revision of the C standard, or whatever version is being referenced in the source cited.Quote:
C: A Reference Manual, Fifth Edition
Page 6, pharagraph 4:
Officially, "Standard C" is C99. However, we use the term Standard C to refer to
features and concepts of C89 that continue through C99. Features of C that exist only in C99
will be identified as such so that programmers using C89 implementations can avoid them.
Quzah.
so all the ANSI versions have the same portability or there is some differences between C89, C90 and C99 (the 3 ISO / ANSI standards) - the 94 isn't I think, and I've read about it.
If so, which is the most portable and what are the differences?
You have already said that C89 and C99 are the ANSI's... I think it is C99 becase it's more recent
but C89 is portable because you can code in C99 using C89 refferences :)
is it that?
is there any difference between C89/C90 ?
I think Quzah answered that question as to what ansi is. C99 and C89 are basically diff standards the compiler should adhere to and support, but most compilers only support c89/c90 and only a little if any of c99.Quote:
Originally Posted by j_spinto
There isn't so much differences between the two as there is more additons and features. Its sort of like an "upgrade" to the language.
I didn't want to confuse you by saying ainsi C is c89. as was pointed out its any that is supported or endorsed by the committee, i only meant C89 because it is the most widely supported.
http://en.wikipedia.org/wiki/C89#ANSI_C_and_ISO_C
C89, C90:[edit]http://gcc.gnu.org/onlinedocs/gcc/Standards.htmlQuote:
In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C. After a long and arduous process, the standard was completed in 1989 and ratified as ANSI X3.159-1989 "Programming Language C". This version of the language is often referred to as ANSI C. In 1990, the ANSI C standard (with a few minor modifications) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990.
Quote:
The original ANSI C standard (X3.159-1989) was ratified in 1989 and published in 1990. This standard was ratified as an ISO standard (ISO/IEC 9899:1990) later in 1990. There were no technical differences between these publications, although the sections of the ANSI standard were renumbered and became clauses in the ISO standard. This standard, in both its forms, is commonly known as C89, or occasionally as C90, from the dates of ratification.
thanks
btw, the fact of being compactible with less compilers interfers in its portability?
dont worry because I dont think c99 advantages are just the // and the int declaration in the loop :P lol..
I think the long long and those things and BOOLEAN type are quite useful
If im using C99 am i loosing the programm performance and portability?
hugs, João
please answer :P
Boy you're damn lucky there's no reputation now. I believe isn't the first time I've seen you simply bump your posts. Read the forum guide lines. We don't just sit around all day waiting to answer your questions. You see, you just aren't that important.
I could answer your question, but just to prove the point, I won't.
Quzah.
Here's something for you...Quote:
Originally Posted by j_spinto
here are a few posts about c89 and 99. and as always remember google is your best friend.
> please answer
We already did - plenty of times.
We're all done here - Closed.