473,378 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

A little one...

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
Jul 22 '05 #1
2 1202
Sava Mikalački wrote:
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 <ostream> // For std::flush (see below).

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
}

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


Jul 22 '05 #2
Sava Mikalački wrote:
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

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ron Stephens | last post by:
I posted to my web site a fun little program called merlin.py today. Please keep in mind that I am a hobbyist and this is just a little hack, if you look at the code you will see that it is still...
2
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working...
18
by: Philipp Lenssen | last post by:
I want to write a third installment of "Little Known HTML Facts"*. I would appreciate your input here. For one thing, I would like to remember what exactly those proprietary icons were you could...
3
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
2
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
13
by: junky_fellow | last post by:
Hi guys, I need to convert a big endian integer to little endian integer. (the integer is 4 bytes in size on my implementation). I came up with the following code. I need your comments on...
102
by: BoogieWithStu22 | last post by:
I am running into a problem with a web page I have created when viewing it in IE6 on some machines. The page has a database lookup. The user enters an account name or number and clicks a lookup...
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
23
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.