Thread: Fraction Input with Scanf

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    Fraction Input with Scanf

    Hey fellers. I'm brand spanking new to the forums and I wanted to introduce myself with my first post. So honestly, I'm working on my homework, and the problem is I'm looking to input a fraction like so "5 2/3". Now I figured I would use a pointer to a char, from there I would tokenize this string for the space and the forward slash. The current problem is that I use scanf, with that pointer yet my program crashes...I'm calling scanf like so:


    char stockTicker[6+1];
    char *pFraction = NULL;
    float pricePerShare,
    sharesPurchased,
    stockValue;
    int whole,
    num,
    dem, i = 0;
    char slash;

    printf( " Enter the stocker symbol: " );
    scanf( "%s", &stockTicker );

    printf( "\n Enter the price per share of %s: ", stockTicker );
    scanf( "%s", &pFraction ); //Doesn't crash here

    printf( "%s", *pFraction ); //Actually its crashing here. ???


    I would prefer it if the solution doesn't have any function calls to another library. Oh and I just read the homework topic, I'm not intending for you all to do my work...the thing is...I have the solution but its in c++, all it required was using cin....*sigh*
    Last edited by daniellopez; 01-29-2011 at 11:04 PM. Reason: Missed something

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can't read into an uninitialised pointer. pFraction needs to be an actual array of characters just like stockTicker.
    Don't ignore your compiler warnings either. They are trying to point out mistakes.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  2. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  3. Reading input with scanf
    By dat in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 03:54 PM
  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. help with input and scanf
    By kpw in forum C Programming
    Replies: 9
    Last Post: 08-28-2001, 08:21 PM

Tags for this Thread