Thread: Mysterious number is being printed.

  1. #1
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108

    Mysterious number is being printed.

    I'm writing this piece of code in my program that gets the second line in a file that says "check no" and that is it, but when I ran my program it displayed a two. Nowhere in my code do i ask for a number to be inputted. What am i doing wrong?
    Code:
    fprintf(stdout, CHECK);
    fprintf(stdout, CHECKBORDER, number);
    fprintf(stdout,CHECKFORMAT);
    fclose(reportFILE);
    Mysterious number is being printed.-screenshot-2013-05-16-23-09-04-jpg
    Last edited by arti; 05-17-2013 at 12:10 AM. Reason: need to add image

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You do not show what is CHECK, CHECKBORDER, number and CHECKFORMAT, so all I can say is that there is a bug in your code
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by laserlight View Post
    You do not show what is CHECK, CHECKBORDER, number and CHECKFORMAT, so all I can say is that there is a bug in your code
    Sorry here are the definitions of what those defines are:
    Code:
    #define CHECK         "************************************************************\n"
    #define CHECKBORDER   "*                                    Check No. %3d         *\n"
    #define CHECKFORMAT   "*                                                          *\n"

  4. #4
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    They are defines that make up the description and formatting of the check I am supposed to write with my program

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    According to your image, there is no two in the output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by laserlight View Post
    According to your image, there is no two in the output.
    Code:
     int number=0;
    The two was showing up before I initialized the variable number. See I thought the problem was that i was not initialzing the variable number that corresponds to number that is being printed, but that is not the case. I don't know what is going on. After initializing the program started to print 0 :/
    Last edited by arti; 05-17-2013 at 12:19 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by laserlight View Post
    Post the smallest and simplest compilable program that demonstrates the problem.
    Okay I believe that this would be the shortest code that would compile.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    void printReport( FILE **inputFILE);
    
    #define CHECK         "************************************************************\n"
    #define CHECKBORDER   "*                                    Check No. %3d         *\n"
    #define CHECKFORMAT   "*                                                          *\n"
    
     
    int number;
    
    FILE * inputFILE;
    printReport(&inputFILE);
    
    fprintf(stdout, CHECK);
    fprintf(stdout, CHECKBORDER, number);
    fprintf(stdout,CHECKFORMAT);
    fclose(inputFILE);
    
    
    
    
    void printReport( FILE **inputFILE)
    {
      *inputFILE=fopen("./check.txt","r");
      if(*inputFILE==NULL)
      { 
        printf("File cannot be accessed");
        exit(-80);
      }
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where's the main function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by laserlight View Post
    Where's the main function?
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    void printReport( FILE **inputFILE);
    
    #define CHECK         "************************************************************\n"
    #define CHECKBORDER   "*                                    Check No. %3d         *\n"
    #define CHECKFORMAT   "*                                                          *\n"
    
    int main(void)
    {
       int number;
    
       FILE * inputFILE;
       printReport(&inputFILE);
    
       fprintf(stdout, CHECK);
       fprintf(stdout, CHECKBORDER, number);
       fprintf(stdout,CHECKFORMAT);
       fclose(inputFILE);
    }
    
    
    
    
    void printReport( FILE **inputFILE)
    {
      *inputFILE=fopen("./check.txt","r");
      if(*inputFILE==NULL)
      { 
        printf("File cannot be accessed");
        exit(-80);
      }
    }

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You have a warning:

    Code:
    C:\Users\Josh2\Documents\sandboxc\main.c||In function 'main':|
    C:\Users\Josh2\Documents\sandboxc\main.c|18|warning: 'number' is used uninitialized in this function [-Wuninitialized]|
    ||=== Build finished: 0 errors, 1 warnings (0 minutes, 0 seconds) ===|
    Mind you I had to wrap lines 11 - 20 in a main function to get it to compile.

  12. #12
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by whiteflags View Post
    You have a warning:

    Code:
    C:\Users\Josh2\Documents\sandboxc\main.c||In function 'main':|
    C:\Users\Josh2\Documents\sandboxc\main.c|18|warning: 'number' is used uninitialized in this function [-Wuninitialized]|
    ||=== Build finished: 0 errors, 1 warnings (0 minutes, 0 seconds) ===|
    Mind you I had to wrap lines 11 - 20 in a main function to get it to compile.
    I didn't get that warning, but at first it printed 2 then I thought it was because i hadn't initialized it so I did then it printed 0 after i initialized it to 0.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I didn't get that warning,
    That's a problem. I have my warnings set to the highest possible level off complaint. I ask my compiler to complain about nearly everything it can. Doing this helps me explain problems like yours. Watch.

    but at first it printed 2 then I thought it was because i hadn't initialized it so I did then it printed 0 after i initialized it to 0.
    Yeah using variables before they are set to a value has unpredictable results. These unpredictable results are called undefined behavior, and variables that haven't been set a value are uninitialized. Local variables will not be automatically initialized in C.

    Part of the reason C99 made it possible to define variables in mixed code is because in older standards, declaring variables bare like you have done was common and unfortunately forgetting to set a value was also a common mistake. Payign attention to warnings is one way to fix it. The other way to fix it was to declare locals as close to where they are used as possible. Which again C99 made possible. I mention C99 because you should be learning it, and in my experience, people learn it somewhat secretly. They aren't told that // comments are a C99 feature for example. Heaven help them if they also compile C as C++.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, so what's the problem now? Printing 0 looks correct if you initialise number to 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by laserlight View Post
    Okay, so what's the problem now? Printing 0 looks correct if you initialise number to 0.
    I thought it wasn't supposed to print 0 if you only initialize it. Also, won't that create a problem in the future for me because the number that is supposed to go in there is 100 then increment to 105.


    I'm over thinking everything aren't I?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mysterious seg fault!
    By MK27 in forum C++ Programming
    Replies: 12
    Last Post: 03-12-2010, 02:29 AM
  2. Getting a bmp copy of what i printed
    By knutso in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2007, 05:29 AM
  3. int wrong value being printed?
    By Axel in forum C Programming
    Replies: 11
    Last Post: 10-14-2006, 01:37 AM
  4. Getting number of pages printed
    By Yasir_Malik in forum Linux Programming
    Replies: 2
    Last Post: 09-21-2004, 02:59 PM
  5. mysterious code.......(??)
    By deltabird in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 03:58 PM