problem appending

This is a discussion on problem appending within the C++ Programming forums, part of the General Programming Boards category; Near the top of my code I have Code: ifstream accfile; And I have been using accfile to open, read ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    66

    problem appending

    Near the top of my code I have
    Code:
    ifstream accfile;
    And I have been using accfile to open, read and close files several times in the code. However, I am not receiving an error with the following code
    Code:
               else
                 {
                   accfile.open ("loans.txt");
                   accfile << name << ";";
                 }
             }
           cout << "enter the ID number of the book." << endl;
           string book;
           accfile << book << endl;
           accfile.close();
           break;
         case 3:
    My errors (line 179 - accfile << name << ";"

    testfile.cc:179: no match for `ifstream & << string &'
    /usr/local/depot/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/std/bastring.cc:469: candidates are: class ostream & operator <<<char, string_char_traits<char>, __default_alloc_template<false,0> >(ostream &, const basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> > &)
    testfile.cc:184: no match for `ifstream & << string &'
    /usr/local/depot/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/std/bastring.cc:469: candidates are: class ostream & operator <<<char, string_char_traits<char>, __default_alloc_template<false,0> >(ostream &, const basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> > &)
    testfile.cc:186: break statement not within loop or switch
    testfile.cc:187: case label `3' not within a switch statement
    testfile.cc:183: warning: destructor needed for `class string book'
    testfile.cc:187: warning: where case label appears here
    testfile.cc:187: warning: (enclose actions of previous case statements requiring
    testfile.cc:187: warning: destructors in their own binding contours.)
    testfile.cc:187: confused by earlier errors, bailing out

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I suggest that you post the smallest and simplest program that demonstrates the problem. The snippet you gave is a little too incomplete.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    But shouldn't this (below) work on it's own anyway?
    Code:
    ifstream accfile;
    accfile.open ("loans.txt");
    string book;
    cin >> book;
    accfile << book << endl;
    accfile.close();
    Sorry, I had forgotten to write cin >> book before, but I still get the same error.

    The reason I'm trying not to put large amounts of code here is because this code is for a coursework and I'm not sure how the system they use works, but I don't want to have to explain afterwards that I haven't plagerised anything.
    Last edited by Furious5k; 01-05-2009 at 10:36 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Furious5k
    But shouldn't this (below) work on it's own anyway?
    Yes, but in your case it is not on its own.

    Quote Originally Posted by Furious5k
    The reason I'm trying not to put large amounts of code here is because this code is for a coursework and I'm not sure how the system they use works, but I don't want to have to explain afterwards that I haven't plagerised anything.
    That's another reason why your should narrow down the code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Read the compiler errors:

    testfile.cc:186: break statement not within loop or switch
    testfile.cc:187: case label `3' not within a switch statement
    These two errors clearly say that the switch has ended earlier. Check your braces.

    string book;
    If this were in a switch case, the block using book would need to have braces around it (you can't declare variables within a switch unless they are in braces, since the scope of a variable declared in one case would be otherwise till the end of the switch, meaning that execution might reach a place where a variable is in scope, yet jump over the declaration / constructor call).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Would this do?

    Code:
       int select;
       cin >> select;
    
       switch (select)
         {
         case 1:
           //code here
           break;
         case 2:
           cout << "message here." << endl;
           string name;
           cin >> name;
    
           int loancount;
           for (int i = 0; i < (BOOK_MAX+1); ++i)
             {
               if (name == Loans[i].User)
               {
                 ++loancount;
               }
             }
    
               if (loancount > 3)
                 {
                   cout << "message here." << endl;
                 }
               else
                 {
                   accfile.open ("loans.txt");
                   accfile << name << ";";
                 }
             }
           cout << "message here." << endl;
           string book;
           cin >> book;
           accfile << book << endl;
           accfile.close();
           break;
         case 3:
           //code here
           break;

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Indent your code properly, e.g.,
    Code:
    int select;
    cin >> select;
    
    switch (select)
    {
    case 1:
        //code here
        break;
    case 2:
        cout << "message here." << endl;
        string name;
        cin >> name;
    
        int loancount;
        for (int i = 0; i < (BOOK_MAX+1); ++i)
        {
            if (name == Loans[i].User)
            {
                ++loancount;
            }
        }
    
        if (loancount > 3)
        {
            cout << "message here." << endl;
        }
        else
        {
            accfile.open ("loans.txt");
            accfile << name << ";";
        }
    }
    cout << "message here." << endl;
    string book;
    cin >> book;
    accfile << book << endl;
    accfile.close();
    break;
    case 3:
    //code here
    break;
    Of course, anon has spoilt my fun, but you can see that anon is correct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
             }
           cout << "message here." << endl;
    Which brace does that match?

    You also need an extra set of braces around case 2, since you declare variables in this case.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You're trying to write to an input file... that won't work

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by EVOEx
    You're trying to write to an input file... that won't work
    Oops, you're right.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Quote Originally Posted by anon View Post
    If this were in a switch case, the block using book would need to have braces around it (you can't declare variables within a switch unless they are in braces, since the scope of a variable declared in one case would be otherwise till the end of the switch, meaning that execution might reach a place where a variable is in scope, yet jump over the declaration / constructor call).
    the last 11 words just went *whoosh* over my head.
    Using any kind of braces ( or { or [ around "string book;" gave me an undeclared error.

    I changed the brace to correctly end switch statement.

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Quote Originally Posted by EVOEx View Post
    You're trying to write to an input file... that won't work
    Thank you!

    To get an output file I should add this line?
    Code:
    ofstream accfile

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Furious5k
    Using any kind of braces ( or { or [ around "string book;" gave me an undeclared error.
    As a simplified example, try to compile this:
    Code:
    int main()
    {
        int x = 10;
        switch (x)
        {
        case 1:
            int y = 20;
            x = y;
            break;
        case 2:
            int z = 30;
            x = z * z;
            break;
        };
    }
    A correct version would be:
    Code:
    int main()
    {
        int x = 10;
        switch (x)
        {
        case 1:
            {
                int y = 20;
                x = y;
            }
            break;
        case 2:
            {
                int z = 30;
                x = z * z;
            }
            break;
        };
    }
    Quote Originally Posted by Furious5k
    To get an output file I should add this line?
    Not add, but change.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Yeah, I realised what he meant after the second reply. First time around I read "block using book would need to have braces around it" as "book would need to have braces around it".

    Poor choice of words, I meant to say "use" this line.

    And I'm not indenting anything myself, emacs is doing it.

    Now I just get a single last error (accfile << book << endl.
    Is this because I have "ofstream accfile" and "accfile.open" inside an if/else statement and then trying to output to a file outside of that statement?
    Code:
               else
                 {
                   ofstream accfile;
                   accfile.open ("loans.txt");
                   accfile << name << ";";
                 }
           cout << "enter the ID number of the book." << endl;
           string book;
           cin >> book;
           accfile << book << endl;
           accfile.close();
           break;
           }
    testfile.cc:186: no match for `ifstream & << string &'
    /usr/local/depot/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/std/bastring.cc:469: candidates are: class ostream & operator <<<char, string_char_traits<char>, __default_alloc_template<false,0> >(ostream &, const basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> > &)

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Furious5k
    Is this because I have "ofstream accfile" and "accfile.open" inside an if/else statement and then trying to output to a file outside of that statement?
    Yes, now it looks like a problem of scope. If you only use accfile within that case, then declare it within that block. However, have you considered separating your code into functions?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21