Thread: Help me debug following codes

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    Help me debug following codes

    Hi folks,
    Thanks to some guys replying to my post this morning and I'm able to finish the codes. However, I don't know why the codes don't work. My goal is pretty humble: read an ASCII data file like the following:
    Code:
    1 1 1 1 1 1 1 1 1
    2 2 2 2 2 2 2 2 2
    ....
    into a vector object defined by me for quantitative modelling purposes.

    The main function is:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cmath>
    #include <vector>
    #include "head.h"
    
    using namespace std;
    
    int main()
    {
    //define the vector corresponding to the model I
    vector<struct observation_model_1> observations;
    
    //read the data into this vector by calling the function
    //readdata_model_1
    
    readdata_model_1("datafile", observations);
    
    checkdata_model_1(observations);
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    The "head.h" includes the function prototypes:
    Code:
    struct observation_model_1 {
                
                 static const int dimofX=8;
                 //Notice that the dataset has 9 numbers for any row             
                 
                 double X[dimofX];
                 //This will take the 2th-9th numbers of one row
                
                  double Y;
                 //This will take the first number of one row
                        
          };
          
    void readdata_model_1(char *filename, vector<struct observation_model_1> &observations);           
    //read the data into a vector with its pointer passed on 
          
    void checkdata_model_1(vector<struct observation_model_1> &observations);
    And these two functions are defined in "function.cpp"
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include "head.h"
    
    void readdata_model_1(char *filename, vector<struct observation_model_1> &observations) {
         
         struct observation_model_1 temp;
         
         ifstream readfile(filename);
         
         if (! readfile) {
               cout<<"File ("<<filename;
               cout<<") could not be opened."<<endl;
               exit(0);
               }
               
         while(! readfile.eof()){
                 
                 readfile>>temp.Y;
         //read in the first number of one row in the data file;
         
                 for (int i=0; i++; i<temp.dimofX) {
                 readfile>>temp.X[i];
                 }
         //read in the 2th-9th numbers of one row in the datafile;
                 
                 observations.push_back(temp);
         //push into the vector observations defined in the main file;        
                              }
                         
                          
            readfile.close();
            
         }
    
    /*****This file is to check the correctness in data reading*******************/
    /*****************************************************************************/
    //It prints out the first, third and last row of the data readed on
    //on the screen for checking
    
    void checkdata_model_1(vector<struct observation_model_1> &observations) {
         
    cout<<"The number of observations in the dataset is: "<<observations.size()<<endl;
    cout<<endl;
    
    cout<<"The 1st row is: "<<endl;
    cout<<observations[0].Y<<endl;
    for (int i=0;i++;i<observations[0].dimofX) {
    cout<<observations[0].X[i]<<endl;
    }
    cout<<endl;
    
    cout<<"The 3rd row is: "<<endl;
    cout<<observations[2].Y<<endl;
    for (int i=0;i++;i<observations[2].dimofX) {
    cout<<observations[2].X[i]<<endl;
    }
    cout<<endl;
    
    cout<<"The last row is: "<<endl;
    cout<<observations[observations.size()-1].Y<<endl;
    for (int i=0;i++;i<observations[observations.size()-1].dimofX) {
    cout<<observations[observations.size()-1].X[i]<<endl;
    }
    cout<<endl;
    
    }
    Unfortunately when I checked the data, X[.] can never be printed out. What is printed on the screen is that:
    Code:
    The 1st row is:
    1
    The 3nd row is:
    3
    The last row is:
    1000
    Press any key to continue...
    Any thoughts about this? I will appreciate it as I'm in the midst of a computing project as you can tell. Thanks a lot in advance...
    Last edited by asmileguo; 09-08-2006 at 09:47 AM.

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    what is your definition of "DP y?"

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (int i=0; i++; i<temp.dimofX)
    Well this for loop is the wrong way round
    i++ is going to be false right from the start,

    > while(! readfile.eof())
    Read the FAQ on why using feof() to control a loop is bad.

    Oh, and your indentation sucks.
    I would suggest you turn OFF tabs in your editor, and set it to always indent with just 2 or 4 spaces (your choice). Then at least what you see will be what everyone else sees.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    struct observation_model_1 temp;
    In C++ you can leave out "class" and "struct" when you're declaring an instance of a class/struct, but you can leave it in too if you like.

    All your for loops should be
    Code:
    for (int i=0; i++; i<temp.dimofX) {
    ->
    Code:
    for (int i=0; i<temp.dimofX; ++i) {
    Remember,
    Code:
    for
    The third and last looping construct in C is the for loop. The
    for loop can execute a block of code for a fixed or given number
    of times. Its syntax is as follows.
    
        for (initializations;test conditions;increment value)
        {
            block of code
        }
    > while(! readfile.eof())
    Read the FAQ on why using feof() to control a loop is bad.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    Gee; I made such a stupid mistake in the looping! I can't believe it; obviously the cold these days destroy my brain...

    Thanks a lot!

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    Hi can I turn OFF tabs in Dev-C++? That's the editor I'm using...

    Quote Originally Posted by Salem
    > for (int i=0; i++; i<temp.dimofX)
    Well this for loop is the wrong way round
    i++ is going to be false right from the start,

    > while(! readfile.eof())
    Read the FAQ on why using feof() to control a loop is bad.

    Oh, and your indentation sucks.
    I would suggest you turn OFF tabs in your editor, and set it to always indent with just 2 or 4 spaces (your choice). Then at least what you see will be what everyone else sees.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    BTW, in above codes, I find I can't access dimofX as defined in the structure (even for print out), and got an error message:
    [linker error]undefined reference to `observation_model_1::dimofX'.
    How to fix it?
    Last edited by asmileguo; 09-08-2006 at 10:47 AM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Static class members must be defined once somewhere in a source file. Add const int observation_model_1::dimofX; to your cpp file.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Salem, he just didn't use tabs at all, so you can't say it's the problem of tabs. Still, using tabs is the cleanest way to write code (that's what you learn when you start with HTML, too).

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, you're right, there are no tabs in the post.
    Code:
    >             readfile>>temp.Y;
    >     //read in the first number of one row in the data file;
    It's just crap indentation - nothing more, nothing less.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I always use tabs in Dev-C++. What's wrong with tabs? It's when you start mixing tabs with spaces then indentation gets out of hand.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I always use tabs in Dev-C++. What's wrong with tabs?
    When you start working in project teams, or downloading source packages from the net you'll soon come across files which are indented using tabs, but with a different tab-stop setting to the one you like. Such files always look like a mess until you guess the correct tab-stop setting.

    At one place I worked, it was so bad I worked out a set of editor macros to quickly rotate through all the editor tab-stop settings just so I could look at the code in that file without going crazy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In DevC++ go to: "Tools>Editor Options" and uncheck "Smart tabs". This is no good and just produces awful indentation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM