printf() error

This is a discussion on printf() error within the C Programming forums, part of the General Programming Boards category; Code: void jogo() { system ("cls"); printf("Jogo Matarrocha\n\n"); printf("Introduza o seu nome: "); scanf("%s",&nome); for (dinheiro=300;dinheiro>0;) { printf("O que deseja ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    7

    printf() error

    Code:
    void jogo()
    {
        
        system ("cls");
        printf("Jogo Matarrocha\n\n");
        printf("Introduza o seu nome: ");
        scanf("%s",&nome);
        for (dinheiro=300;dinheiro>0;)
        {
            
            printf("O que deseja faxer?\n");
            printf("a: Apostar\nb: Comprar\nc: Sair\n\n");     
            scanf("%c",&a);
            
            switch (a)
    {
    case 'a' : apost();
    case 'b' : comprar();
    case 'c' : printf("");
    }       
    }    
        printf("Perdeu");
        getch();
        
    }
    Code:
     void apost()
    {
        fflush(stdin);
        printf("Senhor %s... Tem %d euros\n",nome,dinheiro);
            printf("Quanto deseja apostar?\n");
            scanf("%d",&aposta);
            ra = ( rand() % (1 - 0 + 1) + 0 );        
            if (ra==0)
            {
            printf("Parabens ganhou %d euros\n",aposta);
            dinheiro=dinheiro+aposta;
            }    
            if (ra==1) 
            {
            printf("Perdeu %d euros\n",aposta);
            dinheiro=dinheiro-aposta;
            }
            
    }
    when i run a program with this function.. it dysplay 2 times the:
    Code:
      printf("Senhor %s... Tem %d euros\n",nome,dinheiro);
            printf("Quanto deseja apostar?\n");
    scanf("%d",&aposta);
    and only scan 1 time
    Last edited by Wipe; 01-15-2005 at 11:18 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Well there are several things wrong with the code as it stands.
    1) where are all the variables declared? I mean a lot of them should be local (for example loop control variables), but everything seems to be global.
    2) why are no parameters being passed between functions.
    3) fflush(stdin); is undefined - it might work for you, but it doesn't work for me. This topic is discussed in the FAQ.
    4) scanf() is a tricky function, and %c as a conversion makes it especially difficult.
    5) case 'a' : apost();
    There are no break statements, so typing in 'a' causes all 3 functions to be called.
    6. ra = ( rand() % (1 - 0 + 1) + 0 );
    This seems an overly complex way of saying ra = ( rand() % 2 );
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    *CLOSED*
    Last edited by Wipe; 01-15-2005 at 12:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  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