Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,928 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,837 people online right now. Registration is fast and FREE... Join Now!




C++ Simple Strings - Input and Output Date

 
Reply to this topicStart new topic

C++ Simple Strings - Input and Output Date

kidstechno
5 Dec, 2007 - 02:11 PM
Post #1

New D.I.C Head
*

Joined: 19 Nov, 2007
Posts: 4


My Contributions
I have an assignment due and I'm in my first semester of programming so its pretty basic stuff.
Our assignment is to write a function that accepts a date as a string parameter assumed to be in the form mm/dd/yyyy and outputs in the form
Day: dd
Month: mm
Year: yyyy

Here's my code so far without all the neatness/comments:
CODE

#include <iostream>
#include <string>
using namespace std;

void date(string a)
{
int i = a.size();
cout << "Size of String = " << i << endl;
string day = a.substr(3, 4);
cout << "Day: " << day << endl;
string month = a.substr(0, 2);
cout << "Month: " << month << endl;
string year = a.substr(6, 10);
cout << "Year: " << year << endl;
}



int main()
{
  string x;
   cout << "Enter a date (mm/dd/yyyy): ";
   cin >> x;

date(x);
}


And here's the output I'm getting:
Enter a date (mm/dd/yyyy): 10/02/1985
Size of String = 10
Day: 02/1
Month: 10
Year: 1985


I can't seem to get the 3rd and 4th position just by themselves no matter what I do. Any help?
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: C++ Simple Strings - Input And Output Date
5 Dec, 2007 - 02:20 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,143



Thanked: 77 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Wouldn't this simply be positions zero & two? I think you want 0-2. But that would still give you 3 values. You probably meant 0,1 or 1,2.
CODE

string month = a.substr(0, 2);

User is online!Profile CardPM
+Quote Post

kidstechno
RE: C++ Simple Strings - Input And Output Date
5 Dec, 2007 - 02:40 PM
Post #3

New D.I.C Head
*

Joined: 19 Nov, 2007
Posts: 4


My Contributions
It's the day I'm looking for.. it always outputs a forward slash (/) and something else and not just two numbers, everything else is fine (month year)

And here's the output I'm getting:
Enter a date (mm/dd/yyyy): 10/02/1985
Size of String = 10
Day: 02/1 <----
Month: 10
Year: 1985

Kinda odd.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: C++ Simple Strings - Input And Output Date
5 Dec, 2007 - 02:47 PM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,143



Thanked: 77 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(kidstechno @ 5 Dec, 2007 - 03:40 PM) *


Enter a date (mm/dd/yyyy): 10/02/1985


Day is pulling 3 through 4, the 3rd char in is /, four chars after that is 02/1
Try instead
CODE

string day = a.substr(3, 2);

User is online!Profile CardPM
+Quote Post

kidstechno
RE: C++ Simple Strings - Input And Output Date
5 Dec, 2007 - 03:53 PM
Post #5

New D.I.C Head
*

Joined: 19 Nov, 2007
Posts: 4


My Contributions
Oh durr thanks, I thought the substring was position x through y, not start at position x and show y characters. Thanks smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 03:10PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month