PDA

View Full Version : [SOLVED] Segmentation Fault! Oh no!



spadewarrior
June 3rd, 2008, 03:36 PM
Could one of you fine people please look at this function I've written and show me where the segmentation fault is? I've been staring at it for about 30 minutes and cannot work it out. It's driving me mad. i'm sure it's obvious but I've been working on this so long I must be going blind.

Help appreciated.



int get_money(void)
{
int total, coin;
system("clear");
mvprintw(12, 22, "Please enter money. ");
mvprintw(13, 22, "Only 5p, 10, 20p, 50p or 100p accepted. ");
mvprintw(14, 22 ,"Enter 0 to finish ");
refresh();

while(coin!=0)
{
fflush(stdin);
scanf("%d", coin);
if( (coin==5) || (coin==10) || (coin==20) || (coin==50) || (coin==100) )
{
total=(total+coin);
}
else
{
mvprintw(16, 22, "Invalid coin ");
refresh();
}

}

return total;
}

Wybiral
June 3rd, 2008, 03:43 PM
scanf("%d", coin);

AZzKikR
June 3rd, 2008, 03:43 PM
Try


scanf("%d", &coin);

spadewarrior
June 3rd, 2008, 03:45 PM
Of course!

Boy do I feel ashamed....

Thanks.

AZzKikR
June 3rd, 2008, 03:49 PM
It helps if you compile using the flag -Wall, which generates all warnings (assuming you use GCC). If I tried to compile it, it would output:



make all
mkdir -p ./bin
g++ -Wall -O2 -c ./src/main.cpp -o ./bin/main.o
./src/main.cpp: In function `int get_money()':
./src/main.cpp:10: warning: format argument is not a pointer (arg 2)
./src/main.cpp:8: warning: 'coin' might be used uninitialized in this function
./src/main.cpp:8: warning: 'total' might be used uninitialized in this function
g++ -o ./bin/langton ./bin/langton.o ./bin/main.o


Helpful information :)

spadewarrior
June 3rd, 2008, 03:59 PM
Didn't know about that. Thanks!

nvteighen
June 3rd, 2008, 05:19 PM
gcc -Wall is the C programmer's best friend! :)

Don't forget to mark this thread as solved, using the "Thread Tools" above.

pmasiar
June 3rd, 2008, 06:23 PM
and don't forget to thank the person, who instead of giving you a fish, shown you how to fish by yourself.

That gcc -Wall trick should go to FAQ, if not there, IMHO.