Like Tree1Likes
  • 1 Post By Elysia

how to copy this string in int

This is a discussion on how to copy this string in int within the C++ Programming forums, part of the General Programming Boards category; I m taking input from user in this format(string) DD/MM/YYYY i want to copy DD and MM in seperate int ...

  1. #1
    Registered User
    Join Date
    Nov 2011

    how to copy this string in int

    I m taking input from user in this format(string)


    i want to copy DD and MM in seperate int variable... how can i do that??

  2. #2
    Registered User
    Join Date
    Oct 2006
    use the >> operator of std::istream to get the day, then a slash, then the month, then another slash, then the year.

  3. #3
    Registered User
    Join Date
    Nov 2011
    not working

    explain it plz

    cout << "Enter your Date of Birth OR 0 to exit\n(Format: DD/MM/YYYY e.g: 11/09/1984): ";
            arr >> date >> "/" >> month ;

  4. #4
    Join Date
    Oct 2007
    More like

    int day, month, year;
    char tmp;
    cout << "Enter your Date of Birth OR 0 to exit\n(Format: DD/MM/YYYY e.g: 11/09/1984): ";
    cin >> day >> tmp >> month >> tmp >> year;

    cin >> date will read all digits until it hits a non-digit (ie, /).
    >> tmp will then read one character (ie, the /).
    This then repeats.
    Last edited by Elysia; 02-03-2012 at 05:43 AM. Reason: Typo
    Elkvis likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    If you've already read a string and you want to pick it apart to find the date components, you can do that too. In fact it's probably a better idea in case the user gives invalid input; if they don't enter a number when you're expecting a number, for example, the stream will enter an error state and you'll probably have an infinite loop unless you clear the error state. Much easier to just read a line, try parsing it, and if that doesn't work then grab another line.
    #include <iostream>
    #include <sstream>  // for std::istringstream
    std::cout << "Enter date in format DD/MM/YYYY:\n";
    std::string line;
    std::getline(std::cin, line);
    std::istringstream stream(line);
    int day, month, year;
    char delim;
    if(stream >> day >> delim >> month >> delim >> year) {
        // success
    else {
        // failure, get another line
    You can also try calling e.g. stream.get() once you've read all you need to see if the user typed extra garbage at the end of the line (if you don't get EOF, they typed garbage).

    Be warned! By default all of C++'s number parsing functions will treat a number beginning with a zero as octal. Hence "055" is really 45 in decimal, and "09" will result in an error since "9" isn't a valid octal digit. I suspect you can ask for just decimal parsing, check the functions in <iomanip> or google it. Alternatively, you could use an old C function called strtol() which lets you specify exactly which base you want to use (0 is the default, meaning 10 or octal or hex depending on what it looks like, and you can just say "10" if you definitely want decimal).

    [edit] Also if you want to make sure the string has precisely two digits for the day etc you can look through the string yourself, call std::isdigit() to see if some chars are digits and compare the rest to '/'. Make sure the string is long enough. [/edit]

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    May 2011
    Around 8.3 light-minutes from the Sun
    Stream processing is the way to go with C++. Here is a simple example:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    //just used to create the file we are going to read from
    void createFile(void);
    int main(void){
    	//our string to hold our line from file and our temp
    	std::string myLine, temp;
    	//our string stream object to parse myLine
    	std::stringstream myWord;
    	//call our function to make our file
    	//create our file for reading
    	std::fstream myFile("example.txt", std::ios::in);
    	//Ensure our file is open
    		//Loop through our file until EOF
    		while(std::getline(myFile, myLine)){
    			std::cout << std::endl << "Our line from file: " << myLine << std::endl;
    			std::cout << "Our parsed line: " << std::endl;
    			//Copy our line to parse
    			//break up our string using spaces as delimiter
    			while(myWord >> temp){
    				std::cout<< temp << std::endl;
    			//reset our object
    		//close our file
    	return (0);
    void createFile(){
    	//open our file for writing
    	std::fstream myFile("example.txt", std::ios::out);
    	//ensure our file is open and write to it
    		myFile << "First line of text" << std::endl;
    		myFile << "Second line of text" << std::endl;
    	//close our file
    Last edited by AndrewHunter; 02-05-2012 at 02:34 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-10-2011, 07:47 PM
  2. string copy
    By yangss in forum C Programming
    Replies: 4
    Last Post: 09-02-2010, 06:19 AM
  3. Copy a string
    By thescratchy in forum C Programming
    Replies: 5
    Last Post: 07-28-2010, 03:26 PM
  4. Replies: 1
    Last Post: 12-10-2008, 10:29 AM
  5. string copy
    By SuperNewbie in forum Windows Programming
    Replies: 1
    Last Post: 01-16-2004, 06:59 AM

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