Need Help

This is a discussion on Need Help within the C++ Programming forums, part of the General Programming Boards category; I hope this helps. walk through it with a debugger and see what happens when you run the program the ...

This is the kind of code Sebastiani has in the signature and shows clearly how code bunched together in unreadable. So don't bunch it together like that. Put it on several rows.

And then system("pause") should be avoided. Any type of pause should be avoided, really. And it is usually bad practice to call exit from within a function. Just something to think of. For example, it could return true or false, and if false, then main function cleans up and returns.

system("pause") is the easiest way I know to halt the program so the user can view the output.
It is one line and is self explanatory for a new programmer.
Most debuggers create a new console output for 'std:ut', without halting the program the informations displayed will disappear.

It also locks the code to the Windows platform, making it non-portable.
The best way is to do no pauses at all.
Run the program through a good IDE that doesn't close the window when the program quits, and you won't need to.

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.

>> system("pause") is the easiest way I know to halt the program so the user can view the output.

Try cin.get( ). It's completely portable, and it doesn't resort to using dangerous API's like system( ). Again, with experience you will find there is less and less reason to do such a thing. Invoking a program from the command line or from an IDE is the way to go in most cases.

>> Now Elysia I got one for you. How would you make *** portable across windows and Linux?

You don't. But as Elysia already pointed out, a sufficient level of abstraction can make just about anything portable.