423,850 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

Logical errors in code, output not correct

P: 1
I am very new to programming and do not know what is wrong with this code...
project due soon. need feedback asap please...



Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. // Constant
  4. #define COST_OF_GLASS .02
  5.  
  6. int main() {
  7.  
  8.     // Declare and initiate variables
  9.     int length; 
  10.     int width; 
  11.     int height;
  12.     int cost;
  13.  
  14.     // Prompt length
  15.     printf("Please enter the length of the tank.\n");
  16.     scanf("%lf", &length);
  17.  
  18.     // Prompt width
  19.     printf("Please enter the width of the tank.\n");
  20.     scanf("%lf", &width);
  21.  
  22.     // Prompt height
  23.     printf("Please enter the height of the tank.\n")
  24.     scanf("%lf", &height);
  25.  
  26.     // Calculate glass demensions
  27.     total1 = (length * height)*2
  28.     total2 = (length * width)*2
  29.     total3 = (height * width)
  30.     total = total1 + total2 + total3
  31.     cost = total * .02
  32.  
  33.     // Print results
  34.     printf("The goldfish tank costs %lf cost to build")
  35.  
  36.     return 0;
  37. }
Sep 14 '07 #1
Share this Question
Share on Google+
2 Replies


Savage
Expert 100+
P: 1,764
I am very new to programming and do not know what is wrong with this code...
project due soon. need feedback asap please...



#include <stdio.h>

// Constant
#define COST_OF_GLASS .02

int main() {

// Declare and initiate variables
int length;
int width;
int height;
int cost;

// Prompt length
printf("Please enter the length of the tank.\n");
scanf("%lf", &length);

// Prompt width
printf("Please enter the width of the tank.\n");
scanf("%lf", &width);

// Prompt height
printf("Please enter the height of the tank.\n")
scanf("%lf", &height);

// Calculate glass demensions
total1 = (length * height)*2
total2 = (length * width)*2
total3 = (height * width)
total = total1 + total2 + total3
cost = total * .02

// Print results
printf("The goldfish tank costs %lf cost to build")

return 0;
}
For inputing integer, flag is %d,not %lf.

Savage
Sep 14 '07 #2

100+
P: 121
// Print results
printf("The goldfish tank costs %lf cost to build")

should be:

// Print results
printf("The goldfish tank costs %lf cost to build", cost);
Sep 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.