Thursday, July 30, 2009

I need help in C++ returning 3 different values in one input line?

basically, i want this person to tell me the expiration date of a food product and they have to enter it in the form of mm/dd/yyyy





then i want to return to them the day they entered so the program would look like this:





Enter expiration date in the form mm/dd/yyyy: 3/19/2008


You said the expiration day was the: 19





below is what ive been trying to put into visual studio, but it wont work, im not sure how exactly to have an input in the form of mm/dd/yyyy and then be able to store the month 'm' day 'd' and year 'y' as seperate variable, could you please correct what i have entered below to how it should look?








cout %26lt;%26lt; "Enter expiration date in the form mm/dd/yyyy: ";


cin %26gt;%26gt; m;


cin %26gt;%26gt; d;


cin %26gt;%26gt; y;


cout %26lt;%26lt; "You said that the expiration day was the" %26lt;%26lt; d %26lt;%26lt; endl;

I need help in C++ returning 3 different values in one input line?
It's all about string manipulation ...


You need to take a string from the input:


Using the C++ string library:


string str="";


getline(cin,str);


will read your string from the input once you press Enter.


So now we have a string:


Step 1: split the string by a delimiter "/"


Step 2: read the token that is in the middle


Step 3: Optionally convert it into number


One of the many things I love in C++ is the Vector;


So if we have a vector of strings and we make a synonym for that as:


typedef vector%26lt;string%26gt; vs;


We could create a function that takes the whole string and returns a vector of the tockens splitted by our delimiter as:


01 vs get_tokens (const string%26amp; s, const string%26amp; d)


02 {


03 vs v;


04 string::size_type pos,lpos;


05 lpos=s.find_first_not_of(d,0);


06 pos=s.find_first_of(d,lpos);


07 while(string::npos != pos || string::npos != lpos)


08 {


09 v.push_back(s.substr(lpos,pos-lpos));


10 lpos=s.find_first_not_of(d,pos);


11 pos=s.find_first_of(d,lpos);


12 }


13 return v;


14 }


One more thing we need to do; namely, convert a string into a number:


We need to #include %26lt;sstream%26gt;:


template %26lt;typename T%26gt; static T to_number (string str)


{


T num;


istringstream str_s(str);


str_s %26gt;%26gt; num;


return num;


}


This function will overcome the previous two posts in such it will not matter whether you typpe "03" or "3"; in both cases, the string will be converted into a numeric value as 3, so the leading zero will be skipped.


Now we have all what we need to create simple code:


string str="",del="/";


getline(cin, str);


vs v=get_tokens(str,del);


int exp_date=to_number%26lt;int%26gt;(v[1]);


Enjoy





psst:


There seems to be bugs in Yahoo about some lines of code ending with ...


Line 09: replace the three dots (...) with:


lpos));


Line 10: Replace the three dots (...) with:


);
Reply:EDIT:


I had a bunch of code then lost power so here is an abbreviated version. Im guessing that both month and day can be 1 or 2. So you will want to find where '/' occurs using substring. You can either store the length of each section or the indexes of '/'.





If you find the index of each '/' the substrings would look like


substr(0, index1);


substr(index1+1, index2-index1);


substr(index2+1, 4);





========================





cin reads until it hits whitespace. Since there is no whitespace betweent he numbers it doesnt know what to do.





You will have to either read in the whole line as a string and break it into pieces or read in each character and build as you go.





You can use cin.get() or cin.getline()


You can use atoi() to convert from letters to numbers.
Reply:Your code won't work. Let's analyze the inputs first.





It is something like 03/18/2008. What variable type is this? It's a string. Ok, yes, I can see that there are three numbers in there. But it's all combined with slashes into a giant string, right? So when you use cin, you should be taking the input into a string. Not a number.





#include %26lt;string%26gt;


...


string s;


cin %26gt;%26gt; s;





Ok, now we have a string, but we want the date portion of it. There's a really easy way to do this with C++ strings. There's a function called substr (http://cppreference.com/cppstring/substr... ) which is to substring. You need to have the index and optionally the length. Well, if you understand arrays, you can see why the index for the date starts at 3. There's the 03/ which occupies indicides 0, 1 , and 2. Then for the 18, the 1 starts at index 3. We want 18, which is two numbers, so that's a count of 2.





If you want to show that part, you just need to do:


cout %26lt;%26lt; s.substr(3,2) %26lt;%26lt; endl;





If you actually want to store the date portion as a separate variable, you'll want to use stringstreams.





#include %26lt;string%26gt;


#include %26lt;sstream%26gt;


...


string s;


int i;


cin %26gt;%26gt; s; //the 03/18/2008 part.


stringstream ss(s.substr(3,2)); //select out the 18 portion for stringstreams.


ss %26gt;%26gt; i; //and have the 18 portion actually be put into an integer var





EDIT:





Yes, there is a way you can deal with 2/02 variation for the month. That, is consider the length of the string.





02/18/2008 : length of 10.


2/18/2008: length of 9.





If it's 02, or a length of 10, you know to substr (3,2). Right? But if it's 2, or a length of 9, you have to substr (2,2).





You can use the length property of strings to determine the length.


int index = 3;


if (s.length() == 9)


{


index =2;


}


s.substr(index,2); //don't hardcode the index. Instead use the variable


No comments:

Post a Comment