C++ Program to Read and Store Student Data with Input Validation
#include
int main() { string input; string name, id; int mark;
while (true) {
cout << "Type in name;id;mark or Q to stop: ";
getline(cin, input);
if (input == "Q") {
break;
}
size_t pos1 = input.find(';'); // find the first semicolon
size_t pos2 = input.find(';', pos1+1); // find the second semicolon
name = input.substr(0, pos1); // extract the name
id = input.substr(pos1+1, pos2-pos1-1); // extract the id
mark = stoi(input.substr(pos2+1)); // extract the mark and convert to integer
cout << "Name: '" << name << "' ID: '" << id << "' Mark: '" << mark << "'\n";
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/oZUT 著作权归作者所有。请勿转载和采集!