gets(); on my nerves!!

Hello out there people. Just on the verge of slashing up! No amount of books and forums can fully teach you c++. In the end you need real people that know what they're talking about to help on you past the trickier bits......the lesson today is: GETS(string);
Could someone give me a in depth description of what it does, and what affects its use because i have been trying it instead of cin and its making some strange and unexplainable outputs.The following are alternatives that i've tried without success:
gets(string);
gets_s(string);
cin.getline(string, n)
GETS? As far as I know, there's no such function in the standard library. You probably meant... the function that's on the top of the list of functions that didn't work.

gets(char * s)...
reads characters from stdin and stores them as a string into str until a newline character or an EOF is reached.

That said, this is why you shouldn't use gets():
http://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/300-BSI.html

Assuming string is an std::string, you'll want to do something different for strings with getline:
std::getline(std::cin,string);

-Albatross
Last edited on
Hi Albatros,
I had an idea that my "lecturer" had given me a duff command to use.
here's the example he gave in his notes.

//Program to show the input of single words using gets
#include<iostream>
using namespace std;
void main()
{
char name[20];
cout<<”Enter your full name ”;
gets(name);
cout<<”Hello “<<name<<endl;
system(“pause”);
}

What chance have i got if my lecturer doesn't know his stuff!!
Thanks anyway your help is appreciated.
If this is an exact copy of the code he gave you, then... what?!? See that void main() and that system("pause")? And gets() reading to an array too... fooey. And why the mixing C and C++ I/O?

I'm... @.@

-Albatross
Last edited on
Your instructor doesn't know anything about C++, and shouldn't be teaching it.

You should take that piece of code and go visit the department chairperson (and yes, even if you are in High School there is a department chair). Unless your instructor is the department chairperson, he will very likely recognize the code as very, very poor quality. (If he is the chair, find his boss and complain.)

Be sure prepared to explain why:

  1. The program mixes C and C++ I/O. (Pick a language and stick to it!) People who know C++
     avoid doing that because it can cause problems...

  2. The program is not ISO-standards compliant. void main is illegal in C++.

  3. Every competent C and C++ programmer in the whole world knows that gets() is one of
     the most dangerous security exploits in all of computer language history, and its description
     pointedly and frankly warns against its use. Your professor's code almost guarantees that at
     least one of his students will cause it to overflow, since many people's full names are more
     than nineteen characters long.

  4. Using system() is likewise a security hole and a very bad habit. Its man page also warns
     users to be very careful with it - in addition to the litany of things that can go wrong.

Heck, print this page out and take it with you. Your argument is made.

Good luck!
Hi there i am studying for a computing degree at the university of central lancashire, UK. Its my 1st year and rather not create any waves and jeopardise my student/lecturer relationship at this stage!! lol however i will be sure to pass this link around my fellow classmates and warn them of our plight!!
My next programming lecture is Nov14 and no doubt i will have more quandaries to post.
Many thanks for the guidance i really enjoy the subject and aim to be top of my class! what a geek! hehe
No, seriously, you need to complain to the department chair. If necessary, you can do this anonymously.

That example was so seriously broken that the student/lecturer relationship is already jeopardized -- if not ruined. You cannot trust him to teach you properly.

The University of Central Lancashire is a highly-ranked institution, but the instruction you have recieved is shameful. Bring it to the school's attention so that they can fix it!

In the meantime, do your best in class and ask us as many questions as you need and live out of your textbook.
To be sure, are you 100% sure that this example didn't have a big bold title with the words
"THIS IS HOW NOT TO PROGRAM"
...?

-Albatross
Last edited on
Sometimes we cannot fault the professors or lecturers in Universities. Most of their area of specialization is in computer science algorithm related field of study. Programming language is just a tool to them to realize their theory. It is therefore forgivable when they do not focus a lot on language "correct" syntax or the like. E.g void main() vs int main() etc issues.

Hence very often they have a "side-kick" or a Masters student under their charge pursuing a PhD to do the programming on their "behalf" in their algorithm implementation. Of cuz that does not imply all computer science PhD holders don't do programming but there do exist a group that focus a lot on algorithm creation instead of programming.

A computer science algorithm is programming language independent. This also explain why most of them are in academic circles or if in business world their R&D department instead of their software development department.



The reasons you cite are valid observations, but there is no excuse to not know the basics of the subject you are teaching. If you are required to teach C++, take a week or so to learn something about it before you get to the classroom. Read some books. Write some programs to do stuff. Join a forum and ask questions about your code, examples, etc. Lack of time is not a valid excuse. If you are getting paid to do something, you've got time to do it right.

I don't expect the professor to be an expert in C++. I only expect some competence -- know how to research and learn a little something about valid C++ programs before teaching others what you (don't) know. Having written some C code years ago doesn't qualify. C++ is not C. ("with classes".)

My $0.02
Actually based on information I have from a Masters student during my times, PhD holders are supposed to "churn" out research papers to increase their reputation and also the University world ranking order. For some Universities, even a working prototype that can be used! This is actually their main objective and explain for their existence in the Universities.

As for teaching under-graduates, those are actually "side-objective" on top of their main objective. So until the day when those PhD holders instructions is clear as in full-time "teacher" to under-graduate, the phenomenon of them lax on programming language "correct-ness" is here to stay.

Of cuz there are also a group of PhD holders that do take BOTH main and side-objectives as equal footing. They really do research and read up before they start their lectures and tutorials. These are dedicated researchers and also teachers. I salute them for the extra hours they put in.

In Chinese saying, we say the same kind of rice raise different kinds of people :)
Oh yes, I agree with you totally. It is an actual, systemic problem.

I just think the student has a consumer right to get what he is paying for. No one goes to the university out of a philanthropic need to support his professors' research projects. One goes to support his own education.

[edit] Fixed grammar and added content.
Last edited on
@steallen
Don't delete anything! Leave this topic standing! More on this later!
@steallen
I got your PM, as Duoas did. I recognize what your professor is trying to teach, namely the conversion of algorithms to runable code, but I would suggest to him that he use a safer language than C or C++ for his purposes if he will not teach proper C or C++. Maybe Java Python would be better for this?

-Albatross
Last edited on
I agree, python, perl, or ruby if he needs OO.
Language doesn't matter.

Correct algorithms --> correct code.

Correct code != pedantically perfect code.


You have my (lengthy) response in PM. Your professor's response is inappropriate and unprofessional and should be brought to the attention of the university. You may have a little bit of a fight ahead, but you have nothing to lose but the high-quality education your university is known for. Don't let him bully you!
Last edited on
I wish this wasn't going on in PM so the rest of us could see it =(
Me too. But we need to respect his desires.
Last edited on
Topic archived. No new replies allowed.