Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. vector<string> split(const string& s, const char delimiter, bool ignore_empty = false){
  9. vector<string> result;
  10. string tmp = s;
  11.  
  12. while(tmp.find(delimiter) != string::npos)
  13. {
  14. string new_part = tmp.substr(0, tmp.find(delimiter));
  15. tmp = tmp.substr(tmp.find(delimiter)+1, tmp.size());
  16. if(not (ignore_empty and new_part.empty()))
  17. {
  18. result.push_back(new_part);
  19. }
  20. }
  21. if(not (ignore_empty and tmp.empty()))
  22. {
  23. result.push_back(tmp);
  24. }
  25. return result;
  26. }
  27.  
  28. bool is_number(const string& s)
  29. {
  30. string::const_iterator it = s.begin();
  31. while (it != s.end() && isdigit(*it)) ++it;
  32. return !s.empty() && it == s.end();
  33. }
  34.  
  35.  
  36. int main()
  37. {
  38. string tiedoston_nimi = "";
  39. cout << "Input file: ";
  40. getline(cin, tiedoston_nimi);
  41.  
  42. ifstream tiedosto_olio(tiedoston_nimi);
  43. if ( not tiedosto_olio )
  44. {
  45. cout << "Error: the input file cannot be opened" << endl;
  46. return EXIT_FAILURE;
  47. }
  48.  
  49. string rivi;
  50. while(getline(tiedosto_olio, rivi))
  51. {
  52. vector<string> rivi_vektori = split(rivi, ';');
  53.  
  54. for (unsigned long int k = 0; k < rivi_vektori.size()-1; ++k)
  55. {
  56. cout << rivi_vektori.at(k) << endl;
  57. }
  58.  
  59.  
  60. if (rivi_vektori.size() != 3 or !(is_number(rivi_vektori.at(3)) or rivi_vektori.at(3) == "on-the-shelf"))
  61. {
  62. cout << "Error: the file has an erroneous line" << endl;
  63. }
  64. else
  65. {
  66. for (int i = 0; i < 3; ++i)
  67. {
  68. if (rivi_vektori.at(i) == "")
  69. {
  70. cout << "Error: the file has an erroneous line" << endl;
  71. }
  72. }
  73. }
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement