cin.getline?

This is a discussion on cin.getline? within the C++ Programming forums, part of the General Programming Boards category; Hi! i am sorta new to this forum, but i have a problem with my code/compiler. i dont know if ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    14

    cin.getline?

    Hi! i am sorta new to this forum, but i have a problem with my code/compiler. i dont know if it is the compiler's fault or mine. I am self taught and learning from the book "SAMS teach yourself C++ in 21 days" i am trying to code a program that deletes(well, replaces the file with something else that has nothing in it.) other files. Here is my code...

    Code:
    #include <iostream>
    #include <fstream>
    
    using std::cin;
    using std::endl;
    using std::cout;
    
    int main()
    {
        char filename[80];
        while (true);
        {
            cout<<"Type '0' to exit the program"<<endl;
            cout<<"Enter filename: ";
            cin.getline();
            if (filename[0]==0)
            {
                return 0;
            }
            ifstream fin(filename);
            if (fin)
            {
                fin.close();
                cout<<"Deleting"<<endl;
                ofstream fout(filename,ios::trunc);
                fout.close();
                cout<<"Done!"<<endl;
                continue;
            }
            cout<<"File not found!"<<endl;
        }
        cout<<"Error!!!"<<endl;
        return 1;
    }
    Heres the errors i am getting and why i think i shouldn't get these errors, i am getting...
    Code:
    15 C:\Documents and Settings\Simon\My Documents\Untitled1.cpp no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline()'
    but i know that the getline is being called from the #include <iostream> and also from the code using std::cin;

    If i replace the code
    getline(filename) with cin>>filename;

    i have no problems from the compiler about the cin, but then again i get this error...
    Code:
    20 C:\Documents and Settings\Simon\My Documents\Untitled1.cpp `ifstream' undeclared (first use this function)
    but i know i declared ifstream when i wrote #include <fstream>

    i am using Dev-C++ 4.9.9.2 can someone here tell me if my logic is wrong or if my compiler is really buggy and need to get/download another one?
    Last edited by IKnew; 12-02-2006 at 10:27 AM.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Code:
    cin.getline();
    you didnt give its parameters

    im not sure but i think that fstream is part of the std namespace

    [edit]from what ive heard, "learn X in Y days/hours/weeks" arent recommended[/edit]
    Last edited by h_howee; 12-02-2006 at 10:32 AM.

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    new code... hmm, i still getting the getline error, and i am also getting
    Code:
    27 C:\Documents and Settings\Simon\My Documents\Untitled1.cpp continue statement not within a loop
    the continue, is in the while loop...

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        char filename[80];
        while (true);
        {
            cout<<"Type '0' to exit the program"<<endl;
            cout<<"Enter filename: ";
            cin.getline(filename);
            if (filename[0]==0)
            {
                return 0;
            }
            ifstream fin(filename);
            if (fin)
            {
                fin.close();
                cout<<"Deleting"<<endl;
                ofstream fout(filename,ios::trunc);
                fout.close();
                cout<<"Done!"<<endl;
                continue;
            }
            cout<<"File not found!"<<endl;
        }
        cout<<"Error!!!"<<endl;
        return 1;
    }
    edit: also! thx for telling me that the fstream is a part of std namespace! hehe i didnt know that...

    edit:
    Quote Originally Posted by h_howee
    [edit]from what ive heard, "learn X in Y days/hours/weeks" arent recommended[/edit]
    mind recommending a C++ book i should read from? because i am pretty much done with this book but i noticed theres still ALOT it didnt cover yet, like rand() function.
    Last edited by IKnew; 12-02-2006 at 10:38 AM.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    u have an extra semicolon after while(true)

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    thx! that solved everything except the cin.getline problem...
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        char filename[80];
        while (true)
        {
            cout<<"Type '0' to exit the program"<<endl;
            cout<<"Enter filename: ";
            cin.getline(filename;)
            if (filename[0]==0)
            {
                return 0;
            }
            ifstream fin(filename);
            if (fin)
            {
                fin.close();
                cout<<"Deleting"<<endl;
                ofstream fout(filename,ios::trunc);
                fout.close();
                cout<<"Done!"<<endl;
                continue;
            }
            cout<<"File not found!"<<endl;
        }
        cout<<"Error!!!"<<endl;
        return 1;
    }

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Code:
    cin.getline(filename;)
    the semicolon's in the parentheses
    it wasn't in your last code though (o.O)

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    lol mistyped it, but i am still getting the error tho even after i fixed it...

  8. #8
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    try
    Code:
    cin.getline(filename, sizeof(filename));

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Quote Originally Posted by h_howee
    the error is?
    Code:
    13 C:\Documents and Settings\Simon\My Documents\Untitled1.cpp no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(char[80])'

  10. #10
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Quote Originally Posted by Darklighter
    Code:
    cin.getline(filename, 80);
    mines better =D

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    hurray! thanks alot! forgot that the getline takes 2 parathesis. soo much to remember...

    final code,
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        char filename[80];
        while (true)
        {
            cout<<"Type '0' to exit the program"<<endl;
            cout<<"Enter filename: ";
            cin.getline(filename, sizeof filename);
            if (filename[0]=='0')
            {
                return 0;
            }
            ifstream fin(filename);
            if (fin)
            {
                fin.close();
                cout<<"Deleting"<<endl;
                ofstream fout(filename,ios::trunc);
                fout.close();
                cout<<"Done!"<<endl;
                continue;
            }
            cout<<"File not found!"<<endl;
        }
        cout<<"Error!!!"<<endl;
        return 1;
    }
    thx! :-P

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    why not to use
    string filename;
    ?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    h_howee is right. sizeof ( filename ) would be a better choice.

    Or you could do:

    Code:
    const int size = 80;
    
    char filename [ size ];
    
    cin.getline ( filename, size );
    Last edited by Darklighter; 12-02-2006 at 10:58 AM.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Quote Originally Posted by vart
    why not to use
    string filename;
    ?
    i didnt understand what you mean by that... sorry

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Quote Originally Posted by Darklighter
    const int size = 80;

    char filename[size];

    cin.getline(filename, size);
    err, whats the point of that?

    edit:
    Quote Originally Posted by Darklighter
    h_howee is right. sizeof ( filename ) would be a better choice.
    yeah i saw that so i changed it. hehe

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 06:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 11:01 AM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 08:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM

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