-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
101 lines (77 loc) · 1.65 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <fstream>
bool CheckName(std::string& name)
{
if (name.empty())
return false;
for(auto& c : name)
if (!isalpha(c) && c != ' ')
return false;
return true;
}
std::string GetName()
{
std::string temp;
std::cout << "Please enter your name(with spaces): ";
std::getline(std::cin, temp);
while (!CheckName(temp))
{
std::cout << "Your name is invalid! Please try again: ";
std::getline(std::cin, temp);
}
return temp;
}
bool CheckAge(std::string& temp)
{
if (temp.empty())
return false;
for(auto& c : temp)
if (!isdigit(c))
return false;
return true;
}
int GetAge()
{
std::string temp;
std::cout << "Please enter your age: ";
std::getline(std::cin, temp);
while(!CheckAge(temp))
{
std::cout << "Your age is invalid! Please try again: ";
std::getline(std::cin, temp);
}
return std::stoi(temp);
}
std::string GetNickName()
{
std::string temp;
std::cout << "Please enter your nickname: ";
std::getline(std::cin, temp);
while(temp.empty())
{
std::cout << "Your nickname is invalid! Please try again: ";
std::getline(std::cin, temp);
}
return temp;
}
void SaveToFile(std::string& str)
{
std::ofstream file;
file.open("Log.txt");
if(file.is_open())
file << str;
file.close();
}
int main()
{
const std::string name = GetName();
const int age = GetAge();
const std::string nickname = GetNickName();
std::string output = "Your name is " + name + ", you are " + std::to_string(age) + " years old and your username is " + nickname;
std::cout << '\n' << output << '\n' << '\n';
SaveToFile(output);
std::cout << "Press Enter to continue . . . ";
std::cin.get();
return 0;
}