A little one...

S

Sava Mikalaèki

Hi to all of you god programmers in the world!

One question:

When I want to load a value in some variable (ex. int x) I use cin << x. Ok,
but if I want to use cin.get() what would it be? I'we tryed something like
this:

#include <iostream>

using namespace std;

int main()
{
int x;
cout << "Enter a nuber: ";
x=cin.get();
return 0;
}

But, when I compile it and run it when I enter a value (for example, 3) and
write it
on the screen i get number 6. (or 5 it prints out 8). It's clearly that I'm
doing
something wrong but what?

Regards,
SaVa
 
J

Jeff Schwab

Sava said:
Hi to all of you god programmers in the world!

One question:

When I want to load a value in some variable (ex. int x) I use cin << x.

/* ITYM */ cin >> x;
Ok,
but if I want to use cin.get() what would it be? I'we tryed something like
this:

#include <iostream>

#include said:
using namespace std;

int main()
{
int x;

You don't have to declare the variable here. It's probably better to
wait until you're ready to initialize it.
cout << "Enter a nuber: ";

Do you mean a number? Did you mean to flush the stream? I think you mean:

cout << "Enter a number: " << flush;
x=cin.get();

This returns the next character from the input stream. Don't be fooled
by cin.get()'s return type; the integer assigned to x is whatever number
in your local character set represents the next character in the stream.
If you want to see the character returned, make x a char instead of an
int:

char x = cin.get( );
return 0;

Just FYI: you don't have to return 0 explicitly. It will be done for
you. You still need to write "int main()", as you did in this example.

Hth,
Jeff
 
T

Thomas Matthews

Sava said:
Hi to all of you god programmers in the world!

One question:

When I want to load a value in some variable (ex. int x) I use cin << x. Ok,
but if I want to use cin.get() what would it be? I'we tryed something like
this:

#include <iostream>

using namespace std;

int main()
{
int x;
cout << "Enter a nuber: ";
x=cin.get();
return 0;
}

But, when I compile it and run it when I enter a value (for example, 3) and
write it
on the screen i get number 6. (or 5 it prints out 8). It's clearly that I'm
doing
something wrong but what?

Regards,
SaVa
The cin.get() function will return a _character_. To use cin.get(), you
will have to translate characters into a number.
#include <iostream>
#include <locales>
#include <cstdlib>

using namespace std;

int main(void)
{
int x;
char c;
cout << "Enter a number: ";
cout.flush(); // Make sure O/S prints all characters.
x = 0;
while ((cin.getc(c)) && isdigit(c))
{
x = x * 10 + c - '0'; // Assumes '0'..'9' is contiguous.
}
cout << "\nNumber is: " << x;
return EXIT_SUCCESS;
}

The above program does not account for negative numbers nor
does it perform any range checking.

Alternative methods are:
1. Use getline() and convert to a number via stringstream.
2. Read into a character array and use sscanf().
3. Read into a character array and use strtol().

Most people will use the "cin >> x" for reading in numbers
since it is simpler.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top