I am very new to C++ and have written only basic algorithms. In school we use turbo c++ compiler but now i have to attend a workshop for which I need to be able to use a gnu compiler and I do not know how to change my programs for it. I tried to google it but was not very successful. There are few things i want to know in particular:

  • if I can't use <conio.h>, what do i do if I want to use a function like clrscr()?
  • what is namespace?(i read about this but didn't understand it:()
  • If i want to use void main() instead of int main(), if i can't use getch(), then what do i do?
  • Is the syntax for structures in turbo c++ and a gcc based compiler the same?
  • If i am using int main(), is it not necessary to end it with return 0?
  • Out of the following list, which are the header files that i can use in a gcc based compiler:
    iostream.h
    stdio.h
    stdlib.h
    ctype.h
    time.h
    string.h
    iomanip.h
    math.h
    matrix.h
    process.h

    and in which of them should i drop out the .h?which of them can i use wtih a little modification?

  • should it be <math>,<ctype>,<time> or <cmath>,<cctype><ctime>?
    what is the difference?

please respond asap...it's sort of urgent

  1. Don't clear the screen because there may be information on the screen that people want to see. But you could use something like system("cls") or write your own cls() function using os api calls.
  2. namespace is just an umbrella used to keep symbol names from colliding with each other. It just groups together names/classes/structures/objects.
  3. getch() has nothing to do with the how to declare main(). main should never be declared as void because it always returns an integer whether you tell it to or not. Even if you omit the return statement main() will return an integer anyhow.
  4. Yes -- the syntax for a structure has never changed from day one when K&R was written.
  5. c++ does not require an explicit return statement -- main() defaults to 0 if you don't tell it to return something else.
  6. You can use them all except iostream.h, iomanip.h -- all you have to do is omit the extension on those two leaving <iostream> and <iomanip>.
  7. Put a 'c' in front of those names, such as <cmath> and <cctype>. The only difference is the compiler will put them under std namespace.
commented: Nice +18

thank you ancient dragon, but I still don't understand why I need to use namespace. What happens if I don't use it? And how do I define a namespace?
Can I use <math> and <ctype>...or do i *have to* use c in front of them?
How do I use system("cls")?

>I still don't understand why I need to use namespace
Because the standard library places everything in the std namespace. If you want to use the standard library, you have no choice but to at least mention that namespace somewhere in your code.

>What happens if I don't use it?
You don't have to create your own, but you do have to use the namespaces created by others.

>And how do I define a namespace?
Wrap your code in a namespace block:

// Add foo in the jsw namespace
// If jsw hasn't been defined yet, create it
namespace jsw {
  void foo()
  {
    //...
  }
}

>Can I use <math> and <ctype>...or do i *have to* use c in front of them?
You can use <cxxxx> or <xxxx.h>, but <xxxx.h> is not recommended (it's also officially deprecated) because the names won't be in the std namespace, and unless you've memorized every name in the C library, you're more likely to get conflicts. However, this only applies to the standard headers inherited from C.

>How do I use system("cls")?
You just did. :) Don't forget to include <cstdlib> though:

#include <cstdlib>
#include <iostream>

int main()
{
  std::cout<<"Test\n";
  std::system ( "CLS" );
  std::cout<<"Another test\n";
}

thank you Narue. I would like to try out a few programs though. Where can I download it?

>>Where can I download it?
Where can you download what?

Where can I download a gcc compiler?

Browse around MinGW.org -- the Windows port of the GNU compiler. If you also want a nice IDE then download Code::Blocks, which also supplies MinGW compiler tools.

Thank you. I am trying that out now.
I can use system ("PAUSE") in a gnu compiler, right? Also, would it be better to use system ("PAUSE") on cin.get()?
Does system("PAUSE") also wait for an key to be pressed?

Thank you. I am trying that out now.
I can use system ("PAUSE") in a gnu compiler, right? Also, would it be better to use system ("PAUSE") on cin.get()?
Does system("PAUSE") also wait for an key to be pressed?

Don't use system("PAUSE"). It is causing unncessary overhead to your program by making system calls. Just stick with the latter

>Don't use system("PAUSE"). It is causing unncessary
>overhead to your program by making system calls.
system("PAUSE") is typically used to keep a command prompt window open after the program is finished. Overhead is irrelevant when the very nature of the system call is I/O bound. More important is the fact that a malicious party can easily replace the pause program with malware, and the program will innocently run it with it's own privileges. It also hurts portability because the pause program isn't standard across platforms.

>Don't use system("PAUSE"). It is causing unncessary
>overhead to your program by making system calls.
system("PAUSE") is typically used to keep a command prompt window open after the program is finished. Overhead is irrelevant when the very nature of the system call is I/O bound. More important is the fact that a malicious party can easily replace the pause program with malware, and the program will innocently run it with it's own privileges. It also hurts portability because the pause program isn't standard across platforms.

Bum! I was having wrong concept ;)

All of you, thank you so much for helping out. That's it for now. I'll post more questions when they come.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.