I've written simple numeric menu which displays in the console. When user clicks '1', something happens, then when he clicks enter, I clear the whole output except for the menu itself. When the user clicks '5', program ends.
However, the code got ugly. It's full of cin.get()'s, cout's, and other crap like that.
Let me post it here:
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
? – Jamal Apr 20 at 19:05