Skip to content

Commit 9c2791f

Browse files
committed
refactor(ex4.cpp): add namespace, remove std:: and add more spaces
1 parent 1b7abbb commit 9c2791f

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

exercises/ex4.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
#include <string>
55
#include <map>
66

7+
using namespace std;
8+
79
const int ZERO = 48;
810
const int NINE = 57;
911
const int MAX_NUMBER = 10;
1012
const int MINUS = 45;
1113

12-
const std::string ERROR_COLOR = "\033[31m";
13-
const std::string NORMAL_COLOR = "\033[0m";
14+
const string ERROR_COLOR = "\033[31m";
15+
const string NORMAL_COLOR = "\033[0m";
1416

1517
int insertNumber();
16-
bool checkStringIsNumber(std::string in);
18+
bool checkStringIsNumber(string in);
1719

1820
int main(){
1921
int month = insertNumber();
20-
std::map<int, std::string> calendary = {
22+
map<int, string> calendary = {
2123
{1, "31"},
2224
{2, "28/29"},
2325
{3, "31"},
@@ -31,25 +33,35 @@ int main(){
3133
{11, "30"},
3234
{12, "31"},
3335
};
34-
std::cout << calendary[month] << " days";
36+
cout << calendary[month] << " days";
3537
return 0;
3638
}
3739

3840
int insertNumber(){
39-
std::string insert;
41+
string insert;
4042
bool isCorrect = true;
43+
4144
do {
42-
if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR;
43-
std::cout << "Insert Mounth: ";
44-
getline(std::cin, insert);
45-
} while(!(isCorrect = checkStringIsNumber(insert)));
46-
return std::stoi(insert);
45+
if(!isCorrect) cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR;
46+
47+
cout << "Insert Mounth: ";
48+
getline(cin, insert);
49+
50+
isCorrect = checkStringIsNumber(insert);
51+
} while(!isCorrect);
52+
53+
return stoi(insert);
4754
}
4855

49-
bool checkStringIsNumber(std::string in){
50-
if(in.empty()) return false;
56+
bool checkStringIsNumber(string in){
57+
if(in.empty())
58+
return false;
59+
5160
for(int i = 0; i < in.size(); i++)
52-
if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) return false;
61+
if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS)
62+
return false;
63+
5364
if(stoi(in) > MAX_NUMBER) return false;
65+
5466
return true;
5567
}

0 commit comments

Comments
 (0)