Thread: trouble with a for loop with an if statement nested in

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    trouble with a for loop with an if statement nested in

    Hey people i am having trouble with a for loop with an if statement nested in. I have read the data below
    Code:
    Forsyth, Frederick      ;The fist of god               ;A;102391
    Follett, Ken            ;The pillars of the earth      ;A;103795
    Francis, Clare          ;Wolf winter                   ;A;112424
    Smith, Wilbur           ;A falcon flies                ;A;151395
    Forsyth, Frederick      ;The day of the jackal         ;A;159151
    Blackmore, R.D.         ;Lorna Doone                   ;A;193292
    Smith, Wilbur           ;Men of men                    ;A;292351
    Smith, Wilbur           ;The angels weep               ;A;482315
    James, P.D.             ;Cover her face                ;D;144371
    James, P.D.             ;Devices and desires           ;D;193395
    Kellerman, Faye         ;Stone kiss                    ;D;434517
    James, P.D.             ;The skull beneath the skin    ;D;439518
    Kelly, Jim              ;The water clock               ;D;543951
    Hoag, Tami              ;Dark horse                    ;D;664333
    Cookson, Catherine      ;The silent lady               ;R;151611
    Cookson, Catherine      ;Kate hannigans girl           ;R;165163
    Thompson, E.V.          ;The lost years                ;R;165213
    Steel, Danielle         ;The cottage                   ;R;177516
    Binchley, Maeve         ;Quentins                      ;R;261561
    Alcott, Louisa          ;Little women                  ;R;395104
    Cookson, Catherine      ;Rosie of the river            ;R;512843
    Asimov, Isaac           ;Foundation                    ;S;165108
    Barker, Clive           ;Imajica                       ;S;172816
    Barker, Clive           ;The great and secret show     ;S;174427
    Barker, Clive           ;Weaveworld                    ;S;282118
    Clarke, Arthur C        ;2001 A space odessy           ;S;294561
    Asimov, Isaac           ;Foundation and empire         ;S;313178
    Asimov, Isaac           ;Second foundation             ;S;549201
    I read this data into an array as so.
    Code:
      ifstream accfile ("bookdata.txt");
      book b[max];
      int counter = 0;
    do
      {
        getline(accfile, b[counter].secondname, ',');
        getline(accfile, b[counter].firstname, ';');
        getline(accfile, b[counter].title, ';');
        accfile >> b[counter].cat;
        accfile.ignore(1,';');
        accfile >> b[counter].bnumber;
        if (accfile)
          {
            counter++;
          }
      } while (accfile && counter < max);
     accfile.close(); // close file
    Then i used this code here to let the user enter the author surname and display some data about the book
    Code:
     string authorname;
     cin >> authorname;
     for (int as = 0; as == counter; as++)
       {
         if(b[as].secondname==authorname)
           {
            cout << b[as].secondname << b[as].firstname << b[as].title
                 << b[as].cat;
           }
       }
    for some reason it only works if a enter Forsyth (b[0]) but noting else. Any ideas???.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
     for (int as = 0;  as < counter; as++)
    Edit: but then if you use as == counter it shouldn't enter the loop at all unless counter is 0.
    Last edited by ZuK; 12-14-2005 at 03:47 PM.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Ok i have edited the for loop to
    for (int as = 0; as < counter; as++)
    but it is still only displaying an answer when i type Forsyth a.k.a (b[0]) when i enter any other name it won't display anything

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    On input, you need to ignore the newline at the end of each line. Every time through the loop after the first line, there is still a newline character at the end of the previous line. This newline character is getting tacked on to the beginning of the secondname field, so it never matches what you type in later on.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Ok cheers ZuK and Daved (again) the code is working perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if statement Help!
    By C++Noobie in forum C++ Programming
    Replies: 12
    Last Post: 11-10-2006, 11:44 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. Having trouble with an if statement!
    By coloughl in forum C Programming
    Replies: 6
    Last Post: 12-14-2001, 06:18 AM
  4. While statement
    By taurusborn in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2001, 06:22 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM