473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Making a date format input, what's wrong with my code

Hey everyone, how my code should work is a console screen in which the user enters a date using format dd/mm/yyyy

Regardless of logical errors (checking days if bigger than 31 or negative values or.. etc) My code should be able to validate the following:

- if the user enters a letter, the program should output an error message

- if the user typed the date as 8/2/2014 the program should convert it to 08/02/2014 without returning an error message

Here's my code so far, I really can't find the bugs in it

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. main(){
  5.  char d[2], m[2], y[4], br1, br2, dx, mx;
  6.  bool end, nobr1, nobr2;
  7.  do{
  8.  cout << "Enter Date (format: dd/mm/yyyy):" << endl;
  9.  d={'n','n'};
  10.  m={'n','n'};
  11.  y={'n','n','n','n'};
  12.  dx = 'n';
  13.  mx = 'n';
  14.  end = false;
  15. nobr1 = false;
  16. nobr2 = false;
  17.  
  18.  for(int i=0;i<2;i++)
  19. {
  20. cin >> d[i];
  21. }
  22.  if(d[0]!='1' && d[0]!='2' && d[0]!='3' && d[0]!='4' && d[0]!='5' && d[0]!='6' && d[0]!='7' && d[0]!='8'  && d[0]!='9' && d[0]!='0'){
  23.   cout << "Wrong day value, please enter date again" << endl << endl;
  24.   end = true;
  25. }
  26.  
  27.  if((d[0]=='1' && d[0]=='2' && d[0]=='3' && d[0]=='4' && d[0]=='5' && d[0]=='6' && d[0]=='7' && d[0]=='8'  && d[0]=='9' && d[0]=='0') && (d[1] == '/' ||d[1] == '\\' ||d[1] == '*' ||d[1] == '.'  )){
  28.     d[1] = d[0];
  29.     d[0] = '0';
  30.     br1 = '/';
  31.     nobr1 = true;
  32.  
  33.  }
  34.  
  35. if(nobr1 == false) cin >> br1;
  36.  
  37.  for(int i=0;i<2;i++)
  38. {
  39. cin >> m[i];
  40.  
  41.  if(m[i]!='1' && m[i]!='2' && m[i]!='3' && m[i]!='4' && m[i]!='5' && m[i]!='6' && m[i]!='7' && m[i]!='8'  && m[i]!='9' && m[i]!='0'){
  42.  if (end == true) continue;
  43.    cout << "Wrong month value, please enter date again" << endl << endl;
  44.   end = true;
  45.  }
  46.  
  47. }
  48.  
  49. cin >> br2;
  50.  
  51.   for(int i=0;i<4;i++)
  52. {
  53. cin >> y[i];
  54.  
  55.  if(y[i]!='1' && y[i]!='2' && y[i]!='3' && y[i]!='4' && y[i]!='5' && y[i]!='6' && y[i]!='7' && y[i]!='8'  && y[i]!='9' && y[i]!='0'){
  56.       if (end == true) continue;
  57.    cout << "Wrong year value, please enter date again" << endl << endl;
  58.   end = true;
  59.  }
  60. }
  61.  
  62. if (end == true) continue;
  63.  
  64.  cout << d[0] << d[1] << "/" << m[0] << m[1] << "/" << y[0] << y[1] << y[2] << y[3] << endl << endl;}while(y[0] != 0);
  65.  return 0;
  66. }
  67.  
  68.  
as you notice, I tried two different validating formats in the days and months, unfortunately they both failed..

Thanks for helping!

Here's how my program is outputting so far:

Dec 16 '14 #1
3 1622
weaknessforcats
9,208 Expert Mod 8TB
I would cin into three ints. One for the month, one for the day and one for the year.

After each cin, check to see of the fail bit is set. If it is a non-int character was entered and you can ouptu your error message.

Once you have the three int values you can display them as month/day/year. You don't really need to make a string. In the case of the month, check to see if is less than 10 and if so, cout a "0" before the variable.

If you actually need a string, then use a stringstream object instead of cout. Like this:

Expand|Select|Wrap|Line Numbers
  1. char cstr[10];
  2.     int x = 25;
  3.     stringstream ss;
  4.  
  5.     ss << x;
  6.     ss >> cstr;
  7.  
  8.         cout << x << endl;
  9.     cout << cstr << endl;
Here an int value of 25 is inserted into a stringstream object and then extracted as a C-style string.

Both the object and the C style string are then displayed.

In your case, you insert the month (or maybe a 0 first if the month is less than 10). Then insert a /. Then the day (or maybe a 0 first if the month is less than 10). Then insert a /. Then insert the year.

Using the above example you should display mm/dd/yyyy.
Dec 16 '14 #2
Actually, I originally used three ints and too chars for for separation, but whenever I type a letter it gets crazy and does an infinite loop to the couts I have at the beginning of the program..
is there any header file I can add to avoid the continuous loop when entering a letter or a non numeric character?

To be honest with you I really don't have an idea how to check if the fail bit is set, I'm kinda new to C++ so if you could explain it a little it would be great! :) Thanks!
Dec 16 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
You have to check the fail bit after every cin. If the fail bit is on, the cin failed. Worse, the data that caused the failure is still in the input buffer. Worse even than that is the fact that every cin in your program checks the fail bit first thing. If it's on, the cin fails immediately without doing anything. What appears to happen is that all of your cins suddenly stop working.

My first suggestion is to not type data that will cause your code to fail. Use friendly data. Then after your program works in all respects then you might try editing the input. But I kid you not, this is a lot of work so be sure you want to spend time on it before you start.

This function:

Expand|Select|Wrap|Line Numbers
  1. bool GetInt(int* data)
  2. {
  3.     cin >> *data;
  4.     if (cin.good())
  5.     {
  6.       return true;
  7.     }
  8.     else
  9.     {
  10.         cin.clear();
  11.         _flushall();  //Microsoft only
  12.         return false;
  13.     }
  14. }
has you pass in a pointer to an int, Then rhere is a cin to that int. Then he failbit is checked. If it's good, the function returns true and the vue is in he int. Otherwise, the function resets the fail bit, and attempts to clear out the input buffer, which maybe won't work as input buffers can't be flushed.

There is also cin.fail() which does the same thing as cin.good() and you use whichever one works in the code without putting the rest of the program in an if statement.
Dec 16 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ian | last post by:
I have a problem I hope someone can point out where I am going wrong. I need to store a date/time in a cookie ( ie the date a visitor last visited ) However the date format changes to US, despite...
15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
1
by: Laurence Neville | last post by:
This is regarding a change in the Short Date format under Hebrew Regional Settings, that has caused huge problems in our ASP web application. The change appears to have been introduced sometime...
2
by: Ian | last post by:
I would like to have some validation on a date field. The date format is dd/mm which is used for our financial year end. I suppose I need also consider leap years. Please can you shed some light on...
4
by: rzhang | last post by:
Hi, We have an Access Application which works fine for most of the users. But there is one user who has the date input problem. When he enter a date field from a form, i.e. 09/03/2004 (Sept....
4
by: huzz | last post by:
How to i bring the date in (dd/mm/yyyy) format from the database? Here is what am doing... but having problem converting the string into the short date (dd/mm/yyyy) string strDateReturned =...
7
by: mewanalwis | last post by:
Dear Friends, I have a rather strange problem which invloves SQL server and ASP. The problem is this. I have an ASP application which use a SQL server. it saves date values with MM/dd/yyyy...
5
by: n78298 | last post by:
Hello Friends This is my first post. I am having problems in convetring teh date format. I have following code. if(!strcmp(elementbuf, "depDate")) { strncpy((char...
2
by: jmarr02s | last post by:
I need my Date field to possess the following format: Jan-05-1999 How do I change the Date Format/Input Mask to accomplish this? Thanks! John
1
by: katanah | last post by:
I am using Access, 2003. I have an entry form (single form) with a date field. I would like the user to be able to enter: 115 08 and have Access store and display the date as: 11/05/08, even...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.