#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    8
    Rep Power
    0

    Help. execution stopped everytime i try to input data


    this is a syntax i've been trying to run. apparently it stopped as i entered the first value.

    can anyone check why this is happening?
    this is the value i am trying to key in

    Initial House Cost Annual Fuel Cost Tax Rate
    67,000 2,300 0.025
    62,000 2,500 0.025
    75,000 1,850 0.020


    below is my

    #include <stdio.h>
    #include <conio.h>

    int main(void)

    {

    float houseInt1 , houseInt2 , houseInt3 ;
    float fuelAnnual1 , fuelAnnual2 , fuelAnnual3;
    float taxRate1 , taxRate2 ,taxRate3;
    float total1 , total2 , total3;

    printf("Enter the initial house cost for house number 1\n\n");
    scanf("%f\n\n",houseInt1);

    printf("Enter the Annual Fuel Cost for house number 1\n\n");
    scanf("%f\n\n",fuelAnnual1);

    printf("Enter the taxRate for house number 1\n\n");
    scanf("%f\n\n",taxRate1);

    printf("Enter the initial house cost for house number 2\n\n");
    scanf("%f\n\n",houseInt2);

    printf("Enter the Annual Fuel Cost for house number 2\n\n");
    scanf("%f\n\n",fuelAnnual2);

    printf("Enter the taxRate for house number 2\n\n");
    scanf("%f\n\n",taxRate2);

    printf("Enter the initial house cost for house number 3\n\n");
    scanf("%f\n\n",houseInt3);

    printf("Enter the Annual Fuel Cost for house number 3\n\n");
    scanf("%f\n\n",fuelAnnual3);

    printf("Enter the taxRate for house number 3\n\n");
    scanf("%f\n\n",taxRate3);

    total1 = houseInt1 + fuelAnnual1*5 + houseInt1*taxRate1*5;
    total2 = houseInt2 + fuelAnnual2*5 + houseInt2*taxRate2*5;
    total3 = houseInt3 + fuelAnnual3*5 + houseInt1*taxRate3*5;


    printf("total cost of a house after a five-year period\n\n");
    printf("house 1 = %.2f \t house 2 = %.2f \t house 3 = %.2f", total1, total2, total3 );

    if (total1<=total2 && total1<=total3)
    printf("best buy is house number 1 = %.2f", total1);
    else if(total2<= total1 && total2<=total3)
    printf("best buy is house number 2 = %.2f", total2);
    else
    printf("best buy is house number 3 = %.2f", total3);
    return 0;
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Code:
    scanf("%f\n\n",houseInt1);
    Why are you including two line breaks?

    Also make sure you read into the address of the variable.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    8
    Rep Power
    0
    Originally Posted by bullet
    Code:
    scanf("%f\n\n",houseInt1);
    Why are you including two line breaks?

    Also make sure you read into the address of the variable.

    ok i did what you say but still facing the same problem


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    
    {
    
    	float houseInt1 , houseInt2 , houseInt3 ;
    	float fuelAnnual1 , fuelAnnual2 , fuelAnnual3;
    	float taxRate1 , taxRate2 ,taxRate3;
    	float total1 , total2 , total3;
    	
    			printf("Enter the initial house cost for house number 1");
    			scanf("%f",houseInt1);
    			
    			printf("Enter the Annual Fuel Cost for house number 1");
    			scanf("%f",fuelAnnual1);
    			
    			printf("Enter the taxRate for house number 1");
    			scanf("%f",taxRate1);
    
    			printf("Enter the initial house cost for house number 2");
    			scanf("%f",houseInt2);
    			
    			printf("Enter the Annual Fuel Cost for house number 2");
    			scanf("%f",fuelAnnual2);
    			
    			printf("Enter the taxRate for house number 2");
    			scanf("%f",taxRate2);
    			
    			printf("Enter the initial house cost for house number 3");
    			scanf("%f",houseInt3);
    			
    			printf("Enter the Annual Fuel Cost for house number 3");
    			scanf("%f",fuelAnnual3);
    			
    			printf("Enter the taxRate for house number 3");
    			scanf("%f",taxRate3);
    			
    		total1 = houseInt1 + fuelAnnual1*5 + houseInt1*taxRate1*5;
    		total2 = houseInt2 + fuelAnnual2*5 + houseInt2*taxRate2*5;
    		total3 = houseInt3 + fuelAnnual3*5 + houseInt1*taxRate3*5;
    
    			
    	printf("total cost of a house after a five-year period");
    	printf("house 1 = %f  house 2 = %f  house 3 = %f", total1, total2, total3 );
    	
    	if (total1<=total2 && total1<=total3)
    		printf("best buy is house number 1 = %f", total1);
    	else if(total2<= total1 && total2<=total3)
    		printf("best buy is house number 2 = %f", total2);
    	else
    		printf("best buy is house number 3 = %f", total3);	
    	return 0;	
    }
    i don't know how to attach picture of the
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    8
    Rep Power
    0
    found it.. i didn't include "&" at scanf
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Here is what I get when I compile your program (WinXP, MinGW gcc):
    C:TEST>gcc -Wall gagak1.c
    gagak1.c: In function `main':
    gagak1.c:12: warning: format argument is not a pointer (arg 2)
    gagak1.c:15: warning: format argument is not a pointer (arg 2)
    gagak1.c:18: warning: format argument is not a pointer (arg 2)
    gagak1.c:21: warning: format argument is not a pointer (arg 2)
    gagak1.c:24: warning: format argument is not a pointer (arg 2)
    gagak1.c:27: warning: format argument is not a pointer (arg 2)
    gagak1.c:30: warning: format argument is not a pointer (arg 2)
    gagak1.c:33: warning: format argument is not a pointer (arg 2)
    gagak1.c:36: warning: format argument is not a pointer (arg 2)

    C:TEST>
    Those lines contain your scanf calls. scanf needs the address of the variable, because it needs to know where to put the converted value it generates. Instead, you were giving it garbage float values, which it would then try to interpret as addresses. Your program was filled with bombs just waiting to go off. I'm surprised it didn't just crash because of access violations or SEGFAULTs.

    Feed scanf what it expects to be fed, the addresses of those variables, not their values.

    But a far more important question is: Why are you ignoring warnings?
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Originally Posted by gagakzs
    found it.. i didn't include "&" at scanf
    Indeed you did
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    8
    Rep Power
    0
    Originally Posted by dwise1_aol
    Here is what I get when I compile your program (WinXP, MinGW gcc):

    Those lines contain your scanf calls. scanf needs the address of the variable, because it needs to know where to put the converted value it generates. Instead, you were giving it garbage float values, which it would then try to interpret as addresses. Your program was filled with bombs just waiting to go off. I'm surprised it didn't just crash because of access violations or SEGFAULTs.

    Feed scanf what it expects to be fed, the addresses of those variables, not their values.

    But a far more important question is: Why are you ignoring warnings?
    dwise1_aol when i complie and run using DevC the black panels open . it just doesn't give the warning like the ones you got there. perhaps i should use other compiler.. sigh -__-"
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    It's the same compiler! MinGW gcc is what Dev-C++ installs -- Dev-C++ is just the IDE. In fact, that's how I got my MinGW gcc compiler, by installing Dev-C++. I just gave up on that worse-than-worthless IDE, but I still run gcc from the command line.

    You need to set the options in Dev-C++ to display warnings. Though some have reported that that still doesn't work.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    8
    Rep Power
    0
    Originally Posted by dwise1_aol
    It's the same compiler! MinGW gcc is what Dev-C++ installs -- Dev-C++ is just the IDE. In fact, that's how I got my MinGW gcc compiler, by installing Dev-C++. I just gave up on that worse-than-worthless IDE, but I still run gcc from the command line.

    You need to set the options in Dev-C++ to display warnings. Though some have reported that that still doesn't work.
    i have change the compiler setting

    Tools>Compiler Option>Settings>Warnings

    and change to "Yes"

    for
    Show most warnings (-Wall)
    Show some more warnings (-Wextra)


    its finally working tq

IMN logo majestic logo threadwatch logo seochat tools logo