Compile time error

This is a discussion on Compile time error within the C Programming forums, part of the General Programming Boards category; scanf("%hu %hu %hu\n", &i, &teams[i].problemas, &teams[i].tempo); cant i do this??...

  1. #1
    j_spinto
    Guest

    Compile time error

    scanf("%hu %hu %hu\n", &i, &teams[i].problemas, &teams[i].tempo);
    cant i do this??

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    j_spinto
    Guest
    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;
    }
    using GCC

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    j_spinto
    Guest
    all of those problems are corrected if i compile as C99, the last standardization of C?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    j_spinto
    Guest
    why do u guys always use typedef when creating structs?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Because C isn't C++.

    Valid in C++, not valid in C
    Code:
    struct foo { int member; };
    foo bar;
    If you want to say
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    j_spinto
    Guest
    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?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You know, if you actually posted your errors it would help.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    j_spinto
    Guest
    'for' loop initial declaration used outside C99 mode

    thats the error
    the lines are just the lines where there are the for's

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've already been told why.
    Code:
    for (int i3 = 0; i < N; i3++) {
    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:
    int i3;
    
    ...stuff...
    
    for ( 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.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    j_spinto
    Guest
    if im not compiling on C99 mode is there any way to compile on c99 mode using dev-cpp?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  15. #15
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by j_spinto
    if im not compiling on C99 mode is there any way to compile on c99 mode using dev-cpp?
    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.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21