HELP! bugged program

This is a discussion on HELP! bugged program within the C++ Programming forums, part of the General Programming Boards category; I been having problems debugging this program, can you give me some hints to solve it... i been getting a ...

I agree with the starting from scratch. You have many fundamental flaws in your code. For example:

# include <iostream.h>
# include <iomanip.h>
# include <string.h>
# include <conio.h>
# include <process.h>
# include <fstream.h>

#include<iostream>using namespace std;

You need to start, from the ground up, and make better decisions. E.g. are you planning on using the standard headers (iostream, string, fstream) or the deprecated (iostream.h, string.h, fstream.h). You use BOTH, which is very dangerous. E.g.:

iostream.h defines a variable called cout, in the global namespace (::cout). iostream defines a variable called cout in the std namespace (std::cout). Now, within the global namespace, you included the namespace std (which is itself usually bad form). Now you just have the mother of all name collisions, because you now have 2 definitions of cout that are in scope, 2 definitions of cin, 2 definitions of many, many things.

Further, some parts of your code are just plain unnecesary, which strongly suggests that you should read more on namespaces, and the 1998 C++ standards. Some things you do are just plain bizarre, like:

using namespace std;
#include<iomanip>using std::setw;

The first line brings every symbol from namespace std into the current namespace (in your case, the global namespace). The third line brings one symbol in. Now, it's not WRONG, per se, but it's completely useless; the top line by definition includes std::setw, because it includes std::*.

It IS wrong that you *already* had a ::setw declared in the global namespace because you used deprecated headers, though. You should get a million "ambiguous reference" errors, because "cout" could mean std::cout or ::cout, "setw" could mean std::setw or ::setw, etc.

Just to repeat what Salem said...

Start small... Just write enough code so that it compiles. Add a little code at a time, test-compiling and test-running as you proceed. This way you will know right away where the problems are.

It takes a bit of practice to learn how to sequence your program development so that it's testable as you go along. Usually, the user interface, or input/output is started first (but maybe not finalized) so you can see what your program is doing.

Programming is difficult, and every programmer test-compiles and test-runs during development. Beginners should test-compile every line or two! More experienced programmers might only test-compile every "page" or so, depending on how familiar they are with the particular functions they are using, etc.