sscanf causing problems

This is a discussion on sscanf causing problems within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to use sscanf in a program. It compiles fine, but it is causing the run to ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    sscanf causing problems

    Hello,
    I am trying to use sscanf in a program. It compiles fine, but it is causing the run to hang up everytime.
    This is the code that does it. If I comment it out, the program runs fine without it, just doesn't function, of course.
    Code:
    sscanf((chrString),"%c%c%c", 
              &gObj->vertices[count].xCoord,	
             &gObj->vertices[count].yCoord,
             &gObj->vertices[count].zCoord);
    Any Idea why this would be?

    Stephen
    Last edited by takamineg240; 09-24-2006 at 05:48 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    scanf((chrString),"%c%c%c", 
              gObj->vertices[count].xCoord,
             &gObj->vertices[count].yCoord,
             &gObj->vertices[count].zCoord);
    Missing an ampersand?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Caught that just after posting it. Edited in original post. It didn't fix the problem.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Okay, then you'll have to be more specific.

    >but it is causing the run to hang up everytime
    Describe this more fully. Does the input request never seem to return? Is there a runtime error? Can you post a complete example program that exhibits the problem?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    What happens is the program runs until it hits this line, then stops running. Windows throws an error, and then the console window stops responding. I've put ouput lines directly after this one, and they never execute.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Are x, y, and z coords characters, integers, floats, doubles or what? What doyou enter to stop it working?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Windows throws an error
    Okay, now we're getting somewhere. That's probably an access violation. First, print out the chrString string and post the contents here. Then show us how Obj and any of it's dependent types are defined.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    cout << chrString; yeilds -10 30 0

    Code:
    struct vertex  //Defines a linked list node to hold a vertex
     {
       int    xCoord,
              yCoord,
              zCoord;
       vertex *next;   
     };
     vertex *start_ptr = NULL;
     
     
    struct Group
    {
    	vertex   *vertices;
    	Group *next;
    };
    This appears in the method where the sscanf call is: Group *gObj=NULL;
    Last edited by Dave_Sinkula; 09-24-2006 at 10:59 PM. Reason: Fixed [code][/code] tags.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >cout << chrString; yeilds -10 30 0
    Okay, "%c%c%c" is the incorrect format string. You want "%d%d%d".

    >Group *gObj=NULL;
    That's probably your problem. gObj probably doesn't refer to anything before you try to read data into it. Well, it does refer to something, but you can't predict it and 9 times out of 10 writing to it will break something. Are you doing any memory management with these uninitialized pointers?

    > int xCoord,
    >yCoord,
    >zCoord;
    That's dangerous. When you tell sscanf you're giving it a char, you'd be wise to give it a char and only a char. Since these coords are integers, you need to use %d instead of %c.

    >vertex *vertices;
    This likely has the same problem as gObj.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    OK, I fixed the %d thing. I just wasn't paying attention there, I had copied that from another program.

    Since I have not used C++ in a few years, and then only a bit, I am unsure of how I need to proceed with the pointers. I have used Java extensively, but am having to use C++ for a class. So, I am trying to teach it to myself and use it at the same time.

    Basically, I am trying to create a program that loads an *.obj file and renders it using openGL. For my class, I am writing the loader myself, but have used a prewritten one as a guide. It used a linked list to hold vertices, normals, etc. I am trying to do the same (after a failed attempt at using nested vectors).

    Thanks so much for your patient help.

  11. #11
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Why are you using sscanf in a C++ program, instead of >> ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf matching problems
    By KBriggs in forum C Programming
    Replies: 10
    Last Post: 06-23-2009, 02:20 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. sscanf problems ...
    By pic-o-matic in forum C Programming
    Replies: 5
    Last Post: 07-30-2008, 02:23 PM
  4. strlen causing my program to freeze
    By Beowolf in forum C Programming
    Replies: 2
    Last Post: 09-11-2007, 04:09 PM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 03:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21