By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,728 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

length of integer or numeric values

P: 11
Hi frds,

As i am getting an input value of integer (for ex: 1.413) and i want to limit the number of characters in the input value to 5

for ex:

1.413
14.13
141.3

Expand|Select|Wrap|Line Numbers
  1. double y;
  2. cout<<"Enter the new value of displacement: ";
  3. cin>>y;

the ouput

Enter the new value of displacement:_ _ _ _ _


it shud not accept the input more than 5 characters

if it is a string i can limit by using length () or size() function

i cant convert them as a string and use it further

wat can i do?
Jun 1 '07 #1
Share this Question
Share on Google+
9 Replies


Expert 100+
P: 181
Hi frds,

As i am getting an input value of integer (for ex: 1.413) and i want to limit the number of characters in the input value to 5

for ex:

1.413
14.13
141.3

Expand|Select|Wrap|Line Numbers
  1. double y;
  2. cout<<"Enter the new value of displacement: ";
  3. cin>>y;
use cin.getline() to take the input in a array then use strtof function to convert to float.


the ouput

Enter the new value of displacement:_ _ _ _ _


it shud not accept the input more than 5 characters

if it is a string i can limit by using length () or size() function

i cant convert them as a string and use it further

wat can i do?
use cin.getline() to get the input, using this you can limit the input.
then use strtof to convert to float
have a look at this
http://www.unet.univie.ac.at/aix/libs/basetrf1/atof.htm
Jun 1 '07 #2

P: 11
thx svlsr

ok i will get them as a string first

but i cant limit the character

Expand|Select|Wrap|Line Numbers
  1. string string1;
  2. cin.getline(string1,5);
it saying

no matching function for cin.getline

i hav also tried this

Expand|Select|Wrap|Line Numbers
  1. string string1;
  2. int z=5;
  3. cin.getline(string1,z);
i cant able to use any delimiter too as i am giving input
Jun 1 '07 #3

Savage
Expert 100+
P: 1,764
thx svlsr

ok i will get them as a string first

but i cant limit the character

Expand|Select|Wrap|Line Numbers
  1. string string1;
  2. cin.getline(string1,5);
it saying

no matching function for cin.getline

i hav also tried this

Expand|Select|Wrap|Line Numbers
  1. string string1;
  2. int z=5;
  3. cin.getline(string1,z);
i cant able to use any delimiter too as i am giving input
But why using unformmated input when u can use formated input.

e.g

Expand|Select|Wrap|Line Numbers
  1. string c;
  2.  
  3. cin.width(6) //must also count '\0' character
  4.  
  5. cin>>c;
Savage
Jun 1 '07 #4

P: 11
But why using unformmated input when u can use formated input.

e.g

Expand|Select|Wrap|Line Numbers
  1. string c;
  2.  
  3. cin.width(6) //must also count '\0' character
  4.  
  5. cin>>c;
Savage
thx savage

but while giving input(with width fuction) it accepts more than 6 characters

i want to limit the characters

that i shud not able to give input more than 6 characters
Jun 1 '07 #5

Savage
Expert 100+
P: 1,764
thx savage

but while giving input(with width fuction) it accepts more than 6 characters

i want to limit the characters

that i shud not able to give input more than 6 characters
U can input more then six characters but only six of them are stored.

And there is also a peek member of the cin that returns next input,so u can test it

Savage
Jun 1 '07 #6

P: 11
ya u r right !! savage

it is taking only 6 characters

thx a lot

till my next question bye!!

Happy weekend
Jun 1 '07 #7

Expert 100+
P: 181
thx svlsr

ok i will get them as a string first

but i cant limit the character

Expand|Select|Wrap|Line Numbers
  1. string string1;
  2. cin.getline(string1,5);
it saying

no matching function for cin.getline

i hav also tried this

Expand|Select|Wrap|Line Numbers
  1. string string1;
  2. int z=5;
  3. cin.getline(string1,z);
i cant able to use any delimiter too as i am giving input
getline doesnot take string as arguement
try some like this
char temp[6]
cin.getline(temp,6);
getline expects char * as first arguement.
If u want to use string you can use
string s;
cin.getline(s.c_str(),s.size());
Jun 1 '07 #8

Savage
Expert 100+
P: 1,764
ya u r right !! savage

it is taking only 6 characters

thx a lot

till my next question bye!!

Happy weekend
I'm more than happy to help u.

Savage
Jun 1 '07 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
This is not correct:

[/quote=svlsr2000]
getline expects char * as first arguement.
If u want to use string you can use
string s;
cin.getline(s.c_str(),s.size());
[/quote]

This will not compile. cin.getline() expects a char* argument. the c_str() method of string returns a representation of the string as a non-modifiable C-string, that is, a const char*.

You have to remember that a string does not contain a C-string.
Jun 1 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.