Requesting for simple C++ code to solve the following(I'm new learner):

Discussion in 'Programming' started by Reny, Aug 6, 2006.

  1. #1
    Hello, i'm a new learner for Cpp just only one week and currently using JFE and GCC( i was forced to).
    Here's my following problem:

    1)In a program, user is required to enter a five-number digit. So i try this method so that the program only read a FIVE-DIGIT number and keep looping if it's NOT a FIVE-DIGIT number.

    while (num < 10000 && num > 99999)
    {
    cout << "Please enter a five-digit number:\n";
    cin >> num;
    cout << endl;

    Somehow i failed.
    is there any simple code for me to slove it? I'm still an ameture. Thx for the concern.

    p/s: greatly appreciate if someone show me how to do underline in a line of words:)
     
    Reny, Aug 6, 2006 IP
  2. giraph

    giraph Guest

    Messages:
    484
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #2
    well, let's have a look at this. How can a number be..
    num < 10000 && num > 99999

    && means AND, you will want to use || which means OR.

    So while the number is less than 10000 OR greater than 99999, keep asking for input

    Here is a simple example
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	int n = 0;
    	while(n < 10000 || n > 99999)
    	{
    		cin >> n;
    	}
    	cout << n << " is 5 digits.";
    	cout << endl; 
    	return 0;
    }
    
    Code (markup):
     
    giraph, Aug 6, 2006 IP
  3. rahulm

    rahulm Peon

    Messages:
    178
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thats right there should be a OR operator...........
     
    rahulm, Aug 7, 2006 IP
  4. HanSolo42

    HanSolo42 Peon

    Messages:
    106
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ISO C does provide an or macro :-P
     
    HanSolo42, Aug 9, 2006 IP