An access violation (segmentation fault) raised in program

This is a discussion on An access violation (segmentation fault) raised in program within the C Programming forums, part of the General Programming Boards category; Hi, I wrote a C program, but the error when running it under windows and closes ... when I debug ...

  1. #1
    Registered User mathiasbenitez's Avatar
    Join Date
    Mar 2012
    Location
    São Paulo - Brazil
    Posts
    6

    Question An access violation (segmentation fault) raised in program

    Hi,

    I wrote a C program, but the error when running it under windows and closes ...
    when I debug in Dev-C it appears the following message "An access violation(segmentation fault) raised in program."

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int contmul,conthom,contsim,contnao,ressim,resnao,soma;         //contador de mulheres, contador de homens, contador de sim, contador de não,respostas sim, resposta não
    char sexo,resp[3];               //sexo, resposta
    
    
         int main()
         {
               printf("\n Qual o seu sexo? F/M \n");
               scanf("%c", sexo);
                           if(sexo=='f'||sexo=='F')
                           {
                                        ++contmul;     //mais um no contmul
                           };
                           if(sexo=='m'||sexo=='M')
                           {
                                        ++conthom;     //mais um no conthom
                           };
               printf("\n voce e maior de idade? sim ou nao \n");
               scanf("%s", resp);
                           if(resp!= "sim" || resp!="SIM" || resp!="NAO" || resp!="nao")       //verifica se a resposta é sim ou nao
                                     {
                                           printf("\n Resposta invalida! valido apenas sim e nao");  
                                     }         
                           else 
                                if(resp=="sim" || resp=="SIM")
                                     ++ressim;
                           else
                                          
                           if(resp=="nao" || resp=="NAO")
                               {
                                               ++resnao;
                                               }
         
             soma = contmul+conthom;
             printf("\n Foram registrados %i pessoas" ,soma);     //mostra a soma de contmul + conthom                    
             printf("\n %i homens e %i mulheres" ,conthom, contmul);       //mostra quantos homens e mulhers foram registrados
             printf("\n %i maiores de idade e %i menores de idade" ,ressim,resnao);
             
             printf("\n Fim do programa");
                                               
                                               
                                               
                                               
                                               
                                               
                                               
               getch();
               return(0);
               }
    Can someone help me?
    PS. this is one of my first programs, that I lay on the subject

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I'm guessing the problem is
    Code:
    scanf("%c", sexo);
    Try changing it to
    Code:
    scanf(" %c", &sexo);
    Also, the indentation is absolutely awful and you're abusing globals.

  3. #3
    Registered User mathiasbenitez's Avatar
    Join Date
    Mar 2012
    Location
    São Paulo - Brazil
    Posts
    6
    that was it, thanks.

    and thanks for the criticism. as I said I'm starting now and I'm kind of layman on the subject.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-03-2012, 01:48 PM
  2. Replies: 8
    Last Post: 10-01-2011, 11:26 PM
  3. Replies: 3
    Last Post: 01-13-2010, 10:16 PM
  4. access violation segmentation fault raised in your program
    By dinamit875 in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2009, 11:44 AM
  5. Replies: 3
    Last Post: 07-17-2008, 08:45 AM

Tags for this Thread


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