You are here: Home → Forum Home → The Mac Observer Forums → Design & Create → Thread
Bus error?
-
I’m working on my fourth c lab for my programming class, and i get a “Bus error” I’ve been looking at my code, and looking at the examples my instructor gave, and i can’t figure out whats wrong… can someone please help?
here’s the program….
//——————————————————————————————————————-
//lab4.c#include <stdio.h>
main ()
{
//——declare data
int x, y, z;
char code;
//——prompt and scan for data
printf(“Please enter a value for Xn”);
scanf(”%d”,&x);
printf(“Please enter a value for Yn”);
scanf(”%d”,&y);
//——decide, operate, and display output
printf(“Would you like to (a)dd, (s)ubtract, (m)ultiply, or (d)ivide the values?n”);
scanf(”%c”,code);
if ( code == ‘a’ )
z = x + y;
printf(“x + y = %dn”,z);if ( code == ‘m’ )
z = x * y;
printf(“x * y = %dn”,z);if ( code == ‘s’ )
z = y - x;
printf(“y - x = %dn”,z);if ( code == ‘d’ )
z = x / y;
printf(“x / y = %dn”,z);
return;}
//——————————————————————————————————————
and the output….
//——————————————————————————————————————
[localhost:/Developer/C Programming (labs)] l0u13% cc lab4.c -o lab4
[localhost:/Developer/C Programming (labs)] l0u13% ./lab4
Please enter a value for X
2
Please enter a value for Y
3
Would you like to (a)dd, (s)ubtract, (m)ultiply, or (d)ivide the values?
Bus error
[localhost:/Developer/C Programming (labs)] l0u13%
//——————————————————————————————————————
it doesn’t give me a chance to enter the value for code, just x, y, and BOOM.. bus error…
//——————————————————————————————————————
ps. I found about pico on tech tv today (go brett larson!!) and I love it, I’ve also got my terminal hacked (well.. customized) to perfection.. black w/ white text and 33% transparency, I love it!!_________________
Peace,
-Louie
“‘INTEL INSIDE’—The World’s Most Widely Used Warning Label”
“The box said “Windows ‘95 or Better”... so I got a Mac.”<font size=-1>[ This Message was edited by: L0u13 on 2002-03-14 15:53 ]</font>
Signature
-Louie
-
ps. I do have my “slash-n” line breaks.. they just didin’t show up in the message up there…
-Louie
Signature
-Louie
-
has to do with the way you are reading in a character.
I think you need to pass a pointer to scanf eg scanf(”%c”,&code;);
look up the man page for scanf
just guessing, but you probably want to include all of the printf statements inside your if blocks {} otherwise it will print all of them everytime.
Signature
All statements are to be taken with a grain of salt
-
got my program working.. just needed to init code as an char array instead of just char.. works fine now
thanks jbraun!!
Signature
-Louie
-
that would make sense. When you declare an array of characters it is actually declaring a pointer to an array, then when you pass it to scanf you are passing a pointer. glad to know all is working.
just remember, computers are fun
Signature
All statements are to be taken with a grain of salt
-
EDGar, do you know of a way to do that another way, without an array?
thanks for the input also.
Signature
-Louie
-
Remember: the right tool for the right job.
scanf is a great tool, but if you are only getting one character from the keyboard/file/stream, use getchar() instead. It is quicker, you don’t have to worry about “Do I have to get the return/newline character as well, and what if this runs under Windows, where the ‘newline’ character is two characters? If I don’t, is there another character in the file/stream/etc?”
Signature
Work is the curse of the drinking classes.
- Oscar Wilde

