I've written a simple numeric menu which displays in the console. When the user types in '1', something happens. When the user hits ENTER, I clear the whole output except for the menu itself. When the user types in '5', the program ends.
However, the code got ugly. It's full of cin.get()
s, cout
s, and other stuff.
int main()
{
using namespace std;
node *root = NULL;
char ch = '0';
while(ch != '5')
{
cout << "1. Add new elements\n"
"2. Display info about the tree\n"
"3. Remove nodes\n"
"4. Export to file\n"
"5. Exit\n";
cin >> ch; cin.get();
switch(ch)
{
case '1': case '3':
{
vector<int> numbers;
string line;
cout << "\nProvide numbers divided with spaces: ";
getline(cin, line);
istringstream in(line, istringstream::in);
int current;
while (in >> current) numbers.push_back(current);
(ch == '1') ? insert(root, numbers) : remove(root, numbers);
cout << "\nClick to return to menu";
cin.get();
break;
}
case '2':
{
displayTreeInfo(root, cout);
cout << "\n\nClick to return to menu";
cin.get();
break;
}
case '4':
{
string name;
cout << "Provide filename: ";
cin >> name;
buildTree(root, loadNumbersFromFile(name));
exportTreeToFile(root, name);
std::cout << "\nData has been exported.\n\n";
cin.get(); cin.get();
break;
}
}
system("cls");
}
exit(EXIT_SUCCESS);
}
How could I improve this code?
using namespace std
inside ofmain
? \$\endgroup\$