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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamunknown
    New Member
    • Dec 2014
    • 5

    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

    Code:
    #include<iostream>
    using namespace std;
    
    main(){
     char d[2], m[2], y[4], br1, br2, dx, mx;
     bool end, nobr1, nobr2;
     do{
     cout << "Enter Date (format: dd/mm/yyyy):" << endl;
     d={'n','n'};
     m={'n','n'};
     y={'n','n','n','n'};
     dx = 'n';
     mx = 'n';
     end = false;
    nobr1 = false;
    nobr2 = false;
    
     for(int i=0;i<2;i++)
    {
    cin >> d[i];
    }
     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'){
      cout << "Wrong day value, please enter date again" << endl << endl;
      end = true;
    }
    
     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] == '.'  )){
        d[1] = d[0];
        d[0] = '0';
        br1 = '/';
        nobr1 = true;
    
     }
    
    if(nobr1 == false) cin >> br1;
    
     for(int i=0;i<2;i++)
    {
    cin >> m[i];
    
     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'){
     if (end == true) continue;
       cout << "Wrong month value, please enter date again" << endl << endl;
      end = true;
     }
    
    }
    
    cin >> br2;
    
      for(int i=0;i<4;i++)
    {
    cin >> y[i];
    
     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'){
          if (end == true) continue;
       cout << "Wrong year value, please enter date again" << endl << endl;
      end = true;
     }
    }
    
    if (end == true) continue;
    
     cout << d[0] << d[1] << "/" << m[0] << m[1] << "/" << y[0] << y[1] << y[2] << y[3] << endl << endl;}while(y[0] != 0);
     return 0;
    }
    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:
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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:

    Code:
    char cstr[10];
    	int x = 25;
    	stringstream ss;
    
    	ss << x;
    	ss >> cstr;
            
            cout << x << endl;
    	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.

    Comment

    • adamunknown
      New Member
      • Dec 2014
      • 5

      #3
      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!

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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:

        Code:
        bool GetInt(int* data)
        {
            cin >> *data;
        	if (cin.good())
        	{
        	  return true;
        	}
        	else
        	{
        		cin.clear();
        		_flushall();  //Microsoft only
        		return false;
        	}
        }
        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.

        Comment

        Working...