Thread: Dev-C++ 4.9 confusion

  1. #1
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22

    Dev-C++ 4.9 confusion

    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    int main()
    
    
    {
    char *name;
    int size;
    cout<<"Enter size";
    cin>>size; // Works
    name=new char[size++];
    fgets(name,size++,stdin); // This does not get executed
    cout<<"Name is"<<name;
    getch();
    return 0;
     }
    The first cin works but the program terminates. The same code Works Fine in the Old Turbo C++ compiler. Any suggestions?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I don't know C++, but something looks really screwy with the increment operators. You're allocating a certain size (that's what new does, right?), and then fgets() for that size plus one. That means you can easily be prone to buffer overflow issues.

    Also, get a new compiler, Turbo C++ is terrible even for just testing

  3. #3
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22
    Dude I am using Dev C++ 4.9 compiler and It does not even without the increments.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well...
    • Get rid of #include <conio.h> and getch because they are non-standard and you don't need them here.
    • size should be of an unsigned integer type, e.g., std::size_t or unsigned int.
    • The post-increment of size here is wrong.
    • Do not mix C and C++ style I/O: use cin.getline instead of fgets.
    • delete[] what you new, or simply #include <string> and use std::string instead with getline.


    By the way, Dev-C++ is an IDE, not a compiler, and it is no longer being maintained.

    EDIT:
    Oh, and one more thing: cin >> size; leaves the newline in the buffer. You'll need to remove that (e.g., with cin.ignore) before the cin.getline will work as expected.
    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. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First you should not be mixing C++ streams and C-stdio functions, use one or the other.
    The problem is that the cin leaves the end of line character in the input buffer which fgets() reads and then considers the input complete. You should also replace the fgets() with getline(). So to fix the problem you need to extract the end of line character. Something like:

    Code:
    cin >> yourVariable;
    cin.ignore(100,'\n');
    cin.getline(yourVariable, yourVariableSize);

    Jim

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > Dude I am using Dev C++ 4.9 compiler and It does not even without the increments.
    /facepalm you just said you tested with turbo c.

    Anyway, make these changes:
    Code:
    #include <iostream>
    /* no more conio */
    
    
    using namespace std;
    
    
    int main()
    {
    	char *name;
    	int size;
    	cout<<"Enter size";
    	cin>>size;
    	name=new char[size+1]; /* no more buffer overflow */
    	getchar(); /* get rid of the newline character */
    	fgets(name,size,stdin);
    	cout<<"Name is "<<name;
    
    
    	delete [] name; /* don't ever forget to free memory */
    	return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    memcpy: you might want to read up on fgets to find out just how the second parameter is used before making the comment that size+1 means no more buffer overflow. Other than this, I suggest being consistent in the choice of C or C++ I/O: don't mix them.
    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

  8. #8
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22
    1.So the problem with new line that is left by cin ?
    2. something wrong with DevC++?
    3.Fgets has bugs?
    4.I would like to know what "IDE" you use.?
    Edit: I know that i mixed up some C and C++ but the compiler should work for both as its designed to.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Waleed Mujeeb
    1.So the problem with new line that is left by cin ?
    In terms of what you observed versus what you expected, yes. Note that the line that you claimed does not get executed does get executed.

    Quote Originally Posted by Waleed Mujeeb
    2. something wrong with DevC++?
    Yes, though you may or may not run into the latent bugs present.

    Quote Originally Posted by Waleed Mujeeb
    3.Fgets has bugs?
    The standard library fgets function implementation is unlikely to have (many) bugs.

    Quote Originally Posted by Waleed Mujeeb
    I know that i mixed up some C and C++ but the compiler should work for both as its designed to.
    It is possible for the synchronisation with stdio to be disabled.
    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

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Waleed Mujeeb View Post
    1.So the problem with new line that is left by cin ?
    2. something wrong with DevC++?
    3.Fgets has bugs?
    4.I would like to know what "IDE" you use.?
    Edit: I know that i mixed up some C and C++ but the compiler should work for both as its designed to.
    1. In a way, yes.
    2. Get a newer one.
    3.No, it works exactly as documented.
    4.GCC/Eclipse
    5.You can, but you have to be prepared for doing a lot more syncing (for which there are built in functions).

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Mixing C-stdio and C++ streams can cause subtle problems, so it is better not to mix the two.

    1.So the problem with new line that is left by cin ?
    Yes.
    2. something wrong with DevC++?
    In this case, not yet. But DevC++ is using a extremely old compiler, and is not considered standard compliant. Also this IDE has many bugs that can cause strange problems.
    3.Fgets has bugs?
    No, fgets() is a perfectly fine C function. However as stated earlier mixing C-stdio (fgets) functions with C++ streams (cin) should be avoided. Either use C-stdio or C++ streams, don't mix them.
    4.I would like to know what "IDE" you use.?
    I use Code::Blocks however I also use Linux not Windows. So I would suggest you try one of the following, wxDevC++, Code::Blocks, or MS Visual C++ express. All three of these compilers are free to download and are decent modern C++ IDE with modern compilers. The wxDevC++ is a fork of your current IDE with a modern compiler, and an updated IDE. If you decide on Code::Blocks be sure you download the version with the compiler.


    Jim

  12. #12
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22
    Thanks , I am new here ,Just asking is this forum a part of hacker culture? You might want to call me stupid but i would better know than never.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Waleed Mujeeb
    Just asking is this forum a part of hacker culture?
    To the extent that the members of this message board community are C, C++, etc hackers, yes
    Whether it is or is not does not really matter, methinks.
    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

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > memcpy: you might want to read up on fgets to find out just how the second parameter is used before making the comment that size+1 means no more buffer overflow.

    Gah, I forgot about that. I need to be more careful =S

  15. #15
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Waleed Mujeeb View Post
    Thanks , I am new here ,Just asking is this forum a part of hacker culture?
    Err.. Probably not the definition of hacker you have in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion
    By Nestor in forum C++ Programming
    Replies: 4
    Last Post: 06-20-2008, 02:17 PM
  2. Confusion with * and &
    By Ganoosh in forum C++ Programming
    Replies: 32
    Last Post: 06-23-2005, 10:16 PM
  3. if,else if and else confusion...
    By diding in forum C Programming
    Replies: 5
    Last Post: 10-10-2004, 08:19 PM
  4. COM confusion. When should I use it?
    By bennyandthejets in forum Windows Programming
    Replies: 2
    Last Post: 04-25-2004, 04:40 AM
  5. C++ confusion
    By charlie in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2001, 05:46 AM