Thread: Logical error: I'm not getting expected result

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    51

    Logical error: I'm not getting expected result

    Please check the code and output.
    I've checked again and again but could not sort out the flaw.
    Kindly help please!
    Logical error: I'm not getting expected result-noon-png
    Code:
    #include<iostream>
    #include<cstdlib>
    
    
    using namespace std;
    
    
    class Time{
    	
    	private:
    	
    		int hours;		
    		int minutes;	
    		int seconds;	
    	
    	public:
    	
    		Time( int h, int m, int s ){				
    		setTime(h, m, s);					
    		}
    		
    		void setTime(int h, int m, int s){
    			setHours(h);			
    			setMinutes(m);			
    			setSeconds(s);			
    		}
    		
    		void setHours(int h){
    			hours = (h >= 0 && h < 24) ? h : 0 ;
    		}
    		
    		void setMinutes(int m){
    			minutes = (m >= 0 && m < 60) ? m : 0 ;
    		}
    		
    		void setSeconds(int s){
    			hours = (s >= 0 && s < 24) ? s : 0 ;
    		}
    		
    		
    		int getHours() const{
    			return hours;
    		}
    		
    		int getMinutes() const{
    			return minutes;
    		}
    		
    		int getSeconds() const{
    			return seconds;
    		}
    		
    		void printTime() const{
    			cout << hours << ":" << minutes << ":" << seconds << endl;
    		}
    		
    		~Time() {					
    		
    		}
    };
    
    
    int main(){
    	
    	Time wakeUp(6, 45, 0);		
    	const Time noon(12, 0, 0);			
    	wakeUp.printTime();							
    	noon.printTime();								
    	system("pause");
    	return 0;
    	
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > hours = (s >= 0 && s < 24) ? s : 0 ;
    Try setting seconds.
    Try with 60 seconds.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by Salem View Post
    > hours = (s >= 0 && s < 24) ? s : 0 ;
    Try setting seconds.
    Try with 60 seconds.
    Still same issue

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by rm82co View Post
    Still same issue
    Well... What Salem said is that your code:
    Code:
    void setSeconds(int s){ hours = (s >= 0 && s < 24) ? s : 0; }
    should be:
    Code:
    void setSeconds(int s){ seconds = (s >= 0 && s < 60) ? s : 0; }

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by flp1969 View Post
    Well... What Salem said is that your code:
    Code:
    void setSeconds(int s){ hours = (s >= 0 && s < 24) ? s : 0; }
    should be:
    Code:
    void setSeconds(int s){ seconds = (s >= 0 && s < 60) ? s : 0; }
    Yup I have done it in this way. Still same issue.
    Last edited by rm82co; 07-03-2019 at 01:59 PM.

  6. #6
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Code:
    #include<iostream>
    #include<cstdlib>
    
    
    using namespace std;
    
    
    class Time{
    	
    	private:
    	
    		int hours;		
    		int minutes;	
    		int seconds;	
    	
    	public:
    	
    		Time( int h, int m, int s ){				
    		setTime(h, m, s);					
    		}
    		
    	
    		
    		void setTime(int h, int m, int s){
    			setHours(h);			
    			setMinutes(m);			
    			setSeconds(s);			
    		}
    		
    		void setHours(int h){
    			hours = (h >= 0 && h < 24) ? h : 0 ;
    			cout << "hours: " << hours << endl;
    			
    		}
    		
    		void setMinutes(int m){
    			minutes = (m >= 0 && m < 60) ? m : 0 ;
    			cout << "minutes: " << minutes << endl;
    		}
    		
    		void setSeconds(int s){
    			hours = (s >= 0 && s < 60) ? s : 0 ;
    			cout << "seconds: " << seconds << endl;
    		}
    		
    		
    		
    		int getHours() const{
    			
    			return hours;
    		}
    		
    		int getMinutes() const{
    			return minutes;
    		}
    		
    		int getSeconds() const{
    			return seconds;
    		}
    		
    		
    		
    		void printTime() const{
    			cout << hours << ":" << minutes << ":" << seconds << endl;
    		}
    		
    			
    		}
    };
    
    
    int main(){
    	
    	const Time wakeUp(6, 45, 0);		
    	const Time noon(12, 0, 0);			
    										
    									
    	wakeUp.printTime();					
    	noon.printTime();			
    	system("pause");
    	return 0;
    	
    }
    I have modified code to find errors. Please check and guide me where is the problem. I am still not being able to find

  7. #7
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by rm82co View Post
    Yup I have done it in this way. Still same issue.
    I have done Sir, thank you very much. It is working now.

    Thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2016, 02:12 PM
  2. Replies: 9
    Last Post: 11-28-2016, 05:51 PM
  3. serious logical error!
    By Mukul Kumar in forum C++ Programming
    Replies: 12
    Last Post: 11-18-2013, 07:12 PM
  4. 3DES ECB MODE IN OPENSSL Can't get expected Result
    By homoon in forum C Programming
    Replies: 11
    Last Post: 05-21-2012, 02:32 PM
  5. Logical Error
    By Luigi1 in forum C Programming
    Replies: 1
    Last Post: 02-27-2011, 02:41 PM

Tags for this Thread