14 Replies - 1534 Views - Last Post: 28 August 2011 - 02:47 AM Rate Topic: -----

#1 Luiz herege  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 26-August 11

What should i really use for input/output on c/c++?

Posted 26 August 2011 - 06:19 AM

Hi, i am doing college and i got some issues about what are teaching me.
I know that scanf() is a pain to use since you have to do some fflush() from times to time or it just get shit from the buffer.
Sadly on my college people don't teach c/c++, they teach you how to program and use basic c/c++ knowledge to do it.
So i want to know What a REAL c/c++ programmer use for input/output for a good program and why. Any links to help with my question are welcome.

Grateful,
Luiz.

(My mother language isn't English, sorry for any assassination)

Is This A Good Question/Topic? 1
  • +

Replies To: What should i really use for input/output on c/c++?

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 451
  • View blog
  • Posts: 855
  • Joined: 17-March 11

Re: What should i really use for input/output on c/c++?

Posted 26 August 2011 - 06:31 AM

For console application, the c++ standard defines

std::cin for input
std::cout/std::cerr/std::clog for output.

Those and printf are generally used, everything else is non-standard.

The advantages over printf is that they are generally typesafe, more so than printf. And they allow you to overload the operator <<, so they can work transparently for user defined types.

They are all from the header <iostream> and there is lot's of information about them.

This post has been edited by Karel-Lodewijk: 26 August 2011 - 06:33 AM

Was This Post Helpful? 1
  • +
  • -

#3 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Re: What should i really use for input/output on c/c++?

Posted 26 August 2011 - 06:31 AM

Hey Luiz,


First I suggest taking a look at a c++ reference on the net, www.cplusplus.com/reference/ is a good place to start. This website won't exactly TEACH you, but it will show you the wealth of functions available in both C and C++. For input and output with C++, you want to use IOSTREAM, included as such:

#include <iostream>

It enables you to do stuff like:

#include <iostream>

using namespace std;

int main(void)
{
          // input buffer
          char buff[80];

          // print to stdout hello world
          cout << "Hello World" << endl;

          // get input from stdin
          cin >> buff;

          // print them
          cout << buff << endl;
}




As you can see, It is actually a much easier syntax than C. Handling strings in C++ is much simpler than C..


Use the reference to find out more about other input output libraries, like fstream for files..
Was This Post Helpful? 1
  • +
  • -

#4 Luiz herege  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 26-August 11

Re: What should i really use for input/output on c/c++?

Posted 26 August 2011 - 06:39 AM

View PostKarel-Lodewijk, on 26 August 2011 - 06:31 AM, said:

For console application, the c++ standard defines

std::cin for input
std::cout/std::cerr/std::clog for output.

Those and printf are generally used, everything else is non-standard.

The advantages over printf is that they are generally typesafe, more so than printf. And they allow you to overload the operator <<, so they can work transparently for user defined types.

They are all from the header <iostream> and there is lot's of information about them.


By non standard you mean doing my own functions? I was really only curious about how should i start thinking about that.

View PostDustyD, on 26 August 2011 - 06:31 AM, said:

Hey Luiz,


First I suggest taking a look at a c++ reference on the net, www.cplusplus.com/reference/ is a good place to start. This website won't exactly TEACH you, but it will show you the wealth of functions available in both C and C++. For input and output with C++, you want to use IOSTREAM, included as such:

#include <iostream>

It enables you to do stuff like:

#include <iostream>

using namespace std;

int main(void)
{
          // input buffer
          char buff[80];

          // print to stdout hello world
          cout << "Hello World" << endl;

          // get input from stdin
          cin >> buff;

          // print them
          cout << buff << endl;
}




As you can see, It is actually a much easier syntax than C. Handling strings in C++ is much simpler than C..


Use the reference to find out more about other input output libraries, like fstream for files..


I use cplusplus reference alot, gonna search those libraries. Thanks for the tip.
Was This Post Helpful? 0
  • +
  • -

#5 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Re: What should i really use for input/output on c/c++?

Posted 26 August 2011 - 06:45 AM

You need to know more about the c++ language, use this website to know about all the keywords, and how to make classes for your own input and output operations. When you create your own functions and classes, you will need to use what was described above anyhow..

Use: http://en.cppreferen.../w/cpp/keywords

Have a look, this is more educational on getting started with your own code
Was This Post Helpful? 1
  • +
  • -

#6 Luiz herege  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 26-August 11

Re: What should i really use for input/output on c/c++?

Posted 26 August 2011 - 06:49 AM

View PostDustyD, on 26 August 2011 - 06:45 AM, said:

You need to know more about the c++ language, use this website to know about all the keywords, and how to make classes for your own input and output operations. When you create your own functions and classes, you will need to use what was described above anyhow..

Use: http://en.cppreferen.../w/cpp/keywords

Have a look, this is more educational on getting started with your own code

Ohhh *droll* that helped alot. Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: What should i really use for input/output on c/c++?

Posted 26 August 2011 - 07:02 AM

Well C++ programmers will of course tell you to use cin because it is type safe and kind of *magical* since it can extract integer or doubles or strings all without you having to write some format string.

BUT -- it actually still suffers the problem of leaving things in the buffer (though prehapse not as bad).

Now if you teacher told you to use fflush(stdin) your teacher was giving you BAD ADVICE. fflush is not defined for input streams - so while this might clear the stream that would be nice, but if it were to say format the harddrive that would be perfectly acceptable as far as the language is concerned. in C and C++ you CAN NOT FLUSH AN INPUT STREAM. Honestly it doesn't even make sense. fflush is not supposed to "throw away" anything. What it does is gives the currently buffered data to the stream's sink (consumer or output device).

So if you think about it, your program is the consumer of the input stream... fflush(stdin) should force your program to read the data currently in the buffer.


Anyway I am ranting here, back on track.

cin will also leave unparsable chars in the buffer and you will sometimes need to clear them out. You can do that using cin.ignore(). Specifically you will see this line used a great deal:

std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');

this ignores (throws away) anything in the buffer up-to-and-including the first newline char found.
Was This Post Helpful? 2
  • +
  • -

#8 Luiz herege  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 26-August 11

Re: What should i really use for input/output on c/c++?

Posted 26 August 2011 - 07:09 AM

View PostNickDMax, on 26 August 2011 - 07:02 AM, said:

Well C++ programmers will of course tell you to use cin because it is type safe and kind of *magical* since it can extract integer or doubles or strings all without you having to write some format string.

BUT -- it actually still suffers the problem of leaving things in the buffer (though prehapse not as bad).

Now if you teacher told you to use fflush(stdin) your teacher was giving you BAD ADVICE. fflush is not defined for input streams - so while this might clear the stream that would be nice, but if it were to say format the harddrive that would be perfectly acceptable as far as the language is concerned. in C and C++ you CAN NOT FLUSH AN INPUT STREAM. Honestly it doesn't even make sense. fflush is not supposed to "throw away" anything. What it does is gives the currently buffered data to the stream's sink (consumer or output device).

So if you think about it, your program is the consumer of the input stream... fflush(stdin) should force your program to read the data currently in the buffer.


Anyway I am ranting here, back on track.

cin will also leave unparsable chars in the buffer and you will sometimes need to clear them out. You can do that using cin.ignore(). Specifically you will see this line used a great deal:

std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');

this ignores (throws away) anything in the buffer up-to-and-including the first newline char found.


Oh, that explain a lot. Thanks. Yeah i know its a bad advice and people there use it as a habit now. I just got really worried about how should i program since i want be a real c++ programmer and they don't teach c/c++ really.
Was This Post Helpful? 0
  • +
  • -

#9 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Re: What should i really use for input/output on c/c++?

Posted 26 August 2011 - 08:03 AM

Hmm, you ought to take a look at c++ features such as inheritance and polymorphism, which are concepts of object-oriented programming. C++ is object oriented, whereas C is not..

If you are a making a large program like a game, it might to help to make flow diagrams, or pseudo-code like:

func main -->

1- initialise objects
a - init math objects
b - init graphical entities
2- start game loop
a - process keyboard input
b - calculate new drawing ops
c - draw graphical objects
3- deallocate memory

end main;

When you say you want to be real C++ programmer, you still have a lot of freedom, you just have to make the best use of the features of the language for what suits your purpose. If your making a command line program you might use really C like syntax rather than C++, but you would make use of the really useful STL libraries C++ has..
Was This Post Helpful? 1
  • +
  • -

#10 Luiz herege  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 26-August 11

Re: What should i really use for input/output on c/c++?

Posted 26 August 2011 - 08:08 AM

View PostDustyD, on 26 August 2011 - 08:03 AM, said:

Hmm, you ought to take a look at c++ features such as inheritance and polymorphism, which are concepts of object-oriented programming. C++ is object oriented, whereas C is not..

If you are a making a large program like a game, it might to help to make flow diagrams, or pseudo-code like:

func main -->

1- initialise objects
a - init math objects
b - init graphical entities
2- start game loop
a - process keyboard input
b - calculate new drawing ops
c - draw graphical objects
3- deallocate memory

end main;

When you say you want to be real C++ programmer, you still have a lot of freedom, you just have to make the best use of the features of the language for what suits your purpose. If your making a command line program you might use really C like syntax rather than C++, but you would make use of the really useful STL libraries C++ has..


Hmm knowledge *droll*. Thanks again really helped.
Was This Post Helpful? 0
  • +
  • -

#11 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: What should i really use for input/output on c/c++?

Posted 27 August 2011 - 12:14 AM

View PostLuiz herege, on 26 August 2011 - 02:19 PM, said:

Sadly on my college people don't teach c/c++, they teach you how to program and use basic c/c++ knowledge to do it.
So i want to know What a REAL c/c++ programmer use for input/output for a good program and why. Any links to help with my question are welcome.
What do you mean by "a good program?" - its a pretty loose term.

I would hope that "a good program" would be one which meets all of its requirements, and works as its users expect, while also being resilient against any forms of unexpected/invalid events or data. That "good program" may not even have any visible user interaction (it could be a background application which handles network communication and database access for example); in which case, its I/O would probably be focused on a database API and a network API.

You really need to get out of the mindset of thinking that there are always absolute right and wrong ways of doing things; programming isn't a black-and-white science, instead its about making informed decisions. You should use whatever tool is most appropriate for the job; a lot of the time you'll come up against compromises, and your decision to do things a certain way needs to stem from your understanding of what your options are and what the tradeoffs are for each of those options.


One reason that functions such as scanf/printf/etc are usually avoided in C++ is that they're not type safe. This means that there is no checking (by the compiler) to make sure that your format strings correspond to the arguments you pass to them. Out in the wild, where programmers have used these functions for decades, the effect of this is very often that a programmer makes all sorts of mistakes which end up turning into 'odd' bugs (e.g. passing a double for %d instead of an int). These kinds of bugs can sometimes sit in software for many years without being spotted, and be almost impossible to see until someone, someday, comes along and tries to do something slightly different with the software.

the advantage of cin/cout is that these are type-safe, and its far more difficult to make the same mistakes with cin/cout as you'd expect with printf/scanf. type safety is often your number-one tool in the effort to write more bullet-proof code which can't be broken one day down the line by another programmer making an "innocent" change to the software. The tradeoff is often that the code is a little bit harder to write (but not much harder), and maybe takes a little bit longer (but not much longer). As far as trade-offs go, its almost always worth it, which is why people will often say "always use cin/cout instead".

This post has been edited by Bench: 27 August 2011 - 12:22 AM

Was This Post Helpful? 2
  • +
  • -

#12 Luiz herege  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 26-August 11

Re: What should i really use for input/output on c/c++?

Posted 27 August 2011 - 04:15 AM

View PostBench, on 27 August 2011 - 12:14 AM, said:

What do you mean by "a good program?" - its a pretty loose term.

I would hope that "a good program" would be one which meets all of its requirements, and works as its users expect, while also being resilient against any forms of unexpected/invalid events or data. That "good program" may not even have any visible user interaction (it could be a background application which handles network communication and database access for example); in which case, its I/O would probably be focused on a database API and a network API.

You really need to get out of the mindset of thinking that there are always absolute right and wrong ways of doing things; programming isn't a black-and-white science, instead its about making informed decisions. You should use whatever tool is most appropriate for the job; a lot of the time you'll come up against compromises, and your decision to do things a certain way needs to stem from your understanding of what your options are and what the tradeoffs are for each of those options.


You misunderstood me, i agree with your definition of good program. The only thing i was pissed is that they gave the "you should not do it" way of c programming and by now people, on my class, thinks that warning on the codes are completely normal.

I never said that there was a absolute right way or an absolute wrong way. My all point of getting here was "Man, what a real programmer would do with a knowledge i don't have and why?".
Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: What should i really use for input/output on c/c++?

Posted 27 August 2011 - 04:58 AM

In the words of Steve Jobs "Real artists ship".

A 'real programer' gets something out the door that meets the requirements, is sufficiently fast and economical in system resources for the system it will run on, stops users hurting themselves or the system and is **on time**.

Everything else is highly debatable and is debated at enormous length in coding shops all over the world.

Don't worry too much about thinking there is a nirvana of C or C++ or anything else that you should be aiming for. If your code works and users don't complain about it then do things the way you like.

For example, my standard way of getting user input for the case you are looking at would be be to use getline(). That doesn't make it right or better just what I would tend to do and if I'm not breaking any style guidelines doing it my 'default' way then I am going to that because it's the way I like to do it and because I tend to do it that way I am less likely to make mistakes.

As you look at more code written by more people you'll see things you like and copy and things you don't and will reject, often from the same person. So long as your code is sound and your style is not barbarous then people will be so happy to see you throw another requirement document into the done pile and reach for more work to do that whether you have done it their preferred way will not be a big issue.
Was This Post Helpful? 1
  • +
  • -

#14 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 451
  • View blog
  • Posts: 855
  • Joined: 17-March 11

Re: What should i really use for input/output on c/c++?

Posted 27 August 2011 - 05:03 AM

View PostLuiz herege, on 26 August 2011 - 06:39 AM, said:

By non standard you mean doing my own functions? I was really only curious about how should i start thinking about that.


By non-standard I mean there are a lot of other ways to handle input/output. For example functions which provide more finegrained control like reading characters as they are typed or input/output that works totally different, like a gui. But using this functionality means that your code will not automagicaly work on any standard complying c++ compiler or platform. You should look at the the documentation for the input/output facility itself to know where and under which circumstances it will work.

For example, you will see the getch function a lot, but it really only works when you are compiling on windows.
Was This Post Helpful? 1
  • +
  • -

#15 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Re: What should i really use for input/output on c/c++?

Posted 28 August 2011 - 02:47 AM

In C++

std::cin for input and std::cout for output from iostream

In C

scanf() for input and printf() for output from stdio.h
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1