Thread: Simple C programming question when using scanf! :)

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    21

    Cool Simple C programming question when using scanf! :)

    Hey guys, I'm having trouble trying to figure out why the program throws out the steps that I've written it to do, when the user would input the / character when writing in the mm/dd/yyyy. I realized that if I don't put it in, the program works as intended. Does anyone know what is the cause of this, and how to read it together including the (/)? Thanks!

    Heres my code:

    Code:
    #include <stdio.h>
    #define TICKER "LRCX"
    
    
    
    
    //Prototypes
    int getdate(int* BDate, int* SDate);
    float getprice(float* BPrice, float* SPrice);
    
    
    
    
        //MAIN
    int main()
    {
        printf("HELLO THERE!\n");
        printf("Assignment #4\n");
        printf("Code::Blocks\n");
    
    
    //Local Declarations
        int BDate;
        int SDate;
        float BPrice;
        float SPrice;
    //Statements
    getdate(&BDate, &SDate);
    getprice(&BPrice, &SPrice);
    return 0;
    }
    
    
    
    
    
    
    //BUYING - SELLING DATES
        int getdate(char* BDate, int* SDate)
    {
        printf("Enter purchase date as MM/DD/YYYY: ");
        scanf("%c",&BDate);
    
    
        printf("Enter sell date as MM/DD/YYYY: ");
        scanf("%d",&SDate);
    return ;
    }
    
    
    //Asking for buying - selling prices
        float getprice(float* BPrice, float* SPrice)
    {
        printf("Enter purchase price: $");
        scanf("%f",&BPrice);
    
    
        printf("Enter selling price: $");
        scanf("%f",&SPrice);
    return;
    }
    If I enter in the (/) when putting in the date, it just prints out all of the other printf's without giving the user the chance to enter in what is being asked. Heres an example:

    Code:
    
    HELLO THERE!
    Assignment #4
    Code::Blocks
    Enter purchase date as MM/DD/YYYY: 12/12/1987
    Enter sell date as MM/DD/YYYY: Enter purchase price: $Enter selling price: $
    Process returned 0 (0x0)   execution time : 7.088 s
    Press any key to continue.
    But if I don't:
    Code:
    HELLO THERE!
    Assignment #4
    Code::Blocks
    Enter purchase date as MM/DD/YYYY: 12121987
    Enter sell date as MM/DD/YYYY: Enter purchase price: $15
    Enter selling price: $43
    
    
    Process returned 0 (0x0)   execution time : 8.017 s
    Press any key to continue.
    And it works just fine. Thanks for all your help and advice!

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    your date is being entered as an INT, so it is only accepting numbers, after it reads to the / it is entering that into the following scanf`s,

    for your application, i would recommend using a string instead of an int for the date, because your not computing anything with the date hence you dont need it to be a number, just something to display

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    Ah, I see, it makes sense. In our class we haven't been taught strings yet, as I am still a C newbie. I was thinking about manually printf the (/) and then scanf the numbers in, and then combining all the ints into one int. Thanks for your help!

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    hm, when i try to do it, the program would freeze. and now that i look at it, it made a lot more sense in my mind than it did on screen. Heres what its supposed to to look like:
    Enter purchase date (as mm/dd/yy) 01/02/13 <- user input
    Enter sell date (as mm/dd/yy) 01/25/13
    <- user input
    Enter purchase price 8.88 <- user input
    Enter sell price 9.03 <- user input

    Purchase Date: 01/02/13
    Stock: AA
    Amount Invested: $10,000.00
    Purchase Share Price: $8.88
    Shares Purchased: 1126.126

    Sell Date: 01/25/13
    Sell Share Price: $9.03
    Value of Shares Sold: $10,168.92
    Gain: $168.92
    Percent Gain: 1.7%

    Dont mind the other statements, just the date is the one I cannot figure out on my own. Is there another technique besides a string I can use to enter the date in? The requirement that I have is:

    • A function to get a date from user input. The function should return the date as type int.

    Strange, because if int can only hold numbers, how can I include a "/" when I printf the date out?

    So mindblown

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    how can I include a "/" when I printf the date out?
    Code:
    printf("%d/%d/%d", month, day, year);
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    I followed your advice and used the
    Code:
    printf("%d/%d/%d", month, day, year);
    to manually put the"/" the date out, however, when i call on the ints outside the function, its a bunch of numbers indicating I'm not doing something right. I've tried many things to get it to work, but it just doesn't. I apologize for the simple questions but this has been a rough one :/

    Here's what I got:

    Code:
    #include <stdio.h>
    #define TICKER "LRCX"
    #define INVESTMENT_AMOUNT "10,000.00"
    
    
    //Prototypes
    int getdate(int* month1,int* day1,int* year1,int* month2,int* day2,int* year2);
    float getprice(float* BPrice, float* SPrice);
    
    
    
    
        //MAIN
    int main()
    {
        printf("HELLO THERE!\n");
        printf("Assignment #4\n");
        printf("Code::Blocks\n");
    
    
    //Local Declarations
        float BPrice;
        float SPrice;
        int month1, day1, year1, month2, day2, year2;
    
    
    //Starting Functions
        getdate(&month1, &day1, &year1,&month2, &day2, &year2);
        getprice(&BPrice, &SPrice);
        printf("\n");
    
    
        printf("Purchase Date: %d/%d/%d \n", month1, day1, year1); //<-----Doesn't work!
        printf("Stock: " TICKER "\n");
        return 0;
    }//END OF MAIN
    
    
    
    
    //BUYING - SELLING DATES
    int getdate(int* month1,int* day1,int* year1,int* month2,int* day2,int* year2)
    {
    //Local Declarations
    
    
        printf("Enter purchase date as MM/DD/YY: ");
        scanf("%2d/%2d/%2d", &month1,&day1,&year1);
    
    
        printf("Enter the sell date as MM/DD/YY: ");
        scanf("%2d/%2d/%2d",&month2,&day2,&year2);
    return;
    }
    
    
    
    
    //Asking for buying - selling prices
    float getprice(float* BPrice, float* SPrice)
    {
        printf("Enter purchase price: $");
        scanf("%f",&BPrice);
    
    
        printf("Enter selling price: $");
        scanf("%f",&SPrice);
    return;
    }

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You don't need to use the address-of operator (&) in your calls to scanf. The parameters of your getdate and getprice functions are already pointers to their respective types.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    Oh I see, that fixed it, thank you!
    Just to clarify though, do I only need to not include the (&) when I am using the (int*) or do I not need to have it even when I am not using the (*)?
    Thanks for the help, its really appreciated

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Who View Post
    Just to clarify though, do I only need to not include the (&) when I am using the (int*) or do I not need to have it even when I am not using the (*)?
    If you don't use (int *) in your function declaration then it will look like this

    Code:
    int getfoo(int x) {
    // ...
       scanf("%d", &x);
    // ...
    }
    In that case, x needs an ampersand to take the address, thus rendering an int* from an int. If you define your function the way you had it:

    Code:
    int getfoo(int *x) {
    // ...
    scanf("%d", x);
    // ...
    }
    Now x does not need an ampersand because x is already int*. It just happens that scanf needs an int* when you specify a %d, but the best way to know is to look at the reference page for the function in question, in this case scanf.

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The way that I usually explain this is:

    scanf's inputs require an address of a variable

    When you use '&' before a variable, you are getting the "address of" a variable - That is why you would usually need to use it with scanf.

    However, in your functions, your function inputs are a pointers - These are already addresses to variables, so no further action is required.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C programming question
    By Boxo in forum C Programming
    Replies: 3
    Last Post: 03-27-2010, 09:44 PM
  2. Simple Scanf question for beginner
    By somekid413 in forum C Programming
    Replies: 1
    Last Post: 12-15-2009, 04:56 PM
  3. <( ' '<) Simple Programming Question?
    By strigen in forum C Programming
    Replies: 1
    Last Post: 03-05-2009, 03:17 PM
  4. Simple Qt Programming Question
    By Reisswolf in forum Linux Programming
    Replies: 1
    Last Post: 05-23-2006, 01:26 PM
  5. Simple question with scanf()
    By MadStrum! in forum C Programming
    Replies: 3
    Last Post: 01-20-2003, 10:41 AM