Thread: Help. execution stopped everytime i try to add data from scanf

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    7

    Help. execution stopped everytime i try to add data from scanf

    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
    67,000
    62,000
    75,000

    Annual Fuel Cost
    2,300
    2,500
    1,850

    Tax Rate

    0.025
    0.025
    0.020


    my syntax are as below

    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'm stuck i don't know what to do....

  2. #2
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by chekbob View Post
    i'm stuck i don't know what to do....
    You didn't include & in the scanf.

    Code:
    
    #include <stdio.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;
    }
    Last edited by Cdd101; 11-12-2013 at 01:08 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    All those errors you got about how scanf required pointers instead of values should be giving you a hint about what you need to fix (to wit: all your scanf calls need pointers instead of values).

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    7
    omg.. thank you so much...

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    7
    thank you.. gosh you are right. i should have seen it...

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    The proper solution is turn on warnings in your compiler. Even if you got it this time its too easy to miss these things, that's why all compilers nowadays check for this and issue diagnostics.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is what c99tutorial means by getting a decent compiler, and turning up the warning level.
    Code:
    $ gcc -c -W -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:17:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:20:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:23:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:27:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:30:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:33:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:36:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:39:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:42:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:17:18: warning: ‘houseInt1’ is used uninitialized in this function [-Wuninitialized]
    foo.c:20:18: warning: ‘fuelAnnual1’ is used uninitialized in this function [-Wuninitialized]
    foo.c:23:18: warning: ‘taxRate1’ is used uninitialized in this function [-Wuninitialized]
    foo.c:27:18: warning: ‘houseInt2’ is used uninitialized in this function [-Wuninitialized]
    foo.c:30:18: warning: ‘fuelAnnual2’ is used uninitialized in this function [-Wuninitialized]
    foo.c:33:18: warning: ‘taxRate2’ is used uninitialized in this function [-Wuninitialized]
    foo.c:36:18: warning: ‘houseInt3’ is used uninitialized in this function [-Wuninitialized]
    foo.c:39:18: warning: ‘fuelAnnual3’ is used uninitialized in this function [-Wuninitialized]
    foo.c:42:18: warning: ‘taxRate3’ is used uninitialized in this function [-Wuninitialized]
    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.

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    7
    Quote Originally Posted by Salem View Post
    This is what c99tutorial means by getting a decent compiler, and turning up the warning level.
    Code:
    $ gcc -c -W -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:17:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:20:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:23:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:27:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:30:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:33:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:36:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:39:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:42:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:17:18: warning: ‘houseInt1’ is used uninitialized in this function [-Wuninitialized]
    foo.c:20:18: warning: ‘fuelAnnual1’ is used uninitialized in this function [-Wuninitialized]
    foo.c:23:18: warning: ‘taxRate1’ is used uninitialized in this function [-Wuninitialized]
    foo.c:27:18: warning: ‘houseInt2’ is used uninitialized in this function [-Wuninitialized]
    foo.c:30:18: warning: ‘fuelAnnual2’ is used uninitialized in this function [-Wuninitialized]
    foo.c:33:18: warning: ‘taxRate2’ is used uninitialized in this function [-Wuninitialized]
    foo.c:36:18: warning: ‘houseInt3’ is used uninitialized in this function [-Wuninitialized]
    foo.c:39:18: warning: ‘fuelAnnual3’ is used uninitialized in this function [-Wuninitialized]
    foo.c:42:18: warning: ‘taxRate3’ is used uninitialized in this function [-Wuninitialized]
    thank you for the comments, i am using devc++ compiler i didn't set anything but the compiler didn't even give me any warning... i'll try to search what went wrong to the compiler

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Get Orwell Dev-C++ | Free Development software downloads at SourceForge.net instead.
    It's the maintained fork of dev-c++, and comes with an updated version of gcc.

    Then you normally do something like
    project->settings->compiler
    and you find either check boxes (or an edit box) where you enable all the additional warnings.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2013
    Posts
    7
    Quote Originally Posted by Salem View Post
    Get Orwell Dev-C++ | Free Development software downloads at SourceForge.net instead.
    It's the maintained fork of dev-c++, and comes with an updated version of gcc.

    Then you normally do something like
    project->settings->compiler
    and you find either check boxes (or an edit box) where you enable all the additional warnings.

    thank you Salem the warning is working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2012, 05:42 AM
  2. scanf and formating data
    By xddxogm3 in forum C Programming
    Replies: 23
    Last Post: 04-16-2004, 03:50 PM
  3. Random # everytime (my code)
    By Paz_Rax in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2004, 05:34 AM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  5. I get a parse error everytime I use asm
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 11-09-2001, 01:17 AM