Thread: help with fgets

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Ah the joys of people being given code without an explanation of what it does.

    Any basic texts on C++ (even the really bad ones) will cover this stuff in the first few chapters. Just read them from the beginning.

    :: is the scoping operator. So the name X::Y means something named Y within a namespace (literally a universe of names) named X. So std::getline refers to something named getline in the std namespace. The std namespace is reserved for use by compilers and libraries .... basically so, if a programmer needs to, something called getline can be created in another namespace without clashing.

    The >> and << are operators. They have multiple meanings. For integer types, they are bitshift operators. When working with C++ I/O streams, which is how you're working with them, the syntax "some_output_stream << x" means that a variable s is written out to the specified output stream - << is called things like the "stream insertion operator". The syntax "some_input_stream >> x" means a value of x is extracted from the input stream, and >> is referred to as something like the "stream extraction operator".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  2. #17
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    one final question.. that means using
    Code:
    std::getline()
    and
    Code:
    getline()
    means the same or different... ?
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  3. #18
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    hope the above question was not stupid attempt of me trying to act as not stupid at all :P
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In this context std::getline and getline refer to the same function since the using directive, using namespace std, is in effect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need a good beginner's book for C++. I recommend Accelerated C++.
    Very informative. Very good.
    You will be ready to do good C++ programs afterwards (not just basics!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    yeah... great book for me.. m reading it atm... thx for that
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  7. #22
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    Code:
    // Input your full name and put it in a frame
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout<<"Enter your full name : ";
    	string name;
    	getline(cin,name);
    	const string greet = "Hello, " +name+"!";
    	const string spaces(greet.size(),' ');
    	const string second = "* "+spaces+" *";
    	const string first(second.size(),'*');
    	cout<<"\n\n"<<first<<endl;
    	cout<<second<<endl;
    	cout<<"* "<<greet<<" *"<<endl;
    	cout<<second<<endl;
    	cout<<first<<endl;
    	system("pause");
    return 0;
    }
    I wrote it on my own without looking at the codes.... I am really enjoying reading that book... CHeers
    Last edited by n3cr0_l0rd; 06-09-2009 at 06:47 AM.
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  8. #23
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    When i looked back into the codes in that book, it had included <string> header file too... But why? I couldnt get it why? The original code in the book:
    Code:
    // ask for a person's name, and generate a framed greeting
    #include <iostream>
    #include <string>
    int main()
    {
     std::cout << "Please enter your first name: ";
     std::string name;
     std::cin >> name;
     // build the message that we intend to write
     const std::string greeting = "Hello, " + name + "!";
     // build the second and fourth lines of the output
     const std::string spaces(greeting.size(), ' ');
     const std::string second = "* " + spaces + " *";
     // build the first and fifth lines of the output
     const std::string first(second.size(), '*');
     // write it all
     std::cout << std::endl;
     std::cout << first << std::endl;
     std::cout << second << std::endl;
     std::cout << "* " << greeting << " *" << std::endl;
     std::cout << second << std::endl;
     std::cout << first << std::endl;
     return 0;
    }
    Last edited by n3cr0_l0rd; 06-09-2009 at 06:52 AM.
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by n3cr0_l0rd View Post
    When i looked back into the codes in that book, it had included <string> header file too... But why? I couldnt get it why? The original code in the book:
    Because it is not possible to use a string in a program without including the string header.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::string resides in the <string> header. That mentioned, however, sometimes one header can have dependencies on another - such as iostream dependent on <string>, thus you can sometimes get a header for "free".
    This is not something you should rely on.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    @Elysia Thx, i get it... You are really too great
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM