Thread: Help! Warning: Initialization makes integer from pointer without a cast

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Help! Warning: Initialization makes integer from pointer without a cast

    I am writing a program and in line 50 it marks me this: Warning: Initialization makes integer from pointer without a cast. How do I fix it? The problem started when I had to create a loop to ask whether yes or no if the user would like to calculate another trip. I do not know how to fix. Please help.

    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
       
       float numP, numL, HalfCost, EachLcost, TotalHalf, TotalCost, ans;
       int loop;   
    
       printf("\nPlease enter the number of passengers. Children under the age 12 are represented by .5: ");
     
       scanf("%f",&numP);
    
       if (numP<0 )
    	numP=3;
       else
            printf ("%f",numP);
       
       printf ("\nHow many legs the trip has to get to the destination?");
       scanf("%f",&numL);
       TotalHalf = 0;
    
       loop=1;
       while (loop <= numL){ 
    
          printf("\nWhat is the price of leg %d?",loop);
          scanf("%f",&EachLcost);
          TotalHalf =TotalHalf + EachLcost;
          loop++;
    }
       printf("\nThe total cost for half the trip is:");
    
       if(TotalHalf>0)
    	printf("%f",TotalHalf);
       
          
       TotalCost=TotalHalf*2;
       printf("\nThe round trip cost for one person is: $ %f",TotalCost);
       
       printf("\nThe round trip cost for %f persons is: $ %f",numP, TotalCost*numP);
    
    
      // return TotalCost*numP;
        
       printf("\nWould you like to calculate another trip cost? (Y/N)?");
       scanf("%c",&ans);
       
       if(ans == 'Y')
         return main;
    
       else(ans== 'N');
         printf("\nThank You!");
    
       return TotalCost*numP;   
    }
    Last edited by Salem; 02-09-2011 at 01:21 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    change ans to a char (it's a float now).

    Right after the scanf("%c", &answ);

    add this:

    getchar();

    to remove the newline char that scanf() leaves behind on the keyboard stream.
    Code:
    if(ans == 'Y')
    return main;
    is bad code - make it a loop, also (nested, with the one you have now).
    Last edited by Adak; 02-08-2011 at 09:41 PM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Code:
    if(ans == 'Y')
    return main;
    is bad code - make it a loop, also (nested, with the one you have now).
    Hey, calling main.... always a good way to blow up the stack...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > return main;
    This is a function pointer
    What you're doing is returning the address of main, not calling main.
    And since this is a pointer, and main expects to return an int, this is why you're getting a "integer from pointer" error message.

    return main();
    Now this would call main recursively, and as noted would be a bad thing to do.

    If you want a loop, do this
    Code:
    do {
      // your code you want to repeat
    } while ( ans == 'y' );
    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.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Thank you all very much for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 3
    Last Post: 01-14-2002, 12:13 PM

Tags for this Thread