1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Random Segmentation Fault

Discussion in 'Software Development' started by SuitedMonkey, Jan 27, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. SuitedMonkey

    SuitedMonkey Thread Starter

    Joined:
    Nov 10, 2012
    Messages:
    51
    So i'm trying to make a program to add clocks set in the 24 hours format.

    but after i get input from the user for clock one the program crashes with a segmentation fault...help?
    Code:
    #include <stdio.h>
    void main(){
    int clock1H; //hours for clock 1
    int clock1M; //minute for clock 1
    int clock1S; // seconds for clock 1
    int clock2H; //hours for for clock 2
    int clock2M; //minutes for clock 2
    int clock2S; //seconds for clock 2
    int clock3H; //hours for clock 3
    int clock3M; //minute for clock 3
    int clock3S; // seconds for clock 3
    char operator; //determines whether addition or subtracting will be used.
    
    printf("Please enter the time in 24 hour format for clock 1 in hour, minute, seconds \n");
    scanf("%d, %d, %d,", clock1H, clock1M, clock1S);
    
    
    it gives me the error somewhere in there... but here's the full pogram just in case
    Code:
    #include <stdio.h>
    void main(){
    int clock1H; //hours for clock 1
    int clock1M; //minute for clock 1
    int clock1S; // seconds for clock 1
    int clock2H; //hours for for clock 2
    int clock2M; //minutes for clock 2
    int clock2S; //seconds for clock 2
    int clock3H; //hours for clock 3
    int clock3M; //minute for clock 3
    int clock3S; // seconds for clock 3
    char operator; //determines whether addition or subtracting will be used.
    
    printf("Please enter the time in 24 hour format for clock 1 in hour, minute, seconds \n");
    scanf("%d, %d, %d,", clock1H, clock1M, clock1S);
    
    
    printf("Please enter the time in 24 hour format for clock 2 in hour, minute, seconds \n");
    scanf("%d, %d, %d,", clock2H, clock2M, clock2S);
    
    printf("Please enter whether you want to add or subtract the clocks. \n");
    scanf("%c", &operator);
    
    
            if (operator == '+'){
                    clock3S == clock2S + clock1S;
                    if (clock3S >= 60){
                            clock3M++;
                            clock3S -= 60;
                            }
                    clock3M == clock2M + clock1M;
                    if (clock3M >= 60){
                            clock3H++;
                            clock3M -= 60;
                            }
                    clock3H == clock2H + clock1H;
                    if (clock3H >= 24){
                            clock3H -= 24;
                            }
            printf("%2d, %2d, %2d \n", clock3H, clock3M, clock3S);
            }
    }
    
    any and all help would be appreciated
     
  2. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    You need to use ampersands when using scanf so that the input from the user goes into the memory location of the variable, and not into the memory location held in the variable (probably a random location which won't be in range for your program).

    i.e.
    Code:
    scanf("%d, %d, %d,", &clock1H, &clock1M, &clock1S);
    
     
  3. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
  4. nikomaster

    nikomaster

    Joined:
    Jan 15, 2013
    Messages:
    87
    andy is right, function scanf requires a pointer to be passed as parameters. This is the way it can write the variable content.
     
  5. SuitedMonkey

    SuitedMonkey Thread Starter

    Joined:
    Nov 10, 2012
    Messages:
    51
    Yea...i can't believe i missed the & on scanf...of course it's always the smallest mistake that screws the whole program
     
  6. SuitedMonkey

    SuitedMonkey Thread Starter

    Joined:
    Nov 10, 2012
    Messages:
    51
    so i added the & and it works now, sort of...it accepts the first statement of input, then skips the second and third and ends the program...
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1087058

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice