Thread: dynamic array has junk digits attached to beginning of each element

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    17

    dynamic array has junk digits attached to beginning of each element

    I have been experimenting with dynamic arrays, but have run into a problem with the output. Whenever I read in the numbers from a file, they are correct (output them to the screen to check), but once the movieID gets stored in the array the number "8224" gets appended to the beginning of the number. The output to a small subsection looks like this:

    822430 3
    8224157 3
    8224173 4
    8224175 5

    82248 5
    822428 4
    822430 5
    822483 5

    82241144 4
    82241202 5
    82241428 4
    82241518 4

    Code:
      struct userStruct
      {
        unsigned int length;
        unsigned long userID;
        unsigned int* movieID;
        unsigned short* rating;
      };
    
    
    
     unsigned long nVal = 480000;  //I had to do this for the program to execute
      userStruct* user;
      user = new userStruct[nVal];
    
     
      unsigned int tempLength;
      string line, l1, l2;
      unsigned long tem;
      unsigned int tem1;
      unsigned short tem2;
    
      unsigned long counter = -1;
    
      while (!inFile.eof())
      {
      
        getline(inFile, line);
        if (line.find(':') != string::npos)
        {
          counter++;
    
          getline(inLen, line);
          tem = atoi(line.c_str());
    
          user[counter].movieID = new unsigned int[tem];
          user[counter].rating = new unsigned short[tem];
          user[counter].length = 0;
        }
    
        else
        {
          l1 = line.substr(0, line.find(' '));
          l2 = line.substr(line.find(' ')+1, line.length()-line.find(' ')-1);
    
    
          tem1 = atoi(l1.c_str());
          tem2 = atoi(l2.c_str());
    
          tempLength = user[counter].length;
    
          user[counter].movieID[tempLength] = tem1;
          cout<<endl<<"** "<<tem1;
    
          user[counter].rating[tempLength] = tem2;
          cout<<' '<<tem2;
    
          user[counter].length++;
        }
    
      }
    
       for( int i = 0; i<3; i++)
      {
        cout<<endl;
        for (int j = 0; j< 4; j++)
          cout<<'  '<<user[i].movieID[j]<<' '<<user[i].rating[j]<<endl;
      }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try giving a small but complete sample of code that - when compiled and run - illustrates your concern. Also provide a sample of the input file and describe both the output you get and how how it's different from what you expect.

    The code you have given won't even compile as the code is not in a function.

    It does not pay to paraphrase where you think the problem is as, odds are, you will eliminate something relevant to your actual problem - or introduce some problem unrelated to yourt concern. Forum members are not mind readers, so are unlikely to be able to help if you get things wrong in your problem description.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This:
    Code:
    '  '
    may give you two spaces, but (apparently?) it won't. If you have more than one character, make it a string with "double quotes".

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What compiler are you using? The compiler should give you a warning.

    warning: multi-character character constant
    from GCC at default warning level.

  5. #5
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Quote Originally Posted by tabstop View Post
    This:
    Code:
    '  '
    may give you two spaces, but (apparently?) it won't. If you have more than one character, make it a string with "double quotes".
    Whoa! Good eyes!

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    sorry for that. The code is a little messy, but i've attached it below. Also, i've pasted part of the data file that goes along with it. The actually files are much larger, but this is how they would be set up. I've ran it on these smaller "sample files" and got the same results. Also, I'm compiling it with borland's command line compiler.

    Code:
    void main()
    {
     
      struct userStruct
      {
        unsigned int length;
        unsigned long userID;
        unsigned int* movieID;
        unsigned short* rating;
      };
    
    
    
      unsigned long nVal = 480189;
      userStruct* user;
      user = new userStruct[nVal];
    
      ifstream inFile;
      ifstream inLen;
      inFile.open("sample.dat");
      inLen.open("len.dat");
      
      unsigned int tempLength;
      string line, l1, l2;
      unsigned long tem;
      unsigned int tem1;
      unsigned short tem2;
    
      unsigned long counter = -1;
    
      while (!inFile.eof())
      {
      
        getline(inFile, line);
        if (line.find(':') != string::npos)
        {
          counter++;
    
          tem = atoi(line.c_str());
          user[counter].userID = tem;
    
          getline(inLen, line);
          tem = atoi(line.c_str());
    
          user[counter].movieID = new unsigned int[tem];
          user[counter].rating = new unsigned short[tem];
          user[counter].length = 0;
    
        }
    
        else
        {
          l1 = line.substr(0, line.find(' '));
          l2 = line.substr(line.find(' ')+1, line.length()-line.find(' ')-1);
    
    
          tem1 = atoi(l1.c_str());
          tem2 = atoi(l2.c_str());
    
          tempLength = user[counter].length;
          user[counter].movieID[tempLength] = tem1;
          user[counter].rating[tempLength] = tem2;
          user[counter].length++;
        }
    
      }
    
      inFile.close();
      inLen.close();
    
      
      for( int i = 0; i<3; i++)
      {
        cout<<user[i].length<<' '<<user[i].userID<<endl;
        for (int j = 0; j< 4; j++)
          cout<<'  '<<user[i].movieID[j]<<' '<<user[i].rating[j]<<endl;
      }
    
    }
    This is the sample file's contents:
    Code:
    6:
    30 3
    157 3
    173 4
    175 5
    191 2
    197 3
    241 3
    295 4
    7:
    8 5
    28 4
    30 5
    83 5
    175 5
    185 4
    191 4
    257 5
    273 4
    283 5
    8:
    1144 4
    1202 5
    1428 4
    1518 4
    1719 1
    1799 5
    1843 3
    and this is the file that contains the lengths to make each array (it's the closest thing I could think of to make a 'jagged' array)
    Code:
    8
    10
    7

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    nevermind. I just saw the last post about the double character, but didn't even notice it in the code until now. Now the code runs fine.

    I've recently switched to python, and gotten used to the way you can write strings in it. I know it isn't a good habit to have, but it makes things go quicker then trying to use quotes a lot of the time.

    Sorry for wasting your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  3. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  4. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM