Thread: Range

  1. #1
    Unregistered
    Guest

    Thumbs down Range

    Hi,

    Can any one please let me know where I am going wrong??I am trying this for past 2 hrs ,but .......

    the problem is accept the date of Birth seprately dd,mm yyyy
    but the age should be btw 15 to 70 yrs on 31/12/2001. if its outside the range error message should be displayed.....Again there it should check whether its a leap yr or not......
    Though i havent given a thought about a leap yr ......pls give me hints what should I do??

    Thanks


    Code:
    cout << "Input Year  : ";
    aYear = readLine();
    year= atoi(aYear.c_str());
    
    if(year>1900 || year <2002)
      {
          validAge(year); //function
          cout<<"Age not within range"<<endl;
         cout<<"data not added not database"<<endl;
    			
      }
    
    void validAge(const int aYear)
    {
    	 
    	 const int mini= 1986;
    	 const int max= 1931;
    
    
    	if (aYear>mini)
    	{
    		
    		cout<<" too young"<<endl;
    	}
    	else if(aYear<max )
    	{
    		cout<<"too young"<<endl;
    	}
    
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    aYear = readLine();
    ...don't you mean: aYear.readLine(); ?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That is, cin.readLine();
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Unregistered
    Guest

    Question Help

    Hi

    readLine is a function,that we have been told to use......
    Code:
    string readLine()
    {
        char buffer[SIZE];
    	cin.getline(buffer, SIZE, '\n');
    	return buffer;
    }
    that funcn is not creating any problem,I am sure I am missing soemthing on the logic.......Pls help......
    I just dont know how tell validate the date btw 15 yrs and 70 yrs..
    also to check about the leap year ......

    thanks

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The problem is, you create a variable, buff, within the function, and then try to return it. This is not good. The variable will be destroyed at any time after leaving the function! The solution is to allocate with "new". Also, you show the function to be returning a string, but instead it returns a char[]. That won't work.

    Try:

    Code:
    string readLine()
    {
        string value = new string; //...allocate memory...
    
        char buffer[SIZE];
    
        cin.getline(buffer, SIZE, '\n');
    
        value = buffer; //...operator overload copies contents to "value"... 
    
        return value;
    }

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, validAge is directed to print "too young" regardless of the condition! And you should return a value for this function...



    Code:
    ...
    int valid;
    
    if(year>1900 || year <2002)
      {
          valid = validAge(year); //function
          
          if(valid == 0)
           {
             cout<<"Age not within range"<<endl;
             cout<<"data not added not database"<<endl;
            }		
      }
    
    ...
    
    
    
    int validAge(const int aYear)
    {
    	 
    const int mini= 1986;
    const int max= 1931;
    
    
     if (aYear>mini)
     {		
       cout<<" too young"<<endl;
       return 0;
      }
     else
     if(aYear<max )
     {
     cout<<"too old"<<endl; //cout<<"too young"<<endl;
     return 0;
     }
    return 1;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Unregistered
    Guest

    Thumbs up Thanks

    Thanks a lot for pointing out the flaws in the code....
    Pls. can u give me some idea about ,how to check the leap year....
    in the funcn........Thanks once again.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362

    Leap Year

    Because of the range of years that you're using, if the year is evenly divisible by four, it's a leap year. This would not be sufficient criteria for a much greater range of years but will suffice in this case.


    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. Srand () w/ range
    By xsbinary in forum C Programming
    Replies: 9
    Last Post: 10-21-2007, 03:24 PM
  4. Please help to check.
    By nicoleha in forum C Programming
    Replies: 16
    Last Post: 12-07-2005, 03:29 PM
  5. Random Numbers within a range OTHER than 1-X
    By Kaelin in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2005, 11:57 AM