Need help with input validation

I am trying to modify it so when i enter a integer with a letter like 202o it gives me an error message and tells me to retry instead of accepting it and moving on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//**************************************************
//*This program is designed to print out a calendar*
//*for the year desired by the user and determine  *
//*if the year is a loop year and modify a certain *
//*month based on that criteria, it also shows     *
//*the process of function decomposition in depth  *
//**************************************************

#include<iostream>
#include<cstdlib>
#include<iomanip>   //Used for setw
#include <string>

using namespace std;

int firstWeekDay(/* in */int);  //Function prototype with one value parameter
void getCalendarInput(/* inout */ int&);    //Function prototype with one reference parameter
void yearlyCalendar(/* in */ int);  //Function prototype with one value parameter

int main()
{
    int year, month, day, totalDaysinMonth, weekDay = 0, startingDay;   //Place holders for different situations as local variables

    //Array used to store month names more convenient than an if statement that matches number to name
    char * monthNames[] ={"       January", "       February", "        March", "       April", "        May", "        June", "        July", "       August", "      September", "      October", "      November", "      December"};
    int monthDays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //1-D Array that hold the total amount of days for each month

    getCalendarInput(year); //Function call with one argument

    yearlyCalendar(year);   //Function call with one argument

    return 0;
}

//List function definitions below

//*************** Function definition ***************
//*This function prompts the user to input their    *
//*desired year they would want a calendar displayed*
//*for and it also includes input validation so the *
//*values entered are unique and not random         *
//***************************************************
void getCalendarInput(/* inout */ int& year)
{
    cout << "What year do you want the calendar for? ";
    cin >> year;

    while(year < 0 || year >9999 || !cin)
    {
        cin.clear();
        cin.ignore(200 , '\n');
        cout << "non digit entered" << endl;
        cout << "What year do you want the calendar for? ";
        cin >> year;
    }
}

int firstWeekDay(/* in */ int year)
{
    int startday;
    int num1, num2, num3;
    num1 = (year - 1)/ 4;
    num2 = (year - 1)/ 100;
    num3 = (year - 1)/ 400;
    startday = (year + num1 - num2 + num3) %7;
    return startday;
}

void yearlyCalendar(/* in */ int year)
{
    int month, day, totalDaysinMonth, weekDay = 0, startingDay;
    char * monthNames[] ={"       January", "       February", "        March", "       April", "        May", "        June", "        July", "       August", "      September", "      October", "      November", "      December"};
    int monthDays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if(((year%4==0) && (year%100 !=0))||(year%400==0))
    {
         monthDays[1] = 29;
    }
    else
    {
         monthDays[1] = 28;
    }

    startingDay = firstWeekDay(year);

    for(month = 0; month < 12; month++)
    {
        cout << endl;
        cout << setw(10) << right << monthNames[month]<< "\n";
        cout << " S  M  T  W  T  F  S\n";
        cout << "_____________________\n";

        for(weekDay = 0; weekDay < startingDay; weekDay++)
            cout <<"   ";

        totalDaysinMonth = monthDays[month];
        for(day = 1; day <= totalDaysinMonth; day++)
        {
            cout << setw(3)<< day;

            if(++weekDay > 6)
            {
                cout << "\n";
                weekDay = 0;
            }
            startingDay = weekDay;
        }
        cout << endl;
    }
}
Hello Depressed,

With the formatted input you are using it will extract up to the first non number character, a white space or the new line whichever comes first. So if you meant to enter "2020", but entered "202o" it will extract the "202" which is considered a legal number based on the while loop.

I showed you this http://www.cplusplus.com/forum/beginner/268045/#msg1153396 for the while loop that would catch the low number and how to better use the while loop.

If the letter was at the beginning the "!cin" part of the while condition would catch this when "cin" fails to read a number.

For now the function is working correctly as you wrote it.

Although any year less than 1582 may not produce a correct calendar.

Andy
I am trying to modify it so when i enter a integer with a letter like 202o it gives me an error message and tells me to retry instead of accepting it and moving on.

What have you tried?

This function parse_number yields a double if and only if the argument s contains the string representation of the number and no extra garbage (except possibly leading whitespace).

1
2
3
4
5
6
7
8
9
10
std::optional<double> parse_number(std::string const& s)
{
  char*        parse_end = nullptr;
  char const*  s_begin   = s.data();
  char const*  s_end     = s.data() + s.size();
  double const d         = std::strtod(s_begin, &parse_end);

  if ((parse_end == s_end) && s.size()) return d; 
  else return std::nullopt;
}
Last edited on
Registered users can post here. Sign in or register to post.