Thread: Float is reading 0 regardless of input

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Pittsburgh, Pennsylvania, United States
    Posts
    3

    Float is reading 0 regardless of input

    With the below program, whenever I enter an amount for bookcost, it reads 0. I've tried a few different things but I cannot seem to get it to use my input.
    Around line 49 is where I ask for user input.
    Any thoughts would be aweosme.


    Code:
    //Tschippert
    //September 21st-22nd, 2012
    
    #include <stdio.h>
    
    int main ()
    {
        // Defining Integers//
        //
        
        int bookproduct;
        int bookcount;
        float bookcost;
        float bookcost2;
        
        int totalbookcount;
        float totalcost;
        
        unsigned int counter;
        unsigned int looprun;
    
        
       
        // Initializing Integers//
        //
        
        counter=0;
        totalbookcount=0;
        totalcost=0;
        bookcount=0;
        
        //Begininng Loop//
        //
        
        printf("Would you like to enter a book? 1 for yes, 2 for no.");
        scanf(" %d", &looprun);
        
        while (looprun !=2) {
        
        counter++;
        
        printf("\nEnter the Book Product Number:\n");
        scanf(" %d", &bookproduct);
        
        printf("\nEnter the Number of Books Purchased: (Negative if you are returning)\n");  
        scanf(" %d", &bookcount);
        
        printf("\nEnter the Cost of the Book Purchased:\n");
        scanf(" %.2f", &bookcost);
        while(getchar() != '\n');                                                                           //Fix for input stream//
        
        bookcost2= (float) bookcost*bookcount;
        
        printf("\nYou entered %d of product %d for %.2f.\n", bookcount, bookproduct, bookcost2);            //Displays what was entered//
        printf("\nWould you like to add another book? 1 for yes, 2 for no.\n");                             //Asks if user wants to continue//
        scanf(" %d", &looprun);                                                                             //Changes value of looprun to with continue the loop or end the loop//
        
        totalbookcount = totalbookcount+bookcount;                                                          //Sums up the entered information for a final display.//
        totalcost = (float) (totalcost+bookcost2);
             
    
        
           
        }
        
        if (counter>0) {
                        
        printf("\nNumber of records entered: %d", counter);
        printf("\nTotal Number of Books: %d", totalbookcount);
        printf("\nTotal Cost of Books: %.2f", totalcost);
             
        }
        
        else {
             
        printf("You did not enter any books!");
        
        }
           
        
        
        //Totaling inputed values//
        //
        
    
        //Reiterating entered information//
        //
        
        
        //Counter for number of runs
        
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I would switch this
    Code:
     scanf(" %.2f", &bookcost);
    into this
    Code:
     scanf(" %f", &bookcost);

  3. #3
    Registered User
    Join Date
    Sep 2012
    Location
    Pittsburgh, Pennsylvania, United States
    Posts
    3
    Works! Thanks,

    I was looking at that but never thought about removing it for fear of someone entering more than two decimals. However, only the output needs to be truncated correct?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by MTSchippert View Post
    Works! Thanks,

    I was looking at that but never thought about removing it for fear of someone entering more than two decimals. However, only the output needs to be truncated correct?
    That is correct. If you want to be stringent about input for whatever reason, you should convert a string that is valid (such as "1.00") to a float, and reject the string otherwise. Precision is for output only.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well i am not sure if understood the question(maybe my English are not good enough).Or say it in other words,or give me an example or wait for the other users to answer .

    Also,welcome to the forum

    EDIT->already answered

  6. #6
    Registered User
    Join Date
    Sep 2012
    Location
    Pittsburgh, Pennsylvania, United States
    Posts
    3
    Thanks. I will probably be using it often haha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. check if input is int float or char
    By ankiit in forum C Programming
    Replies: 11
    Last Post: 02-07-2012, 01:56 PM
  2. Replies: 5
    Last Post: 10-28-2009, 03:10 PM
  3. float input
    By Asimghouri in forum C Programming
    Replies: 3
    Last Post: 01-13-2009, 03:06 PM
  4. reading float from file
    By arjunajay in forum C++ Programming
    Replies: 10
    Last Post: 07-30-2005, 11:01 PM
  5. reading a float from file
    By jamie in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2003, 09:27 PM