Thread: Help With Funny Integer Output

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    16

    Help With Funny Integer Output

    Hi, I'm new to the forum, and really new to C, but I'd like to get better...

    I'm trying to read information from a text file with headers and info as well.

    The header is printing fine, but the info is not. The strings comes out perfectly, but the integers aren't.

    Any help would be appreciated.

    Here's the code:

    Everythings inside of a function and the main only contains the call to those function

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    
    #define  text 200
    #define  inti 200
    
    
    
    
    void printSummary()
    
    
    
    
    int main()
    
    
           {
        printSummary();
        }
    
    
    void printSummary()    {
    
    
            
            
            char month[text]; 
            char date[text]; 
            char day[text];
            char activity[text];
            char repetitions[text];
            FILE *fptr;
            char filename[20];
            int i;
            int dater2[inti];
            int reps[inti];
            
            
            
                printf("Please enter filename [.txt]: ");
                scanf("%s", filename);
    
    
            fptr = fopen(filename,"r");
    
    
            if (fptr != NULL)
    
    
            {
            printf("\n");
            fscanf(fptr,"%s %s %s %s %s", month, date, day, activity, repetitions);
            printf ("%s %s %s %s %s\n", month, date, day, activity, repetitions);
    
    
                for (i=0; i<8; i++)
                    {
                        fscanf(fptr, "%s %d %s %s %d", month, dater2, day, activity, reps);
                        printf("%s %d %s %s %d\n", month, dater2, day, activity, reps);
            
                    }
            }
            else
            {
                printf("File cannot be opened");
            }
            
        }
    Yes, I know the for loop initializes the i, but somehow the compiler gave me an error stating the i wasn't initialized, even though it was in the for loop :S

    Any help would be appreciated, if you need any more info feel free to ask!

    Oh and here's the ouput

    http://img444.imageshack.us/img444/8535/capturell.png

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Are you and him from the same class or something?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Lol, possibly
    Last edited by Spamtacus; 11-26-2011 at 09:23 AM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Check what "fscanf" returns to see if you get all the desired input. If not, it's probably because when you try to get a number you are on a letter and fscanf just moves on to the next input argument ( leaving the number's contents unchanged ).
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Well like I said before, everything BUT the integers (the date and numbers), are showing up properly.

    I've checked and can't seem to find anything....

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Spamtacus View Post
    Well like I said before, everything BUT the integers (the date and numbers), are showing up properly.

    I've checked and can't seem to find anything....
    Look up scanf() and printf() in your C documentation...

    I think you'll find that scanf() needs pointers... so for %d conversions you need &variable.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    Look up scanf() and printf() in your C documentation...

    I think you'll find that scanf() needs pointers... so for %d conversions you need &variable.
    Yep, I've fixed that in the code but it's still a no go...the integers are still borked..

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Spamtacus View Post
    Yep, I've fixed that in the code but it's still a no go...the integers are still borked..
    Post your current code, with the changes you made...
    Also the content of that data file, please.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    
    
    
    #define  text 200
    #define  inti 200
    
    
    
    
    void printSummary();//prints summary to text file
    //list of all function prototypes go here
    
    
    int main()
    
    
    {
        printSummary();
    
    }
    
    
        void printSummary()//Purpose: Writes summary into text file
        {
    
    
            
           // prints row one//
            char month[text]; 
            char date[text]; 
            char day[text];
            char activity[text];
            char repetitions[text];
            FILE *fptr;
            char filename[20];
            //print row Two
            // in row date and reps are integers
            int i;
            int dater2[inti];
            int reps[inti];
            
            
            
                printf("Please enter filename [.txt]: ");
                scanf("%s", filename);
    
    
            fptr = fopen(filename,"r");
    
    
            if (fptr != NULL)
    
    
            {
            printf("\n");
            fscanf(fptr,"%s %s %s %s %s", month, date, day, activity, repetitions);
            printf ("%s %s %s %s %s\n", month, date, day, activity, repetitions);
    
    
                for (i=0; i<8; i++)
                    {
                        fscanf(fptr, "%s %d %s %s %d", month, &dater2, day, activity, &reps);
                        printf("%s %d %s %s %d\n", month, dater2, day, activity, reps);//[i] suppose to go there but gets error when running the program
            
                    }
            }
            else
            {
                printf("File cannot be opened");
            }
            
        }
    This is the output

    http://img27.imageshack.us/img27/9017/capturetyt.png

    new.txt << file
    Last edited by Spamtacus; 11-26-2011 at 10:10 AM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I don't want to see your output... I need to see the file you are reading in...

    Why do you have an all string read at line 60? Is there a header on the file or something?

    You also have a function named print_summary() that is actually reading your disk file... I'd suggest renaming it to something more indicative of it's function.

    If you intend to do anything with the data you get from that file, you need to recreate the variables in main...

    For that matter, if your code does this only once, it's a poor candidate for a function in the first place. Moving your entire program out of main into a function is pretty silly...

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Yes there's a header (which reads fine), the file is attached in the previous post, and the function is going to be doing more than that once I can get the integers to read properly...and ok, I'll move it to main.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Spamtacus View Post
    Yes there's a header (which reads fine), the file is attached in the previous post, and the function is going to be doing more than that once I can get the integers to read properly...and ok, I'll move it to main.
    The reason your integers are not printing properly is you are attempting to treat an array as a single integer. Either load to an array index or make the variable a single integer... I'd do the latter until you get the load file working. You can always re-edit to work on arrays later.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Can you give a sample code showing this? It's still a bit unclear...

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Spamtacus View Post
    Can you give a sample code showing this? It's still a bit unclear...
    If you will stop clicking like on everything I say...

    Code:
    int var[10];
    
    // wrong
    scanf("%d", var);
    
    // wrong
    scanf("%d", &var);
    
    // right
    scanf("%d", &var[0]);
    Code:
    int var;
    
    // wrong
    scanf("%d", var);
    
    // wrong
    scanf("%d", &var[1]);
    
    // right
    scanf("%d", &var);
    Last edited by CommonTater; 11-26-2011 at 10:43 AM.

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Thanks man, it works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting the strtok output to integer
    By gabrielksa in forum C Programming
    Replies: 3
    Last Post: 01-28-2011, 10:21 AM
  2. Maximum integer length output?
    By RyanLeonard in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 01:22 PM
  3. Using putchar to output an integer with more than 1 digit
    By RandomEvil in forum C Programming
    Replies: 2
    Last Post: 10-14-2009, 10:23 PM
  4. Using putchar to output an integer with more than 1 digit
    By RandomEvil in forum C Programming
    Replies: 1
    Last Post: 10-14-2009, 10:20 PM
  5. Funny output on hex value of number
    By shav in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 08:19 AM