I m taking input from user in this format(string)
DD/MM/YYYY
i want to copy DD and MM in seperate int variable... how can i do that??
I m taking input from user in this format(string)
DD/MM/YYYY
i want to copy DD and MM in seperate int variable... how can i do that??
use the >> operator of std::istream to get the day, then a slash, then the month, then another slash, then the year.
not working
explain it plz
Code:cout << "Enter your Date of Birth OR 0 to exit\n(Format: DD/MM/YYYY e.g: 11/09/1984): "; getline(cin,arr); arr >> date >> "/" >> month ;
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.
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.
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).Code:#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 }
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]
dwk
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: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Stream processing is the way to go with C++. Here is a simple example:
Code:#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 createFile(); //create our file for reading std::fstream myFile("example.txt", std::ios::in); //Ensure our file is open if(myFile.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 myWord.str(myLine); //break up our string using spaces as delimiter while(myWord >> temp){ std::cout<< temp << std::endl; } //reset our object myWord.clear(); } //close our file myFile.close(); } std::cin.get(); 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 if(myFile.is_open()){ myFile << "First line of text" << std::endl; myFile << "Second line of text" << std::endl; } //close our file myFile.close(); }