Random Segmentation Fault

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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
 
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);
 
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.
 

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
 

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...
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Members online

Top