+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Junior Member
    Join Date
    Sep 2001
    Location
    NYC
    Posts
    8

    >>> C++ problem!

    For school I need to make this program that reeds in a file... and processes it
    <B>everything works fine..</B>
    except for one detail where I get strange #s (looks like address locations instead of regular numbers)

    Code:
    int currentLine=0;
    while ( fscanf( reader, "%s %s %s %d %d %d %d %d", &item[currentLine].code, &item[currentLine].name, &item[currentLine].category, &item[currentLine].location[0], &item[currentLine].location[1], &item[currentLine].location[2], &item[currentLine].location[3], &item[currentLine].location[4]) != EOF ){
    			printf( " %s \t%s \t\t%s \t%d \t%d \t%d \t%d \t%d\n", &item[currentLine].code, &item[currentLine].name, &item[currentLine].category, &item[currentLine].location[0], &item[currentLine].location[1], &item[currentLine].location[2], &item[currentLine].location[3], &item[currentLine].location[4]);
    			currentLine++;
    }
    (sorry for the page width the code has made)
    see where I have &item[currentLine].location[3] on read line,
    and then I print using the same statement and I get 4 digit numbers instead of what I am reading in!

    I attached the C++ file and source I am reading, could you compile and run (it's all comple fine and ready (no erroros)) to see if you get the same result, or what could be the problem? My OS? My Turbo C++?

    C SOURCE FILE: http://www.dslreports.com/r0/downloa...356/Dev1a2.zip
    READ FILE: http://www.dslreports.com/r0/downloa...94/invorig.zip
    I am sorry for .zip format but each .zip has only one file...
    Just place both into same directory and run...

    ]ld you please help me fix this!
    Where could I post such request on what boards?
    Thank you all...

  2. #2
    Member strangerstill's Avatar
    Join Date
    Sep 2001
    Location
    Oxford
    Posts
    203
    Some time since I did any C++, but...

    When printf reads a %d, it expects the argument to be passed by value (sorry if I sound like a vb programmer, its what I use day-to-day)

    So when you pass printf &item[currentLine].location[3] it reads the address of item[currentLine].location[3] and writes that into the text stream.

    Solution: for the %d arguments remove the initial & so that you pass the value of that argument, not the address.

    Of course, you need to leave the & in when you're calling scanf.

    Incidentally, I seem to have lost the Book; is there a way to tell printf that its being passed an int * rather than an int? Something like %*d maybe? That would be the other (rather convoluted) solution.

  3. #3
    Junior Member
    Join Date
    Sep 2001
    Location
    NYC
    Posts
    8
    Yep, great I did it....
    thank you...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts







New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke