Thread: Program Not working Right

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Angry Program Not working Right

    I am taking a Computer Science Class, where we are learning C++. The assignment I am working on is here: http://home.earthlink.net/~craie/121...ay.simple.html

    The problem I am having is that the output of my program is not working correctly at my conversion to CST and after. Can someone please look at my code and explain why, or give me a good enough example/hint as of why.

    Code:
    /* Computer Science 1
       Name: edit
       Email: edit
    */
    
    
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
    	short hour_gmt, hour_cst, min, sec;
    	const short sec_hour = 3600, sec_min = 60, cst_conv = -5;
    	time_t seconds;
    	seconds = time (NULL);	
    	
    	hour_gmt = (seconds/sec_hour)%24;
    	min = (seconds/sec_min)%60;
    	sec =  seconds%60;
    
    	cout << endl;
    	cout << "The current time in GMT is " << hour_gmt << ":";
    	if (min < 10)
    		cout << "0" << min << ":";
    	else 
    		cout << min << ":";
    	if (sec < 10)
    		cout << "0" << sec << ".";
    	else 
    		cout << sec << ".";
    
    // American Standard Time format AM/PM
    	cout << " (";
    	if (hour_gmt > 12)
    		cout << hour_gmt - 12 << ":";
    	if (hour_gmt < 1) 
    		cout << "12:";
    	else
    		cout << hour_gmt << ":";
    	if (min < 10)
    		cout << "0" << min << ":";
    	else 
    		cout << min << ":";
    	if (sec < 10)
    		cout << "0" << sec;
    	else 
    		cout << sec;
    	if (hour_gmt > 12)
    		cout << " PM.)";
    	if (hour_gmt = 0)
    		cout << "AM.)";
    	else 
    		cout << " AM.)\n";
    	
    // Convert to Central Time
    	cout << "The current time CST (Central Standard Time) is ";
    	if (hour_gmt + cst_conv < 1)
    		hour_cst = hour_gmt+24+cst_conv;
    	else
    		hour_cst = hour_gmt + cst_conv;
    	cout << hour_cst << ":";
    	if (min < 10)
    		cout << "0" << min << ":";
    	else 
    		cout << min << ":";
    	if (sec < 10)
    		cout << "0" << sec << ".";
    	else 
    		cout << sec << ".";
    
    // CST American Standard Time format AM/PM
    	cout << " (";
    	if (hour_cst > 12)
    		cout << hour_gmt-12 << ":";
    	if (hour_cst < 1) 
    		cout << "12:";
    	else
    		cout << hour_cst << ":";
    	if (min < 10)
    		cout << "0" << min << ":";
    	else 
    		cout << min << ":";
    	if (sec < 10)
    		cout << "0" << sec;
    	else 
    		cout << sec;
    	if (hour_gmt > 12)
    		cout << " PM.)";
    	if (hour_gmt = 0)
    		cout << "AM.)";
    	else 
    		cout << " AM.)";
    		
         return 0;
    }
    Last edited by raven420smoke; 09-15-2005 at 09:48 PM.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Everywhere you see if (hour_gmt = 0) is a problem. You need to use == for comparison, = is assignment. The result of the above statement is that hour_gmt is set to 0, and the value 0 is passed to the if test. In short, it will always be false, and hour_gmt will always end up reset to 0, if you use = instead of == by accident.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    Thanks for the help. Its amazing how that one little thing threw off 3/4 of my program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM