converting a char to an int

This is a discussion on converting a char to an int within the C++ Programming forums, part of the General Programming Boards category; ok, i just got the time from the computer and i want to turn "Fri Apr 26 09:47:43 2002" which ...

  1. #1
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    converting a char to an int

    ok, i just got the time from the computer and i want to turn
    "Fri Apr 26 09:47:43 2002" which is what the char holds into an integer. Basically i want to take the 09:47:43, turn it into 094743, and put it in an integer so i can later use that number and compare it with others... thanks

  2. #2
    Unregistered
    Guest
    what are you using to get the time string. To do the task you want would be easiest using the tm struct often associated with time functions from the time.h file. If you want you can parse the string for the information, but that isn't a trivial process either. To do it, use strtok() with space and colon as the delimiters. This should break up the string into seven substrings. You can then concatenate substrings 4, 5 and 6 together and send the result to atol(); assigning the result to a variable of type long.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It will probably be faster to use the tm struct, as unregistered said. See function localtime() to use it. But if you wish:
    Code:
    #include <stdlib.h>
    #include <time.h>
    #include <cstring>
    
    using namespace std;
    
    int main(void)
    {
       char date_time[26] = "Fri Apr 26 09:47:43 2002";
       char time_string[7] = "";
       int t;
    
       strncat(time_string,date_time+11,2);
       strncat(time_string,date_time+14,2);
       strncat(time_string,date_time+17,2);
       t = atoi(time_string);
       cout << "time:" << time_string << endl;
       cout << "time:" << t << endl;
       return 0;
    }
    Just make sure an int is 4 bytes on your machine, or you will need a long instead of an int (use atol() instead of atoi()).
    Last edited by swoopy; 04-26-2002 at 04:50 PM.

  4. #4
    Registered User JTtheCPPgod's Avatar
    Join Date
    Dec 2001
    Posts
    44
    The tm struct...
    well heat and I are inn the same class, and working on the same project too. I'm convinced that using this struct will be easier, but I can't quite get it to work (which somewhat defeats my theory that it'll be easier)
    Anyway, here's my code:
    Code:
    #include<time.h>
    #include<iostream.h>
    #include<windows.h>
    
    const int x = 2;
    //time_t now; 
    //time_t *pNow; 
    //now = time(pNow); 
    time_t *pnow; 
    
    int main()
    {
    
    	tm tNow = localtime(pnow); 
    
    		//time_t hold_time;
    		//hold_time = time(NULL);
    		//cout<<"The date is: "<< ctime(&hold_time) << endl;
    	cout << tNow.tm_hour << ':' << tNow.tm_min << ':' << tNow.tm_sec << endl; 
    	//cout << tm.tm_hour << ':' << tm.tm_min << ':' << tm.tm_sec << endl;
    
      return 0;
    }
    yea, a buncha stuff is commented out cos I'm totally guessing as to how this works....
    If anyone would be kind enough to make this work and/or help me write one that works in MSVCPP on windows 98.
    "No! I must have my delicious cupcakes, my sweet cakey treasures, piping hot from their 40 watt WOMB!!!"
    --Captain Murphy

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    time is the variable for the real time here:
    Code:
    char time;
    int times;
    times = (int)time;
    ofstream fout;
    fout.open("C:\Save\file\goesh.ere" | ios::append);
    fout<<times;
    fout.close();
    i think the ios::append might be wrong, but i was thinking that was the command to add to theend of a file. is that right?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include<time.h>
    #include<iostream.h>
    
    int main()
    {
       time_t hold_time;
       hold_time = time(NULL);
       cout<<"The date is: "<< ctime(&hold_time) << endl;
       tm *tNow = localtime(&hold_time);
    
       cout << tNow->tm_hour << ':' << tNow->tm_min << ':' << tNow->tm_sec << endl;
       //cout << tm.tm_hour << ':' << tm.tm_min << ':' << tm.tm_sec << endl;
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21