-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLab2.cpp
More file actions
81 lines (74 loc) · 1.68 KB
/
Lab2.cpp
File metadata and controls
81 lines (74 loc) · 1.68 KB
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
/*
* Lab2.cpp
*
* Created on: Sep 19, 2013
* Author: leon
*/
#include <cstdlib>
#include <string>
#include <cctype>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
using namespace std;
bool is_valid_ID(const string& id) {
if (id.length() != 9 || id[0] != 'A') {
return false;
}
for (string::size_type i = 1; i < id.length(); i++) {
if (!isdigit(id[i])) {
return false;
}
}
return true;
}
bool is_valid_score(int score){
return score >=0 && score <= 100;
}
bool get_valid_word(const string& prompt, string& word, bool (*is_valid)(const string&) = 0){
string line;
string temp;
while(1){
cout << prompt;
if (!getline(cin, line))
break;
istringstream iss(line);
if(iss >> temp){
if(is_valid == 0 || is_valid(temp)){
word = temp;
return true;
}
}
}
cin.clear();
return false;
}
bool get_valid_int(const string& prompt, int& n, bool (*is_valid)(int) = 0){
string line;
int score;
while(1){
cout << prompt;
if (!getline(cin, line))
break;
istringstream iss(line);
if(iss >> score){
if(is_valid == 0 || is_valid_score(score)){
n = score;
return true;
}
}
}
cin.clear();
return false;
}
int lab2_main(int argc, char** argv) {
int score;
string word;
while(get_valid_word("please inter ID. \n ", word , is_valid_ID) &&
get_valid_int("please inter score \n ", score, is_valid_score)){
cerr << word << " " << score << endl;
}
return 0;
}