Thread: How to reset a pointer in a loop?

  1. #1
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80

    How to reset a pointer in a loop?

    I'm making this program to check a number, and see if it's valid.

    It is reading the numbers in from a text file, and the loop is made to make it keep taking in the numbers from the text file, until it reaches eof.

    Problem is, the second time it runs through the loop there occur some errors in the part with the pointer 'i'. After the first round, the integers values gets all wrong. When I try to set i = 0, it crashes my compiler.

    Please take a look and let me hear if you know what to do.
    Code:
    int Iday, Imonth, Iyear, Ictrlnr, Igen, test = 0;
    
    char input[12], day[3], month[3], year[3], ctrlnr[4], gen[2];
    
    char* i = input;
    
      ifstream b_file ( "testen.txt" );
    
      while(!b_file.eof())
      {
    
      b_file >> input;
    
      cout << endl << input;
    
      //cout << "Indtast cprnr. i formatet xxxxxx-xxxx ";
      //cin  >> input;
    
      if(input[11]!='\0')
        test = 1;
    
      strncpy(day, i, 2);
      i += 2;
    
      strncpy(month, i, 2);
      i += 2;
    
      strncpy(year, i, 2);
      i += 3;
    
      strncpy(ctrlnr, i, 3);
      i += 3;
    
      strncpy(gen, i, 1);
    
      Iday = atoi(day);
      Imonth = atoi(month);
      Iyear = atoi(year);
      Ictrlnr = atoi(ctrlnr);
      Igen = atoi(gen);
    
      cout << endl << Iday << Imonth << Iyear << "-" << Ictrlnr << Igen;
    
      if(Imonth == 1 || Imonth == 3 || Imonth == 5 || Imonth == 7 || Imonth == 9 || Imonth == 11)
      {
        if(Iday > 31)
          test = 1;
      }
      else if(Imonth == 4 || Imonth == 6 || Imonth == 8 || Imonth == 10 || Imonth == 12)
      {
        if(Iday > 30)
          test = 1;
      }
      else if(Imonth == 2)
      {
        if(Iyear%4 == 0)
        {
          if(Iday > 29)
            test = 1;
        }
        else if(!Iyear%4 == 0)
        {
          if(Iday > 28)
            test = 1;
        }
      }
      else if(Imonth > 12)
        test = 1;
    
      if(Iyear > 99)
        test = 1;
    
      if(Iday > 31)
        test = 1;
    
    
      if(test == 0)
        cout << endl << input <<" is a valid cprnr. ";
      else if(test == 1)
        cout << endl << input << " is not a valid cprnr. ";
      }
      cin  >> test;
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You need to point i back to the beginning of input:
    Code:
      while(!b_file.eof())
      {
        b_file >> input;
        i = input;

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Setting a pointer to zero is bad. Remember that a pointer is nothing more than a memory address with special tools to help you access what is stored at the address. Guess what is stored at memory address 0.

    Keep that in mind and you should be able to figure out a better solution on your own.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You don't assign 0 to i, as i isn't an index it's an address. Instead assign input to i after each read in from file

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It's also better to put your read as the loop's condition:
    Code:
      while(b_file >> input)
      {
        i = input;

  6. #6
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Thanks guys. My knowledge of pointers is very limited. I only just got to the chapter, in my book

    And now it works perfectly Thanks again.
    Last edited by stillwell; 10-18-2004 at 02:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM