Created
March 3, 2018 17:01
-
-
Save anonymous/95613cbea240872b161bf10f1205f6cd to your computer and use it in GitHub Desktop.
CSE 273 Assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int Check(string stdID); | |
int main(void) | |
{ | |
string stdID = ""; | |
while (true) | |
{ | |
cout << "Please enter the student ID: "; | |
cin >> stdID; | |
if(stdID == "X" || stdID == "x") | |
{ | |
cout << "Thank you for using the program!" << endl; | |
break; | |
} | |
else | |
Check(stdID); | |
} | |
} | |
int Check(string stdID) | |
{ | |
/* | |
string stdID; | |
cout << "Please enter the student ID: "; | |
cin >> stdID; | |
*/ | |
// 1. Check the length | |
int len = stdID.length(); | |
if (len != 10) | |
{ | |
cout << "Invalid student ID : length is wrong." << endl; | |
return 0; | |
} | |
// 2. Check if all the characters are digits | |
for (int i = 0; i < len; ++i) | |
{ | |
if (!isdigit(stdID[i])) | |
{ | |
cout << "Invalid student ID : all characters must be digits." << endl; | |
return 0; | |
} | |
} | |
// 3. Check the first character | |
if (stdID[0] > '1') | |
{ | |
cout << "Invalid student ID : invalid year." << endl; // Time traveller! | |
return 0; | |
} | |
// 4. Deduct the year and check it | |
int year = (stdID[0] - '0') * 10 + (stdID[1] - '0'); | |
if (year > 18) // Another time traveller! | |
{ | |
cout << "Invalid student ID : invalid year." << endl; | |
return 0; | |
} | |
else if (year == 18) | |
{ | |
if (stdID[2] > '1') // Invalid semester for 2018 | |
{ | |
cout << "Invalid student ID : invalid semester." << endl; | |
return 0; | |
} | |
} | |
else | |
{ | |
if(stdID[2] > '3') // A student admitted into a non-existent semester. Fishy 🤔 | |
{ | |
cout << "Invalid student ID : invalid semester." << endl; | |
return 0; | |
} | |
} | |
if (stdID[7] != '0' && stdID[7] != '6') | |
{ | |
cout << "Invalid student ID : third last digit must be either 0 or 6." << endl; | |
return 0; | |
} | |
cout << "Valid student ID." << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment