The Mac Observer

 
   
 
Bus error?
Posted: 15 March 2002 03:58 AM [ Ignore ]
stars_big_1
Avatar
Total Posts:  4106
Joined  2001-12-26

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

thinkdifferent2.gif
“‘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

Profile
 
 
Posted: 14 March 2002 10:55 AM [ Ignore ] [ # 1 ]
stars_big_1
Avatar
Total Posts:  4106
Joined  2001-12-26

ps. I do have my “slash-n” line breaks.. they just didin’t show up in the message up there…

-Louie

 Signature 

-Louie

Profile
 
 
Posted: 14 March 2002 12:20 PM [ Ignore ] [ # 2 ]
stars_5
Total Posts:  1162
Joined  2001-11-02

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. icon_wink.gif

 Signature 

All statements are to be taken with a grain of salt

Profile
 
 
Posted: 14 March 2002 12:57 PM [ Ignore ] [ # 3 ]
stars_big_1
Avatar
Total Posts:  4106
Joined  2001-12-26

got my program working.. just needed to init code as an char array instead of just char.. works fine now

thanks jbraun!!

 Signature 

-Louie

Profile
 
 
Posted: 14 March 2002 02:57 PM [ Ignore ] [ # 4 ]
stars_5
Total Posts:  1162
Joined  2001-11-02

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 icon_smile.gif

 Signature 

All statements are to be taken with a grain of salt

Profile
 
 
Posted: 14 March 2002 07:37 PM [ Ignore ] [ # 5 ]
stars_big_1
Avatar
Total Posts:  4106
Joined  2001-12-26

EDGar, do you know of a way to do that another way, without an array?

thanks for the input also.

 Signature 

-Louie

Profile
 
 
Posted: 15 March 2002 03:58 AM [ Ignore ] [ # 6 ]
Moderator
Total Posts:  3912
Joined  2001-07-13

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

Profile
 
 
   
 
 

Apple Stock Quote (AAPL)

Loading...

Hot Topics

TMO Express

Join the TMO Express Daily Newsletter to get the latest Mac headlines in your e-mail every weekday. Find out more!

Top Deals From DealBrothers.com

Recent Features

Support The Mac Observer

We noticed you may be running AdBlock on your computer. It takes real money to run this site and to deliver the news, tips, and opinions you love to read.

If you wish to block the ads that pay for the creation of our content, we ask that you instead support TMO Directly, either with a $5 monthly recurring contribution, or a one-time donation of any amount of your choice. Thanks!

Subscribe with Paypal Donate with Paypal