Tech Support Guy banner
  • Please post in our Community Feedback thread for help with the new forum software! If you are having trouble logging in, please Contact Us for assistance.
Status
Not open for further replies.
1 - 9 of 9 Posts

·
Registered
Joined
·
11 Posts
Discussion Starter · #1 ·
Working on a homework assignment and I am a little jammed up here. I want to have a user enter a date in the format mm-dd-yy and store the month, day and year in seperate variables. I wanted to just see if I could read in the month first, and decided to try it by reading in one char at a time and copying them into the variabl. It doesn't work. Anyone have any helpful advice? :)

Code:
#include <string>

char entry;
char month[3] = ' ';
char day[3] = ' ';
char year[3] = ' ';

cout << "enter date (mm-dd-yy): ";
do
{
   cin.get(entry);
   while(entry!='-')
   {
      strcat(month, entry);
   }
}while(entry!='\n');
 

·
Registered
Joined
·
3,015 Posts
Welcome to TSG!!

Congratulations on asking the right question. You would be surprised how many times all of us have seen "this is a homework assignment -- how do I do it" and that's it! We're all happy to help point you in the right direction but don't ask us to do your homework for you.

Now to your question: strtok( ) is your best option assuming that you can guarantee that the user entered the date with '-' separators and not '/' or '.' or whatever. If you're new to programming, get used to the idea of users not doing what they're told. You need to handle any concievable variation in your code so that it doesn't crash when someone does something stupid. If the input doesn't comply with what you're expecting, rejecting it and making the user enter it right is perfectly fine.
 

·
Registered
Joined
·
3,015 Posts
BTW -- strtok( ) is an ANSI C function which you will be able to use in C++ but there may be another way too. Some of the more experienced C++ developers here may be able to help with that.
 

·
Registered
Joined
·
2,438 Posts
You could also use "_getch()" which will read in one character at a time from the keyboard. This would give you a lot of control over what is input and could even offer a way of not needing the user to input any separator characters at all. It may be offered as simply "getch()" with no underscore depending on which compiler you are using.

Note _getch() does not echo its input to the screen so you must arrange to do this yourself. You could use putchar(ch).

Another useful function you could use here is "isdigit(ch)" which returns nonzero if ch is a digit 0-9, or zero if ch is anything else.
Code:
if ( isdigit(ch) )
   "ch is acceptable, process it";
else
   "warn the user and get it again"
or code to that effect. :D

Which approach you use will depend on how advanced you are in your course and consequently what you have covered. There is no right or wrong, only what you can make work or not.

And congrats for approaching the subject of help the way you did. It is easy to post that what was advised is too far ahead of you and you couldn't get it to work, someone will always then offer a little more under those circumstances. :)
 

·
Registered
Joined
·
52 Posts
it will be very simple if you use a structure for date format just check this out

struct
{
char date[];
char month[];
char year[];
} D;

then in main

D.date=getch();
D.month=getch();
D.yera=getch();

i hope this will simplify your job.
 

·
Registered
Joined
·
2,438 Posts
Remember Itlu that we have no idea of what are the capabilities of each poster here. Thanks to him being brave enough to post his own thoughts in the first instance we know he does have a knowledge of STL strings and char arrays. To set out a solution using anything else risks going too far beyond what he can cope with. There is also the question of what his assignment is meant to lead him towards. Is it to broaden his knowledge by using everything he can, or is it to experience and improve a specific aspect of C++? Does he already know of structures or would that put him off the whole suggestion?

Sometimes posters will give this information but other times we have to be careful not to get overexcited and just suggest what we ourselves would do, (one of my own worst faults)! :D I personally like the structure idea you suggested but why don't we suggest that we go the whole hog and declare it as a class then write functions to improve data abstraction - get_Day(), get_Month(), get_Year(), set_day(), set_Month() etc etc? Do you catch my drift? ;)

Coderitr has the experience of this and we would be wise to follow his lead. :)

Now back to my favourite, those loops and char[]! ;)

getch() won't work but getchar() will if you don't mind the user having to press <ENTER> after each part of their entry. Moving on to C++ the function often used for this type of thing is getline().

I prefer the individual char method simply because that offers the opportunity to verify each character as it is typed by the user. Like Coderitr I have a very poor view of the ability (or inclination) of users to input things in the format in which they should! :D
 

·
Registered
Joined
·
11 Posts
Discussion Starter · #9 ·
Thanks for all the feedback guys!

I wanted to mention that I have already studied classes and structs, and the problem I gave was part of a class member function I was trying to get to work.

I tried to figure out strtok() and didn't really understand it.

To solve my problem I did the following:

Code:
char date[9],
        day[3],
        month[3], 
        year[3];

cout << "Enter date (dd-mm-yy) ";
cin >> date;

day[0] = date[0];
day[1] = date[1];
month[0] = date[3];
month[1] = date[4];
year[0] = date[6];
year[1] = date[7];
There are probably much nicer ways to do it, but I just want it to do what I need for now, and I will perfect the process later.

In answer to AlbertB's questions...my instructor hands out a project spec and says "I don't care how you get it to work, just get it to work." So I think the assignment is more to broaden my knowledge, to get me to go out and look for ways to get things to work.
 
1 - 9 of 9 Posts
Status
Not open for further replies.
Top