Hi, this is not a HUGE problem but I was jw why at the end off all my codes I have to type, system("pause");.

When I even do the Hello World! program thing the black screen just flashes. I was jw why no one elses codes have this.
Its not a big problem but i was just wondering.

jw is "just wandering" right?

Without system("pause");, your program will end right after printing your final text, so you won't have any time to read the text before is closes, if you where in DOS rather than in a consol, you wouldn't have to worry about this.

You can use getch() to do the same thing but without the anoying text. ;)

Q

Hi, this is not a HUGE problem but I was jw why at the end off all my codes I have to type, system("pause");.

When I even do the Hello World! program thing the black screen just flashes. I was jw why no one elses codes have this.
Its not a big problem but i was just wondering.

When you program for a console window, the program just do what
you ask to do, and then exit returning control to the operating system.
That's why "the black screen just flashes". In orden to see what is happening you has to make the program to pause or wait for you to enter a key. System("PAUSE") achieves this but is operating system
depended. Meaning "not portable". Another way of doing it, is using the getchar function. Or you can always run the program in a terminal or command line console.

You can use getch() to do the same thing but without the anoying text. ;)

The function getch is a compiler especific function, meaning that is not
standard so you can not use it with every C compiler.
Stay away from it. Getchar is the standard, use it, and build good program habits from the beginning. :confused:

>Another way of doing it, is using the getchar function.
In C. It's not a good idea to use that in C++, use cin.get() instead.

Also, you should be aware of the input buffer scenario, and often you'll have to clear it before calling these functions.

In C:

int ch;
while (ch = getchar() != '\n' && ch != EOF); // keeps grabbing chars until none left
getchar(); // NOW pause the program

In C++:

std::cin.clear(); // first get rid of any errors
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // clears input buffer
cin.get(); // pause the program

> In C. It's not a good idea to use that in C++, use cin.get() instead.

Any good reason to back that up ? ;)

> Any good reason to back that up ?
Bleh, I never really learned the technical details behind it. But feel free to explain any potential problems that you're aware of that come as a result of mixing C and C++ input functions.

In any case, I know this much: getchar() is C, cin.get() is C++. Use the function that matches your language and you'll be far safer.

> In C. It's not a good idea to use that in C++, use cin.get() instead.

Any good reason to back that up ? ;)

According to the standard, the c header files are deprecated, meaning they might be removed from the standard in future revisions.

The good thing about C / C++ is its flexibility and its the flexibility which kills the beginners. You can as well mix fgets and cin >> and still get away with it, no problems as such.

Oh and btw, getchar is C++ . Just include cstdio and you are good to go. The fact that getchar is included in the standard namespace makes it a C++ standard function. Call it backward compatibility or consideration for C programmers if you want, but IMHO getchar is C++. If you will notice C++ has no direct character manipulation and processing functions. The header cstdlib , ctype and cstring seem to do that job pretty well... ;)

The good thing about C / C++ is its flexibility and its the flexibility which kills the beginners. You can as well mix fgets and cin >> and still get away with it, no problems as such.

Right, it is part of the current standard, but such C functions might be removed in the future (and a new standard is due out in the next couple years)

How would you account for those millions of lines of code which use the old C functions, wrapped up in the new standard namespace? And like I pointed out getchar IS C++ otherwise it wouldn't have been included in the C++ standard namespace. Call it backward compatibility if you like, but thats how the situation stands.

Boy is this thread full of BS :D

1) cstdio is not deprecated. It is the C header for C functions fully allowable in C++. You don't deprecate a language... C is a language, not an old set of functions for C++. If you can offer a link stating otherwise, please do so.

2) It is better to use C++ I/O functions in C++. But C I/O functions are allowed -- although it is best not to mix the two, because they do work differently. If you use getchar(), use C input functions throughout the code.

I wonder what BS here stands for... ;)

I wonder what BS here stands for... ;)

Bumm Steer... I think :twisted:

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.