views:

382

answers:

6

I'm working as a TA in an introductory programming class, and the students tend to submit their programs as either one line, or without any indentation. Is there any tool that allows me to insert indents and things like that automatically? (We're using C++ and VisualStudio)

+3  A: 

There is a gnu program called indent, usually shipped with linux but also available here (gnu indent) and available under Cygwin.

However, you are using VS, so you could use it to format code. They have hidden the feature just a bit: Edit -> Advanced -> Format Document, or Control/E, D.

DigitalRoss
+17  A: 

You're after a pretty printer. I'd suggest Googling for C++ pretty printer, and looking for something that meets your requirements (price, platform).

As an aside, you may find that deducting marks for poorly formatted code will work just as well. Students need to learn that good code layout is an important part of writing maintainable code.

Martin
Upvoted for suggesting that points are deducted for poorly formatted code.
Ryan Taylor
Great idea, except that online homework site we're using removes all formatting.
chustar
There's your problem then. You might want to consider asking them to change that, or find another supplier.Right tools for the right job and all.
Martin
Be very careful deciding what is "poorly formatted". I must admit to finding it difficult to read code that is not in my style (for example the linux kernel), but that doesn't mean that the linux kernel is poorly formatted.
Richard Corden
@Richard: Absolutely, but you're talking about what Alexandrescu and Sutter refer to as the "small stuff". Where you put your braced doesn't matter so much, but not indenting code blocks, or putting multiple statements on the one line (particularly throwing something short on the end of a long, complicated line), will lead to bugs.
Martin
A: 

Select the entire file (Ctrl-A) and then hit Ctrl-K Ctrl-F, which is essentially format the entire document.

EDIT: Of course in Visual Studio IDE

Ashwin
This won't change vertical space though. If people are submitting on a single line, it will just correct the indentation on that single line.
Martin
Agreed.
Ashwin
This is a stupid answer. Real-life programming is more about communicating ideas to other humans than it is about communicating with the compiler. If you don't teach them that lesson, then you are letting them down.
alex tingle
When started reading, I thought it will be "Ctrl-A and DELETE" :-D
Pawka
That is actually a good idea! Why did I not think of that?
Ashwin
+2  A: 

If you need to do this in batch mode, try using astyle, also available in the Cygwin installer.

Amro
+14  A: 

Rather than answering your question I will advise:

Don't let them do that.

Making their code readable for human beings is a part of programming, and you are fully justified in grading them on it. You might want to point them at the pretty printers listed in the other answers, however. Just to be nice.

dmckee
I don't have a choice. Its done by the online homework system.
chustar
Then your homework system needs to be fixed, acquiring good coding style is essential in our business so its good to learn early.
Anders K.
What is readable depends very much on the readers previous experience and what they're accustomed to. There are possibly a few concepts which overlap to all styles which might be worth enforcing, but it's much more important that the code does what it is supposed to do first.
Richard Corden
Richard, I'm am certainly not suggestion "Use my brace style or else" which would be unreasonable and extreme. But it is fair to expect the code to be indented and possessed of a more-or-less consistent formatting. However, it sounds like the problem may be that student submissions are being mangled by the online tool, which means that the tool is broken. So: **give the students a break for now, but make them learn how to use the tool successfully**.
dmckee
A: 

In Vim it's gg=G.

StackedCrooked
This will correct indentation, but will not break lines.
David Rodríguez - dribeas