Thread: Help!

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Help!

    Hey!

    I now im pretty newbie but i have a problem in my code

    Code:
    void search(void)
    {
    char *p, *n, line[5000], name[64];
    int a = 1;
    p = line;
    n = name;
    cout << "\nWrite the name on the person you want to view." << endl;
    cin >> name;
    clrscr();
    ifstream input_fil("person.dat", ios::in);
    if (input_fil.fail())
    {
    cerr << "The file couldnt be opend!\n" << endl;
    a = 0;
    }
    input_fil >> line;
    if (a)
    {
    while (a)
    {
    if (*p == *n)
    {
    cout << *p;
    while (*p != '|')
    {
    cout << *p;
    p = p++;
    }
    a = 0;
    }
    else
    p = p++;
    }
    }
    input_fil.close();
    }
    This is suposed to open the file "person.dat" in the array "line", and then write everything to the screen, from the name the user type, to the end icon '|'.

    But it dont?!

    Plz help me!

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try formatting your code better...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    void search(void)
    {
      char *p, *n, line[5000], name[64];
      int a = 1;
      p = line;
      n = name;
      cout << "\nWrite the name on the person you want to view." 
              <<    endl;
      cin >> name;
      cout << name << endl;
      clrscr();
      ifstream input_fil("person.dat", ios::in);
      if (input_fil.fail())
      //try if(!input_fil) instead of if(input_fil.fail())
      {
         cerr << "The file couldnt be opend!\n" << endl;
         a = 0;
      }
      cout << "a = " <<  a  << endl;
      input_fil >> line;
      cout << "line = " << line << endl;
      if (a)
      {
         while (a)
         {
            cout << "*p = " << *p << endl;
            cout << "*n = " << *n << endl;
            if (*p == *n)
           {
              cout << *p;
              while (*p != '|')
              {
                 p = p++;
                 cout << *p;
                 //switched the above two lines so *p not displayed twice
              }
              a = 0;
          }
          else
            p = p++;
        }
      }
      input_fil.close();
    }
    The above is indented so the function reads easier. It also has a series of cout statements to help debug the function and I switched two lines to get the response I think you want. Now run the program and see where the values of a, p, and n fail to meet your expectations.

Popular pages Recent additions subscribe to a feed