GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Jan-2009, 13:34
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

How to validate the number of inputted digits?


My program has a user input of 8 digits. But, I'm suppose to provide a error if the input falls short or to many of the 8 digits. How can I do this?

Relation Operators??
  #2  
Old 23-Jan-2009, 14:01
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: How to vailidate the number of inputted digits?


Quote:
Originally Posted by 8100 Power
But, I'm suppose to provide a error if the input falls short or to many of the 8 digits. How can I do this?
One way is to set up input as a string & count the number of characters which have been received from the user. If you ultimately need the value to be in a numeric format, convert it to a binary value via atoi() after you have determined that the correct number of digits has been entered.
  #3  
Old 23-Jan-2009, 17:28
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: How to vailidate the number of inputted digits?


Quote:
Originally Posted by ocicat
One way is to set up input as a string & count the number of characters which have been received from the user. If you ultimately need the value to be in a numeric format, convert it to a binary value via atoi() after you have determined that the correct number of digits has been entered.

I though of using a string, but it's not allowed at this time.

  #4  
Old 23-Jan-2009, 17:33
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: How to vailidate the number of inputted digits?


Quote:
Originally Posted by 8100 Power
I though of using a string, but it's not allowed at this time.
Okay, if input is restricted to numeric values, you will need to figure out what numerical range contains the appropriate number of digits.
  #5  
Old 23-Jan-2009, 20:26
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: How to vailidate the number of inputted digits?


Quote:
Originally Posted by ocicat
Okay, if input is restricted to numeric values, you will need to figure out what numerical range contains the appropriate number of digits.

It actually can be any 8 digits, that's my problem.

Anything else?
  #6  
Old 23-Jan-2009, 22:53
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: How to vailidate the number of inputted digits?


Quote:
Originally Posted by 8100 Power
It actually can be any 8 digits, that's my problem.
  • So, what is the smallest number that can be represented in eight digits?
  • What's the largest?
  • Assuming a minus sign is not considered appropriate, this should give you further information on what subset of values are correct.
Once you can answer these questions, you know the valid range of values allowed, & you could check input for membership accordingly.

If leading zeroes are allowable, then you need to make some decisions/adjustments:
  • If the user is allowed to enter less than eight digits, do you assume that the omitted digits are leading zeroes?
  • If leading zeroes are allowed, be prepared to deal with numerical values expressed in octal (base 8 ) as input. I have a suspicion your instructor is wanting you to recognize this fact.
It appears that you need to clarify this situation with your instructor, & write code which implements his/her intent. Since none of us here are (most likely) in your class, no one here will be able to give you a definitive answer.
  #7  
Old 24-Jan-2009, 20:35
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: How to vailidate the number of inputted digits?


Quote:
Originally Posted by 8100 Power
It actually can be any 8 digits, that's my problem.

Anything else?

CPP / C++ / C Code:
#include <iostream>

bool is_exactly_8_digits(const int value)
{
    bool result = false;   
    int tmp = value;

    if(tmp < 0)
    {
	tmp = -tmp;
    }
    if(tmp > 9999999 && tmp < 100000000)
    {
	result = true;
    }
    return result;
}

int main()
{
    std::cout << "Enter an integer value containing 8 digits: ";
    int value;
    std::cin >> value;
    if(is_exactly_8_digits(value))
    {
	std::cout << "You successfully entered " << value << " which does contain exactly 8 digits." << std::endl;
    }
    else
    {
	std::cout << value << " does not contain exactly 8 digits." << std::endl;
    }
    return 0;
}




MxB
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to show a 3 digit number with separate digits showing fomi101 C++ Forum 9 29-Sep-2007 14:50
Reverse a number or digits tootoo C++ Forum 6 29-Jul-2007 21:34
Converting a number amount to text Godzilla C++ Forum 5 31-Mar-2006 12:38
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 21:25
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 07:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:06.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.