Skip to main content
Code Review

Return to Question

Rollback to Revision 4
Source Link
pacmaninbw
  • 26.2k
  • 13
  • 47
  • 113
#include <iostream>
#include <vector>
#include <string>
#include <limits>
// cin flushing function
void fluxInputError() 
{
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// function for controlling exit from submenus
void checkExit(bool &stay) // exit function for internal menus
{
 while (true) 
 {
 std::cout << "\nDo you want to continue?(y/n)\n";
 std::string check{ 0 };
 if (std::cin >> check) // check if cin is valid
 {
 std::cout << "\nI'm inside\n";
 if (check == "y" || check == "Y")
 {
 stay = true; // 
 break;
 }
 else if (check == "n" || check == "N") 
 {
 stay = false; // this value is used to exit from evoking, not from here 
 break; // that use break
 }
 }
 else
 {
 fluxInputError();
 continue;
 }
 }
}
//data insertion function in the two vectors 
void insertData(std::string &name, int &score, std::vector<std::string> &names, std::vector<int> &scores)
{
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 while (true) 
 {
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 if (std::cin >> name >> score) // check if cin is valid
 {
 if (name == "NoName" && score == 0) // as required by the exercise. I didn't use my custom checkExit function.
 {
 break;
 }
 names.push_back(name);
 scores.push_back(score);
 for (int i = 0; i < names.size(); ++i)
 {
 for (int x = 0; x < names.size(); ++x)
 {
 if (i != x && x < names.size() && names[i] == names[x])
 {
 //std::cout << "\nError! " << names[x] << " is entered twice\n\n";
 //names.erase(names.begin() + x);
 //scores.erase(scores.begin() + x);
 // instead of giving an error message for repeated names as asked from exercixe, 
 // I prefered add the score to the first name entered and remove duplicates
 names.erase(names.begin() + x);
 scores[i] += scores[x];
 scores.erase(scores.begin() + x);
 }
 }
 }
 }
 else
 {
 std::cout << "\nWrong input!\n";
 fluxInputError();
 continue;
 }
 }
}
// selection function
void askingWhatToDo(char& selection)
{
 
 std::cout << "\nWhat do you want to do now?\n\n";
 std::cout << "1) I want to insert some data into the database?\n";
 std::cout << "2) I want to search for a name in the database?\n";
 std::cout << "3) I want to search for a score in the database\n";
 std::cout << "4) I want to see all names and scores\n";
 std::cout << "5) I finished\n\n";
 std::cin >> selection;
}
// function used to search for values ​​by name
void lookinFornName(bool&stay, std::string &name, std::vector<std::string> &names, std::vector<int> &scores) //*
{
 while (stay)
 {
 std::cout << "\nEnter a name\n";
 if (std::cin >> name) 
 {
 bool isNotFound{ true };
 for (int i = 0; i < names.size(); ++i)
 {
 if (name == names[i])
 {
 std::cout << "The score of " << name << " is " << scores[i] << "\n";
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nName not found\n";
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay); 
 }
}
// function used to search for values ​​by score
void lookingForScore(bool& stay, int& score, std::vector<std::string> &names, std::vector<int> &scores) //**
{ 
 
 while (stay)
 {
 
 std::cout << "\nEnter a score\n";
 if (std::cin >> score)
 {
 bool isNotFound{ true };
 for (int i = 0; i < scores.size(); ++i)
 {
 if (score == scores[i])
 {
 std::cout << names[i] << '\n';
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nScore not found\n";
 
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay);
 }
}
// this function shows all the (name, score) pairs present in the two vectors.
void allNamesAndScores(std::vector<std::string> &names, std::vector<int> &scores)
{
 if (names.size() > 0)
 {
 std::cout << "\nNames and scores are; " << "\n\n";
 for (int i = 0; i < names.size(); i++)
 {
 std::cout << names[i] << ',' << scores[i] << '\n';
 }
 }
 else
 { 
 std::cout << "\nThe database is empty\n";
 }
}
int main()
{
 //4 - Exercise - 21)
 
 std::vector<std::string> names;
 std::vector<int> scores;
 std::string name{ 0 };
 int score{ 0 };
 bool isFinished{ false };
 while (!isFinished)
 {
 bool stay{ true };
 char selection{ 0 };
 askingWhatToDo(selection);
 switch (selection)
 {
 case '1':
 insertData(name, score, names, scores);
 break;
 case '2':
 lookinFornName(stay, name, names, scores);
 break;
 case '3':
 lookingForScore(stay, score, names, scores);
 break;
 case '4':
 allNamesAndScores(names, scores);
 break;
 case '5':
 isFinished = true;
 break;
 }
 }
 return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <limits>
// cin flushing function
void fluxInputError() 
{
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// function for controlling exit from submenus
void checkExit(bool &stay) // exit function for internal menus
{
 while (true) 
 {
 std::cout << "\nDo you want to continue?(y/n)\n";
 std::string check{ 0 };
 if (std::cin >> check) // check if cin is valid
 {
 if (check == "y" || check == "Y")
 {
 stay = true; // 
 break;
 }
 else if (check == "n" || check == "N") 
 {
 stay = false; // this value is used to exit from evoking, not from here 
 break; // that use break
 }
 }
 else
 {
 fluxInputError();
 continue;
 }
 }
}
//data insertion function in the two vectors 
void insertData(std::string &name, int &score, std::vector<std::string> &names, std::vector<int> &scores)
{
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 while (true) 
 {
 if (std::cin >> name >> score) // check if cin is valid
 {
 if (name == "NoName" && score == 0) // as required by the exercise. I didn't use my custom checkExit function.
 {
 break;
 }
 names.push_back(name);
 scores.push_back(score);
 for (int i = 0; i < names.size(); ++i)
 {
 for (int x = 0; x < names.size(); ++x)
 {
 if (i != x && x < names.size() && names[i] == names[x])
 {
 //std::cout << "\nError! " << names[x] << " is entered twice\n\n";
 //names.erase(names.begin() + x);
 //scores.erase(scores.begin() + x);
 // instead of giving an error message for repeated names as asked from exercixe, 
 // I prefered add the score to the first name entered and remove duplicates
 names.erase(names.begin() + x);
 scores[i] += scores[x];
 scores.erase(scores.begin() + x);
 }
 }
 }
 }
 else
 {
 std::cout << "\nWrong input!\n";
 fluxInputError();
 continue;
 }
 }
}
// selection function
void askingWhatToDo(char& selection)
{
 
 std::cout << "\nWhat do you want to do now?\n\n";
 std::cout << "1) I want to insert some data into the database?\n";
 std::cout << "2) I want to search for a name in the database?\n";
 std::cout << "3) I want to search for a score in the database\n";
 std::cout << "4) I want to see all names and scores\n";
 std::cout << "5) I finished\n\n";
 std::cin >> selection;
}
// function used to search for values ​​by name
void lookinFornName(bool&stay, std::string &name, std::vector<std::string> &names, std::vector<int> &scores) //*
{
 while (stay)
 {
 std::cout << "\nEnter a name\n";
 if (std::cin >> name) 
 {
 bool isNotFound{ true };
 for (int i = 0; i < names.size(); ++i)
 {
 if (name == names[i])
 {
 std::cout << "The score of " << name << " is " << scores[i] << "\n";
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nName not found\n";
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay); 
 }
}
// function used to search for values ​​by score
void lookingForScore(bool& stay, int& score, std::vector<std::string> &names, std::vector<int> &scores) //**
{ 
 
 while (stay)
 {
 
 std::cout << "\nEnter a score\n";
 if (std::cin >> score)
 {
 bool isNotFound{ true };
 for (int i = 0; i < scores.size(); ++i)
 {
 if (score == scores[i])
 {
 std::cout << names[i] << '\n';
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nScore not found\n";
 
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay);
 }
}
// this function shows all the (name, score) pairs present in the two vectors.
void allNamesAndScores(std::vector<std::string> &names, std::vector<int> &scores)
{
 if (names.size() > 0)
 {
 std::cout << "\nNames and scores are; " << "\n\n";
 for (int i = 0; i < names.size(); i++)
 {
 std::cout << names[i] << ',' << scores[i] << '\n';
 }
 }
 else
 { 
 std::cout << "\nThe database is empty\n";
 }
}
int main()
{
 //4 - Exercise - 21)
 
 std::vector<std::string> names;
 std::vector<int> scores;
 std::string name{ 0 };
 int score{ 0 };
 bool isFinished{ false };
 while (!isFinished)
 {
 bool stay{ true };
 char selection{ 0 };
 askingWhatToDo(selection);
 switch (selection)
 {
 case '1':
 insertData(name, score, names, scores);
 break;
 case '2':
 lookinFornName(stay, name, names, scores);
 break;
 case '3':
 lookingForScore(stay, score, names, scores);
 break;
 case '4':
 allNamesAndScores(names, scores);
 break;
 case '5':
 isFinished = true;
 break;
 }
 }
 return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <limits>
// cin flushing function
void fluxInputError() 
{
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// function for controlling exit from submenus
void checkExit(bool &stay) // exit function for internal menus
{
 while (true) 
 {
 std::cout << "\nDo you want to continue?(y/n)\n";
 std::string check{ 0 };
 if (std::cin >> check) // check if cin is valid
 {
 std::cout << "\nI'm inside\n";
 if (check == "y" || check == "Y")
 {
 stay = true; // 
 break;
 }
 else if (check == "n" || check == "N") 
 {
 stay = false; // this value is used to exit from evoking, not from here 
 break; // that use break
 }
 }
 else
 {
 fluxInputError();
 continue;
 }
 }
}
//data insertion function in the two vectors 
void insertData(std::string &name, int &score, std::vector<std::string> &names, std::vector<int> &scores)
{
 while (true) 
 {
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 if (std::cin >> name >> score) // check if cin is valid
 {
 if (name == "NoName" && score == 0) // as required by the exercise. I didn't use my custom checkExit function.
 {
 break;
 }
 names.push_back(name);
 scores.push_back(score);
 for (int i = 0; i < names.size(); ++i)
 {
 for (int x = 0; x < names.size(); ++x)
 {
 if (i != x && x < names.size() && names[i] == names[x])
 {
 //std::cout << "\nError! " << names[x] << " is entered twice\n\n";
 //names.erase(names.begin() + x);
 //scores.erase(scores.begin() + x);
 // instead of giving an error message for repeated names as asked from exercixe, 
 // I prefered add the score to the first name entered and remove duplicates
 names.erase(names.begin() + x);
 scores[i] += scores[x];
 scores.erase(scores.begin() + x);
 }
 }
 }
 }
 else
 {
 std::cout << "\nWrong input!\n";
 fluxInputError();
 continue;
 }
 }
}
// selection function
void askingWhatToDo(char& selection)
{
 
 std::cout << "\nWhat do you want to do now?\n\n";
 std::cout << "1) I want to insert some data into the database?\n";
 std::cout << "2) I want to search for a name in the database?\n";
 std::cout << "3) I want to search for a score in the database\n";
 std::cout << "4) I want to see all names and scores\n";
 std::cout << "5) I finished\n\n";
 std::cin >> selection;
}
// function used to search for values ​​by name
void lookinFornName(bool&stay, std::string &name, std::vector<std::string> &names, std::vector<int> &scores) //*
{
 while (stay)
 {
 std::cout << "\nEnter a name\n";
 if (std::cin >> name) 
 {
 bool isNotFound{ true };
 for (int i = 0; i < names.size(); ++i)
 {
 if (name == names[i])
 {
 std::cout << "The score of " << name << " is " << scores[i] << "\n";
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nName not found\n";
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay); 
 }
}
// function used to search for values ​​by score
void lookingForScore(bool& stay, int& score, std::vector<std::string> &names, std::vector<int> &scores) //**
{ 
 
 while (stay)
 {
 
 std::cout << "\nEnter a score\n";
 if (std::cin >> score)
 {
 bool isNotFound{ true };
 for (int i = 0; i < scores.size(); ++i)
 {
 if (score == scores[i])
 {
 std::cout << names[i] << '\n';
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nScore not found\n";
 
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay);
 }
}
// this function shows all the (name, score) pairs present in the two vectors.
void allNamesAndScores(std::vector<std::string> &names, std::vector<int> &scores)
{
 if (names.size() > 0)
 {
 std::cout << "\nNames and scores are; " << "\n\n";
 for (int i = 0; i < names.size(); i++)
 {
 std::cout << names[i] << ',' << scores[i] << '\n';
 }
 }
 else
 { 
 std::cout << "\nThe database is empty\n";
 }
}
int main()
{
 //4 - Exercise - 21)
 
 std::vector<std::string> names;
 std::vector<int> scores;
 std::string name{ 0 };
 int score{ 0 };
 bool isFinished{ false };
 while (!isFinished)
 {
 bool stay{ true };
 char selection{ 0 };
 askingWhatToDo(selection);
 switch (selection)
 {
 case '1':
 insertData(name, score, names, scores);
 break;
 case '2':
 lookinFornName(stay, name, names, scores);
 break;
 case '3':
 lookingForScore(stay, score, names, scores);
 break;
 case '4':
 allNamesAndScores(names, scores);
 break;
 case '5':
 isFinished = true;
 break;
 }
 }
 return 0;
}
deleted 35 characters in body
Source Link
RedM3tal
  • 391
  • 1
  • 7
#include <iostream>
#include <vector>
#include <string>
#include <limits>
// cin flushing function
void fluxInputError() 
{
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// function for controlling exit from submenus
void checkExit(bool &stay) // exit function for internal menus
{
 while (true) 
 {
 std::cout << "\nDo you want to continue?(y/n)\n";
 std::string check{ 0 };
 if (std::cin >> check) // check if cin is valid
 {
 std::cout << "\nI'm inside\n";
 if (check == "y" || check == "Y")
 {
 stay = true; // 
 break;
 }
 else if (check == "n" || check == "N") 
 {
 stay = false; // this value is used to exit from evoking, not from here 
 break; // that use break
 }
 }
 else
 {
 fluxInputError();
 continue;
 }
 }
}
//data insertion function in the two vectors 
void insertData(std::string &name, int &score, std::vector<std::string> &names, std::vector<int> &scores)
{
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 while (true) 
 {
 if (std::cin >> name >> score) // check if cin is valid
 {
 if (name == "NoName" && score == 0) // as required by the exercise. I didn't use my custom checkExit function.
 {
 break;
 }
 names.push_back(name);
 scores.push_back(score);
 for (int i = 0; i < names.size(); ++i)
 {
 for (int x = 0; x < names.size(); ++x)
 {
 if (i != x && x < names.size() && names[i] == names[x])
 {
 //std::cout << "\nError! " << names[x] << " is entered twice\n\n";
 //names.erase(names.begin() + x);
 //scores.erase(scores.begin() + x);
 // instead of giving an error message for repeated names as asked from exercixe, 
 // I prefered add the score to the first name entered and remove duplicates
 names.erase(names.begin() + x);
 scores[i] += scores[x];
 scores.erase(scores.begin() + x);
 }
 }
 }
 }
 else
 {
 std::cout << "\nWrong input!\n";
 fluxInputError();
 continue;
 }
 }
}
// selection function
void askingWhatToDo(char& selection)
{
 
 std::cout << "\nWhat do you want to do now?\n\n";
 std::cout << "1) I want to insert some data into the database?\n";
 std::cout << "2) I want to search for a name in the database?\n";
 std::cout << "3) I want to search for a score in the database\n";
 std::cout << "4) I want to see all names and scores\n";
 std::cout << "5) I finished\n\n";
 std::cin >> selection;
}
// function used to search for values ​​by name
void lookinFornName(bool&stay, std::string &name, std::vector<std::string> &names, std::vector<int> &scores) //*
{
 while (stay)
 {
 std::cout << "\nEnter a name\n";
 if (std::cin >> name) 
 {
 bool isNotFound{ true };
 for (int i = 0; i < names.size(); ++i)
 {
 if (name == names[i])
 {
 std::cout << "The score of " << name << " is " << scores[i] << "\n";
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nName not found\n";
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay); 
 }
}
// function used to search for values ​​by score
void lookingForScore(bool& stay, int& score, std::vector<std::string> &names, std::vector<int> &scores) //**
{ 
 
 while (stay)
 {
 
 std::cout << "\nEnter a score\n";
 if (std::cin >> score)
 {
 bool isNotFound{ true };
 for (int i = 0; i < scores.size(); ++i)
 {
 if (score == scores[i])
 {
 std::cout << names[i] << '\n';
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nScore not found\n";
 
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay);
 }
}
// this function shows all the (name, score) pairs present in the two vectors.
void allNamesAndScores(std::vector<std::string> &names, std::vector<int> &scores)
{
 if (names.size() > 0)
 {
 std::cout << "\nNames and scores are; " << "\n\n";
 for (int i = 0; i < names.size(); i++)
 {
 std::cout << names[i] << ',' << scores[i] << '\n';
 }
 }
 else
 { 
 std::cout << "\nThe database is empty\n";
 }
}
int main()
{
 //4 - Exercise - 21)
 
 std::vector<std::string> names;
 std::vector<int> scores;
 std::string name{ 0 };
 int score{ 0 };
 bool isFinished{ false };
 while (!isFinished)
 {
 bool stay{ true };
 char selection{ 0 };
 askingWhatToDo(selection);
 switch (selection)
 {
 case '1':
 insertData(name, score, names, scores);
 break;
 case '2':
 lookinFornName(stay, name, names, scores);
 break;
 case '3':
 lookingForScore(stay, score, names, scores);
 break;
 case '4':
 allNamesAndScores(names, scores);
 break;
 case '5':
 isFinished = true;
 break;
 }
 }
 return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <limits>
// cin flushing function
void fluxInputError() 
{
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// function for controlling exit from submenus
void checkExit(bool &stay) // exit function for internal menus
{
 while (true) 
 {
 std::cout << "\nDo you want to continue?(y/n)\n";
 std::string check{ 0 };
 if (std::cin >> check) // check if cin is valid
 {
 std::cout << "\nI'm inside\n";
 if (check == "y" || check == "Y")
 {
 stay = true; // 
 break;
 }
 else if (check == "n" || check == "N") 
 {
 stay = false; // this value is used to exit from evoking, not from here 
 break; // that use break
 }
 }
 else
 {
 fluxInputError();
 continue;
 }
 }
}
//data insertion function in the two vectors 
void insertData(std::string &name, int &score, std::vector<std::string> &names, std::vector<int> &scores)
{
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 while (true) 
 {
 if (std::cin >> name >> score) // check if cin is valid
 {
 if (name == "NoName" && score == 0) // as required by the exercise. I didn't use my custom checkExit function.
 {
 break;
 }
 names.push_back(name);
 scores.push_back(score);
 for (int i = 0; i < names.size(); ++i)
 {
 for (int x = 0; x < names.size(); ++x)
 {
 if (i != x && x < names.size() && names[i] == names[x])
 {
 //std::cout << "\nError! " << names[x] << " is entered twice\n\n";
 //names.erase(names.begin() + x);
 //scores.erase(scores.begin() + x);
 // instead of giving an error message for repeated names as asked from exercixe, 
 // I prefered add the score to the first name entered and remove duplicates
 names.erase(names.begin() + x);
 scores[i] += scores[x];
 scores.erase(scores.begin() + x);
 }
 }
 }
 }
 else
 {
 std::cout << "\nWrong input!\n";
 fluxInputError();
 continue;
 }
 }
}
// selection function
void askingWhatToDo(char& selection)
{
 
 std::cout << "\nWhat do you want to do now?\n\n";
 std::cout << "1) I want to insert some data into the database?\n";
 std::cout << "2) I want to search for a name in the database?\n";
 std::cout << "3) I want to search for a score in the database\n";
 std::cout << "4) I want to see all names and scores\n";
 std::cout << "5) I finished\n\n";
 std::cin >> selection;
}
// function used to search for values ​​by name
void lookinFornName(bool&stay, std::string &name, std::vector<std::string> &names, std::vector<int> &scores) //*
{
 while (stay)
 {
 std::cout << "\nEnter a name\n";
 if (std::cin >> name) 
 {
 bool isNotFound{ true };
 for (int i = 0; i < names.size(); ++i)
 {
 if (name == names[i])
 {
 std::cout << "The score of " << name << " is " << scores[i] << "\n";
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nName not found\n";
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay); 
 }
}
// function used to search for values ​​by score
void lookingForScore(bool& stay, int& score, std::vector<std::string> &names, std::vector<int> &scores) //**
{ 
 
 while (stay)
 {
 
 std::cout << "\nEnter a score\n";
 if (std::cin >> score)
 {
 bool isNotFound{ true };
 for (int i = 0; i < scores.size(); ++i)
 {
 if (score == scores[i])
 {
 std::cout << names[i] << '\n';
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nScore not found\n";
 
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay);
 }
}
// this function shows all the (name, score) pairs present in the two vectors.
void allNamesAndScores(std::vector<std::string> &names, std::vector<int> &scores)
{
 if (names.size() > 0)
 {
 std::cout << "\nNames and scores are; " << "\n\n";
 for (int i = 0; i < names.size(); i++)
 {
 std::cout << names[i] << ',' << scores[i] << '\n';
 }
 }
 else
 { 
 std::cout << "\nThe database is empty\n";
 }
}
int main()
{
 //4 - Exercise - 21)
 
 std::vector<std::string> names;
 std::vector<int> scores;
 std::string name{ 0 };
 int score{ 0 };
 bool isFinished{ false };
 while (!isFinished)
 {
 bool stay{ true };
 char selection{ 0 };
 askingWhatToDo(selection);
 switch (selection)
 {
 case '1':
 insertData(name, score, names, scores);
 break;
 case '2':
 lookinFornName(stay, name, names, scores);
 break;
 case '3':
 lookingForScore(stay, score, names, scores);
 break;
 case '4':
 allNamesAndScores(names, scores);
 break;
 case '5':
 isFinished = true;
 break;
 }
 }
 return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <limits>
// cin flushing function
void fluxInputError() 
{
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// function for controlling exit from submenus
void checkExit(bool &stay) // exit function for internal menus
{
 while (true) 
 {
 std::cout << "\nDo you want to continue?(y/n)\n";
 std::string check{ 0 };
 if (std::cin >> check) // check if cin is valid
 {
 if (check == "y" || check == "Y")
 {
 stay = true; // 
 break;
 }
 else if (check == "n" || check == "N") 
 {
 stay = false; // this value is used to exit from evoking, not from here 
 break; // that use break
 }
 }
 else
 {
 fluxInputError();
 continue;
 }
 }
}
//data insertion function in the two vectors 
void insertData(std::string &name, int &score, std::vector<std::string> &names, std::vector<int> &scores)
{
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 while (true) 
 {
 if (std::cin >> name >> score) // check if cin is valid
 {
 if (name == "NoName" && score == 0) // as required by the exercise. I didn't use my custom checkExit function.
 {
 break;
 }
 names.push_back(name);
 scores.push_back(score);
 for (int i = 0; i < names.size(); ++i)
 {
 for (int x = 0; x < names.size(); ++x)
 {
 if (i != x && x < names.size() && names[i] == names[x])
 {
 //std::cout << "\nError! " << names[x] << " is entered twice\n\n";
 //names.erase(names.begin() + x);
 //scores.erase(scores.begin() + x);
 // instead of giving an error message for repeated names as asked from exercixe, 
 // I prefered add the score to the first name entered and remove duplicates
 names.erase(names.begin() + x);
 scores[i] += scores[x];
 scores.erase(scores.begin() + x);
 }
 }
 }
 }
 else
 {
 std::cout << "\nWrong input!\n";
 fluxInputError();
 continue;
 }
 }
}
// selection function
void askingWhatToDo(char& selection)
{
 
 std::cout << "\nWhat do you want to do now?\n\n";
 std::cout << "1) I want to insert some data into the database?\n";
 std::cout << "2) I want to search for a name in the database?\n";
 std::cout << "3) I want to search for a score in the database\n";
 std::cout << "4) I want to see all names and scores\n";
 std::cout << "5) I finished\n\n";
 std::cin >> selection;
}
// function used to search for values ​​by name
void lookinFornName(bool&stay, std::string &name, std::vector<std::string> &names, std::vector<int> &scores) //*
{
 while (stay)
 {
 std::cout << "\nEnter a name\n";
 if (std::cin >> name) 
 {
 bool isNotFound{ true };
 for (int i = 0; i < names.size(); ++i)
 {
 if (name == names[i])
 {
 std::cout << "The score of " << name << " is " << scores[i] << "\n";
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nName not found\n";
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay); 
 }
}
// function used to search for values ​​by score
void lookingForScore(bool& stay, int& score, std::vector<std::string> &names, std::vector<int> &scores) //**
{ 
 
 while (stay)
 {
 
 std::cout << "\nEnter a score\n";
 if (std::cin >> score)
 {
 bool isNotFound{ true };
 for (int i = 0; i < scores.size(); ++i)
 {
 if (score == scores[i])
 {
 std::cout << names[i] << '\n';
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nScore not found\n";
 
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay);
 }
}
// this function shows all the (name, score) pairs present in the two vectors.
void allNamesAndScores(std::vector<std::string> &names, std::vector<int> &scores)
{
 if (names.size() > 0)
 {
 std::cout << "\nNames and scores are; " << "\n\n";
 for (int i = 0; i < names.size(); i++)
 {
 std::cout << names[i] << ',' << scores[i] << '\n';
 }
 }
 else
 { 
 std::cout << "\nThe database is empty\n";
 }
}
int main()
{
 //4 - Exercise - 21)
 
 std::vector<std::string> names;
 std::vector<int> scores;
 std::string name{ 0 };
 int score{ 0 };
 bool isFinished{ false };
 while (!isFinished)
 {
 bool stay{ true };
 char selection{ 0 };
 askingWhatToDo(selection);
 switch (selection)
 {
 case '1':
 insertData(name, score, names, scores);
 break;
 case '2':
 lookinFornName(stay, name, names, scores);
 break;
 case '3':
 lookingForScore(stay, score, names, scores);
 break;
 case '4':
 allNamesAndScores(names, scores);
 break;
 case '5':
 isFinished = true;
 break;
 }
 }
 return 0;
}
edited body
Source Link
RedM3tal
  • 391
  • 1
  • 7
#include <iostream>
#include <vector>
#include <string>
#include <limits>
// cin flushing function
void fluxInputError() 
{
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// function for controlling exit from submenus
void checkExit(bool &stay) // exit function for internal menus
{
 while (true) 
 {
 std::cout << "\nDo you want to continue?(y/n)\n";
 std::string check{ 0 };
 if (std::cin >> check) // check if cin is valid
 {
 std::cout << "\nI'm inside\n";
 if (check == "y" || check == "Y")
 {
 stay = true; // 
 break;
 }
 else if (check == "n" || check == "N") 
 {
 stay = false; // this value is used to exit from evoking, not from here 
 break; // that use break
 }
 }
 else
 {
 fluxInputError();
 continue;
 }
 }
}
//data insertion function in the two vectors 
void insertData(std::string &name, int &score, std::vector<std::string> &names, std::vector<int> &scores)
{
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 while (true) 
 {
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 if (std::cin >> name >> score) // check if cin is valid
 {
 if (name == "NoName" && score == 0) // as required by the exercise. I didn't use my custom checkExit function.
 {
 break;
 }
 names.push_back(name);
 scores.push_back(score);
 for (int i = 0; i < names.size(); ++i)
 {
 for (int x = 0; x < names.size(); ++x)
 {
 if (i != x && x < names.size() && names[i] == names[x])
 {
 //std::cout << "\nError! " << names[x] << " is entered twice\n\n";
 //names.erase(names.begin() + x);
 //scores.erase(scores.begin() + x);
 // instead of giving an error message for repeated names as asked from exercixe, 
 // I prefered add the score to the first name entered and remove duplicates
 names.erase(names.begin() + x);
 scores[i] += scores[x];
 scores.erase(scores.begin() + x);
 }
 }
 }
 }
 else
 {
 std::cout << "\nWrong input!\n";
 fluxInputError();
 continue;
 }
 }
}
// selection function
void askingWhatToDo(char& selection)
{
 
 std::cout << "\nWhat do you want to do now?\n\n";
 std::cout << "1) I want to insert some data into the database?\n";
 std::cout << "2) I want to search for a name in the database?\n";
 std::cout << "3) I want to search for a score in the database\n";
 std::cout << "4) I want to see all names and scores\n";
 std::cout << "5) I finished\n\n";
 std::cin >> selection;
}
// function used to search for values ​​by name
void lookinFornName(bool&stay, std::string &name, std::vector<std::string> &names, std::vector<int> &scores) //*
{
 while (stay)
 {
 std::cout << "\nEnter a name\n";
 if (std::cin >> name) 
 {
 bool isNotFound{ true };
 for (int i = 0; i < names.size(); ++i)
 {
 if (name == names[i])
 {
 std::cout << "The score of " << name << " is " << scores[i] << "\n";
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nName not found\n";
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay); 
 }
}
// function used to search for values ​​by score
void lookingForScore(bool& stay, int& score, std::vector<std::string> &names, std::vector<int> &scores) //**
{ 
 
 while (stay)
 {
 
 std::cout << "\nEnter a score\n";
 if (std::cin >> score)
 {
 bool isNotFound{ true };
 for (int i = 0; i < scores.size(); ++i)
 {
 if (score == scores[i])
 {
 std::cout << names[i] << '\n';
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nScore not found\n";
 
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay);
 }
}
// this function shows all the (name, score) pairs present in the two vectors.
void allNamesAndScores(std::vector<std::string> &names, std::vector<int> &scores)
{
 if (names.size() > 0)
 {
 std::cout << "\nNames and scores are; " << "\n\n";
 for (int i = 0; i < names.size(); i++)
 {
 std::cout << names[i] << ',' << scores[i] << '\n';
 }
 }
 else
 { 
 std::cout << "\nThe database is empty\n";
 }
}
int main()
{
 //4 - Exercise - 21)
 
 std::vector<std::string> names;
 std::vector<int> scores;
 std::string name{ 0 };
 int score{ 0 };
 bool isFinished{ false };
 while (!isFinished)
 {
 bool stay{ true };
 char selection{ 0 };
 askingWhatToDo(selection);
 switch (selection)
 {
 case '1':
 insertData(name, score, names, scores);
 break;
 case '2':
 lookinFornName(stay, name, names, scores);
 break;
 case '3':
 lookingForScore(stay, score, names, scores);
 break;
 case '4':
 allNamesAndScores(names, scores);
 break;
 case '5':
 isFinished = true;
 break;
 }
 }
 return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <limits>
// cin flushing function
void fluxInputError() 
{
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// function for controlling exit from submenus
void checkExit(bool &stay) // exit function for internal menus
{
 while (true) 
 {
 std::cout << "\nDo you want to continue?(y/n)\n";
 std::string check{ 0 };
 if (std::cin >> check) // check if cin is valid
 {
 std::cout << "\nI'm inside\n";
 if (check == "y" || check == "Y")
 {
 stay = true; // 
 break;
 }
 else if (check == "n" || check == "N") 
 {
 stay = false; // this value is used to exit from evoking, not from here 
 break; // that use break
 }
 }
 else
 {
 fluxInputError();
 continue;
 }
 }
}
//data insertion function in the two vectors 
void insertData(std::string &name, int &score, std::vector<std::string> &names, std::vector<int> &scores)
{
 while (true) 
 {
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 if (std::cin >> name >> score) // check if cin is valid
 {
 if (name == "NoName" && score == 0) // as required by the exercise. I didn't use my custom checkExit function.
 {
 break;
 }
 names.push_back(name);
 scores.push_back(score);
 for (int i = 0; i < names.size(); ++i)
 {
 for (int x = 0; x < names.size(); ++x)
 {
 if (i != x && x < names.size() && names[i] == names[x])
 {
 //std::cout << "\nError! " << names[x] << " is entered twice\n\n";
 //names.erase(names.begin() + x);
 //scores.erase(scores.begin() + x);
 // instead of giving an error message for repeated names as asked from exercixe, 
 // I prefered add the score to the first name entered and remove duplicates
 names.erase(names.begin() + x);
 scores[i] += scores[x];
 scores.erase(scores.begin() + x);
 }
 }
 }
 }
 else
 {
 std::cout << "\nWrong input!\n";
 fluxInputError();
 continue;
 }
 }
}
// selection function
void askingWhatToDo(char& selection)
{
 
 std::cout << "\nWhat do you want to do now?\n\n";
 std::cout << "1) I want to insert some data into the database?\n";
 std::cout << "2) I want to search for a name in the database?\n";
 std::cout << "3) I want to search for a score in the database\n";
 std::cout << "4) I want to see all names and scores\n";
 std::cout << "5) I finished\n\n";
 std::cin >> selection;
}
// function used to search for values ​​by name
void lookinFornName(bool&stay, std::string &name, std::vector<std::string> &names, std::vector<int> &scores) //*
{
 while (stay)
 {
 std::cout << "\nEnter a name\n";
 if (std::cin >> name) 
 {
 bool isNotFound{ true };
 for (int i = 0; i < names.size(); ++i)
 {
 if (name == names[i])
 {
 std::cout << "The score of " << name << " is " << scores[i] << "\n";
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nName not found\n";
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay); 
 }
}
// function used to search for values ​​by score
void lookingForScore(bool& stay, int& score, std::vector<std::string> &names, std::vector<int> &scores) //**
{ 
 
 while (stay)
 {
 
 std::cout << "\nEnter a score\n";
 if (std::cin >> score)
 {
 bool isNotFound{ true };
 for (int i = 0; i < scores.size(); ++i)
 {
 if (score == scores[i])
 {
 std::cout << names[i] << '\n';
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nScore not found\n";
 
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay);
 }
}
// this function shows all the (name, score) pairs present in the two vectors.
void allNamesAndScores(std::vector<std::string> &names, std::vector<int> &scores)
{
 if (names.size() > 0)
 {
 std::cout << "\nNames and scores are; " << "\n\n";
 for (int i = 0; i < names.size(); i++)
 {
 std::cout << names[i] << ',' << scores[i] << '\n';
 }
 }
 else
 { 
 std::cout << "\nThe database is empty\n";
 }
}
int main()
{
 //4 - Exercise - 21)
 
 std::vector<std::string> names;
 std::vector<int> scores;
 std::string name{ 0 };
 int score{ 0 };
 bool isFinished{ false };
 while (!isFinished)
 {
 bool stay{ true };
 char selection{ 0 };
 askingWhatToDo(selection);
 switch (selection)
 {
 case '1':
 insertData(name, score, names, scores);
 break;
 case '2':
 lookinFornName(stay, name, names, scores);
 break;
 case '3':
 lookingForScore(stay, score, names, scores);
 break;
 case '4':
 allNamesAndScores(names, scores);
 break;
 case '5':
 isFinished = true;
 break;
 }
 }
 return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <limits>
// cin flushing function
void fluxInputError() 
{
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// function for controlling exit from submenus
void checkExit(bool &stay) // exit function for internal menus
{
 while (true) 
 {
 std::cout << "\nDo you want to continue?(y/n)\n";
 std::string check{ 0 };
 if (std::cin >> check) // check if cin is valid
 {
 std::cout << "\nI'm inside\n";
 if (check == "y" || check == "Y")
 {
 stay = true; // 
 break;
 }
 else if (check == "n" || check == "N") 
 {
 stay = false; // this value is used to exit from evoking, not from here 
 break; // that use break
 }
 }
 else
 {
 fluxInputError();
 continue;
 }
 }
}
//data insertion function in the two vectors 
void insertData(std::string &name, int &score, std::vector<std::string> &names, std::vector<int> &scores)
{
 std::cout << "\nEnter a name and a score (enter 'NoName 0' when finished)" << '\n';
 while (true) 
 {
 if (std::cin >> name >> score) // check if cin is valid
 {
 if (name == "NoName" && score == 0) // as required by the exercise. I didn't use my custom checkExit function.
 {
 break;
 }
 names.push_back(name);
 scores.push_back(score);
 for (int i = 0; i < names.size(); ++i)
 {
 for (int x = 0; x < names.size(); ++x)
 {
 if (i != x && x < names.size() && names[i] == names[x])
 {
 //std::cout << "\nError! " << names[x] << " is entered twice\n\n";
 //names.erase(names.begin() + x);
 //scores.erase(scores.begin() + x);
 // instead of giving an error message for repeated names as asked from exercixe, 
 // I prefered add the score to the first name entered and remove duplicates
 names.erase(names.begin() + x);
 scores[i] += scores[x];
 scores.erase(scores.begin() + x);
 }
 }
 }
 }
 else
 {
 std::cout << "\nWrong input!\n";
 fluxInputError();
 continue;
 }
 }
}
// selection function
void askingWhatToDo(char& selection)
{
 
 std::cout << "\nWhat do you want to do now?\n\n";
 std::cout << "1) I want to insert some data into the database?\n";
 std::cout << "2) I want to search for a name in the database?\n";
 std::cout << "3) I want to search for a score in the database\n";
 std::cout << "4) I want to see all names and scores\n";
 std::cout << "5) I finished\n\n";
 std::cin >> selection;
}
// function used to search for values ​​by name
void lookinFornName(bool&stay, std::string &name, std::vector<std::string> &names, std::vector<int> &scores) //*
{
 while (stay)
 {
 std::cout << "\nEnter a name\n";
 if (std::cin >> name) 
 {
 bool isNotFound{ true };
 for (int i = 0; i < names.size(); ++i)
 {
 if (name == names[i])
 {
 std::cout << "The score of " << name << " is " << scores[i] << "\n";
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nName not found\n";
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay); 
 }
}
// function used to search for values ​​by score
void lookingForScore(bool& stay, int& score, std::vector<std::string> &names, std::vector<int> &scores) //**
{ 
 
 while (stay)
 {
 
 std::cout << "\nEnter a score\n";
 if (std::cin >> score)
 {
 bool isNotFound{ true };
 for (int i = 0; i < scores.size(); ++i)
 {
 if (score == scores[i])
 {
 std::cout << names[i] << '\n';
 isNotFound = false;
 }
 }
 if (isNotFound) std::cout << "\nScore not found\n";
 
 }
 else
 {
 std::cout << "\nWrong input!\n"; 
 fluxInputError();
 continue;
 }
 checkExit(stay);
 }
}
// this function shows all the (name, score) pairs present in the two vectors.
void allNamesAndScores(std::vector<std::string> &names, std::vector<int> &scores)
{
 if (names.size() > 0)
 {
 std::cout << "\nNames and scores are; " << "\n\n";
 for (int i = 0; i < names.size(); i++)
 {
 std::cout << names[i] << ',' << scores[i] << '\n';
 }
 }
 else
 { 
 std::cout << "\nThe database is empty\n";
 }
}
int main()
{
 //4 - Exercise - 21)
 
 std::vector<std::string> names;
 std::vector<int> scores;
 std::string name{ 0 };
 int score{ 0 };
 bool isFinished{ false };
 while (!isFinished)
 {
 bool stay{ true };
 char selection{ 0 };
 askingWhatToDo(selection);
 switch (selection)
 {
 case '1':
 insertData(name, score, names, scores);
 break;
 case '2':
 lookinFornName(stay, name, names, scores);
 break;
 case '3':
 lookingForScore(stay, score, names, scores);
 break;
 case '4':
 allNamesAndScores(names, scores);
 break;
 case '5':
 isFinished = true;
 break;
 }
 }
 return 0;
}
More informative title; other minor copy-edits
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322
Loading
deleted 7 characters in body; edited tags
Source Link
RedM3tal
  • 391
  • 1
  • 7
Loading
edited tags
Link
toolic
  • 14.6k
  • 5
  • 29
  • 204
Loading
Source Link
RedM3tal
  • 391
  • 1
  • 7
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /