Thread: Why wont this work?

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Why wont this work?

    Ok, this has been bothering me for about 30 minutes now. I cannot figure out why this code doesnt work. It compiles with no errors, but as soon as I enter the first number, it quits and gives me an error.

    Code:
    #include <stdio.h>
    
    struct rect {
    	float length, width, height;
    };
    
    int main() {
    
    	struct rect box;
    	float volume=0;
    
    	printf("Please enter the length of the box: ");
    	scanf("%f", box.length);
    	printf("Please enter the width of the box: ");
    	scanf("%f", box.width);
    	printf("Please enter the height of the box: ");
    	scanf("%f", box.height);
    
    	volume = box.length * box.width * box.height;
    
    	printf("The volume of the box is: %f\n",volume);
    
    	if(box.length == box.width  && box.width == box.height) {
    		printf("The box is a perfect cube\n");
    	}
    	else {
    		printf("The box is not a perfect cube\n");
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    scanf("%f", &box.length);

    You have to pass the address to scanf so it can you the variable, except for strings since thats 'implied'.

    Check out a site like
    cplusplus.com's refference section for this stuff. Or a POSIX book.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    OMG, I cant believe I did that haha. I knew that. It was sooo obvious too... wow I feel stupid... I guess its just late and I was overlooking that...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM