fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. const size_t SIZE=256;
  7.  
  8. int main() {
  9. int rank;
  10. string names, grades;
  11. // int grades[SIZE];
  12.  
  13. istringstream inFile{"125 {John_BROWN_Biology} {100_81}\n245 {Amanda_SMITH_Chemistry} {83_74}\n436 {Michael_CLARK_Calculus} {35_48}"}; //!!
  14. //inFile.open("records.txt");
  15. string line;
  16.  
  17. while (getline(inFile, line)) // red line by line
  18. {
  19. stringstream sst{line}; // then parse the stream
  20. char delim;
  21.  
  22. sst>>rank; // read an integer
  23. sst>>delim; // skip white and read a single char
  24. if (delim!='{') {
  25. cout<<"Error on line: { expected for name instead of "<<delim<<endl;
  26. continue; // next line, please !!
  27. }
  28. getline(sst,names, '}'); // TO DO: error handling
  29. sst>>delim;
  30. if (delim!='{') {
  31. cout<<"Error on line: { expected for grades instead of "<<delim<<endl;
  32. continue; // next line, please !!
  33. }
  34. getline(sst,grades, '}'); // TO DO: error handling
  35.  
  36. cout << rank<<" "<<names<<" "<<grades<<endl;
  37.  
  38. }
  39. /* int i=0, j=0;
  40. while (!inFile.getline((char*)&rank, 30, '{'){
  41. inFile.getline(st_info, SIZE, '_');
  42. inFile.getline(words_array, SIZE, '_');
  43. }
  44. while (!success1)
  45. {
  46. getline(inFile,Line,'{'); // get values for info array
  47. stringstream iss1;
  48. iss1 << Line; //Put the string into a stream
  49. iss1 >> rank; //To send data as an int.
  50. cout << "STUDENT NUMBER:" << rank << " ";
  51. while (!success2){
  52. // for the info in {} part
  53.   getline(inFile,Line,'_');
  54.   stringstream iss;
  55.   iss << Line;
  56.   iss >> st_info[i];
  57.   cout <<"STUDENT INFO:" << st_info[i] << " ";
  58.   i++;
  59.   if (getline(inFile,Line,'}')){
  60. stringstream iss2;
  61.   iss2 << Line;
  62.   iss2 >> st_info[i];
  63.   cout << st_info[i] << " ";
  64.   i=0;
  65.   success2 = true;
  66.   }
  67.   }
  68.   getline(inFile,Line,'{');
  69.   stringstream iss3;
  70.   iss3 << Line;
  71.   iss3 >> grades[j];
  72.   cout << "GRADES: "<< grades[i] << " ";
  73.   j++;
  74.   while (!success3){
  75. getline(inFile,Line,'_');
  76.   stringstream iss4;
  77.   iss4 << Line;
  78.   iss4 >> grades[j];
  79.   cout << grades[i] << " ";
  80.   j++;
  81.   if (getline(inFile,Line,'}')){
  82. stringstream iss5;
  83.   iss5 << Line;
  84.   iss5 >> grades[j];
  85.   cout << grades[i] << " ";
  86.   j=0;
  87.   success3 = true;
  88.   }
  89.   }
  90. } */
  91. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
125 John_BROWN_Biology 100_81
245 Amanda_SMITH_Chemistry 83_74
436 Michael_CLARK_Calculus 35_48