Help: Program Breaking on Improper Input

Hello, my program keeps breaking when either too many letters are entered or too many spaces and I cannot seem to figure out why. I am guessing that having 3-4 letters/spaces will get it passed the length check of the string and then the conversion of string to int is what is breaking the program. Though I included the bool check in the if statement so it should not even reach that part.

Thanks for your help and I apologize if any formatting is off, this is my first post and the preview does not seem to work.


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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
	string time, a, b, c, d, choice; //Time, abcd are for isolating the hours and minutes, choice for repeat
	bool check, repcheck; //Check for input validation and repeat validation
	int len, chlen, numA, numB, numC, numD; //len for input validation, chlen for repeat validation. numA, numB...for calculating the times


	do
	{
		//Intro and prompt
		cout << "Hello! This program will let you know if it is a lucky time!\n\n" << endl;

		do
		{
			check = true;
			cout << "Enter the time as HH:MM: ";
			getline(cin, time);

			len = time.length();

			//Checking proper length
			if (len == 0)
			{
				cout << "There was no entry. Please try again." << endl;
				check = false;
			}
			else if (len < 4)
			{
				cout << "That was too few numbers. Please use <HH:MM> format." << endl;
				check = false;
			}
			else if (len > 5)
			{
				cout << "That was too many numbers. Please use <HH:MM> format." << endl;
				check = false;
			}

			//Checking each place in the string
			for (int i = 0; i < time.length(); i++)
			{
				if (time[i] == ' ')
				{
					cout << "Improper entry, no spaces allowed. Please try again." << endl;
					check = false;
				}
				if (time[i] < '0' && time[i] > '9' && time[i] != ':')
				{
					cout << "Improper entry, must use numbers. Please try again." << endl;
					check = false;
					break;
				}
				if (time[i] == ':' && i != time.length() - 3)
				{
					cout << "Improper entry, colon is in the wrong spot. Please try again." << endl;
					check = false;
					if (time[i] < '0' && time[i] > '9')
					{
						cout << "Improper entry, must use numbers. Please try again." << endl;
						check = false;
					}
				}
				if (i == time.length() - 3 && time[i] != ':')
				{
					cout << "Improper entry, must use colon. Please try again." << endl;
					check = false;
				}

			}

			//For times such as H:MM
			if (check == true && len == 4)
			{
				a = time.at(0);
				c = time.at(2);
				d = time.at(3);

				numA = stoi(a);
				numC = stoi(c);
				numD = stoi(d);

				if (numC > 5)
				{
					cout << "Improper entry, minutes too high. Please try again." << endl;
					check = false;
					break;
				}
					if (numA == numC && numA == numD)
					{
						cout << time << " -> " << "Lucky Time!! Go play the lottery!" << endl;
						break;
					}
					if (numD == numA + 2 && numC == numA + 1)
					{
						cout << time << " -> " << "Great Time!! Your dreams will come true." << endl;
						break;
					}
					if (numA == numC + 1 && numA == numD + 2)
					{
						cout << time << " -> " << "Rough Time. Challenging roads ahead." << endl;
					}
					else
					{
						cout << time << " -> " << "Normal time, have a nice day." << endl;
					}
			}

			//For times such as HH:MM
			else if (check == true && len == 5)
			{
				a = time.at(0);
				b = time.at(1);
				c = time.at(3);
				d = time.at(4);

				numA = stoi(a);
				numB = stoi(b);
				numC = stoi(c);
				numD = stoi(d);

				if (numA > 1)
				{
					cout << "Improper entry, hours too high. Please try again" << endl;
					check = false;
					break;
				}
				if (numA == 1 && numB > 2)
				{
					cout << "Improper entry, hours too high. Please try again" << endl;
					check = false;
					break;
				}
				if (numC > 5)
				{
					cout << "Improper entry, minutes too high. Please try again." << endl;
					check = false;
					break;
				}
					if (numA == numB && numA == numC && numA == numD)
					{
						cout << time << " -> " << "Lucky Time!! Go play the lottery!" << endl;
						break;
					}
					if (numD == numA + 3 && numC == numA + 2 && numB == numA + 1)
					{
						cout << time << " -> " << "Great Time!! Your dreams will come true." << endl;
						break;
					}
					else
					{
						cout << time << " -> " << "Normal time, have a nice day." << endl;
					}
			}
		} while (check == false);

		//Repeat check and validation
		do
		{
			repcheck = true;
			cout << "\nWould you like to repeat? y/n : ";
			getline(cin, choice);
			chlen = choice.length();

			if (chlen == 0)
			{
				cout << "\nYou must choose, no skipping. " << endl;
				repcheck = false;
			}
			if (chlen > 1)
			{
				cout << "\nPlease enter just one letter, y/n." << endl;
				repcheck = false;
			}
			if (choice == "y" || choice == "Y" || choice == "n" || choice == "N")
			{
				repcheck = true;
			}
			else
			{
				cout << "\nPlease enter y or n " << endl;
				repcheck = false;
			}
			
		}while (repcheck == false);

	}while (choice == "y" || choice == "Y");
	

	//Sign off and signature
	cout << "\n\n\n     Thanks for using my program!!\n";
	cout << "Please contact me, Bowski, if there are any problems.\n";
	cout << "     Have a great day!!\n\n\n";

	system("pause");
	return 0;
}
Using the standard library (regular expressions http://en.cppreference.com/w/cpp/regex ):

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
#include <iostream>
#include <string>
#include <regex>
#include <iomanip>

int main()
{
    const std::regex time_regex( "^([0-1][0-9]|2[0-3]):([0-5][0-9])$" ) ; // HH:MM

    // std::cout << "enter time in HH:MM format: " ;
    // std::string str ;
    // std::getline( std::cin, str ) ;

    for( std::string str : { "19:46", "1a:46", "23:46", "29:46", "19:62", "04:06", "04 :06", "04: 06", "00:00" } )
    {
        std::cout << std::setw(8) << std::quoted(str) << "  =>  " ;

        if( std::regex_match( str, time_regex ) )
        {
            const int hour = std::stoi( str.substr(0,2) ) ;
            const int minute = std::stoi( str.substr(3) ) ;
            std::cout << "matched: hour == " << hour << "  minute == " << minute << '\n' ;
        }

        else std::cout << "badly formed input\n" ;
    }
}

http://coliru.stacked-crooked.com/a/1fdb15d8365eee45
Last edited on
Topic archived. No new replies allowed.