I/O related

hi all,

my question is
How to read and print one character(including spaces) at a time from the keyboard?

and

What is an invariant?

Thank You :)
read a single character:
http://www.cplusplus.com/reference/iostream/istream/get/
write a single character:
http://www.cplusplus.com/reference/iostream/ostream/put/

1
2
3
char c;
std::cin.get(c);
std::cout.pu(c);


An Invariant of an object is something that is true before you manipulate the object and after you manipulate the object.
Example:
You have a class car like this:
1
2
3
4
5
6
7
8
9
10
class Car
{
public:
  Car(Engine* e) : _e(e) {assert(e != 0);};
  void startEngine() {_e->start();};
  void removeEngine() {_e = 0;};
private:
  Car(); // prevent creation of Cars without engine
  Engine* _e;
};

The constructor establishes the invariant "a Car has an engine".
The method "startEngine" relies on this invariant.
The method "removeEngine" violates the invariant. That's why a subsequent call to "startEngine" will fail!
thanks for replying :)

i understood the code about the get() and put().

but am unable to understand the code of invariant.
i understood what an invariant is.
Invariant is a specific rule that an object has to satisfy in order to be created or manipulated.
1
2
3
4
5
6
7
8
9
10
11
#include<iostream.h>
#include<conio.h>
int main()
{
char x;
cout<<"Enter a character"<<endl;
cin.get(x);
cout.put(x);
getch();
return 0;
}
i understood the invariant code too.
if e=0 then the car doesn't have an engine and the car won't start.
Thank you very much..
this might help with invariant

http://tinyurl.com/6aeqkqa
thank you ceruleus for the google link.
fool, did u think that i didn't google stuff before asking here?
do u think that i don't have any other work or i feel gud to waste time asking stupid questions?

My best regards.
don't take it personally... every forum needs a troll!
yeah! we have u :p
Topic archived. No new replies allowed.