I am just reviewing C++ and I don't know why 3 and 5 are the only working option. I already converted it to if-else statement but still the same issue. Below is the code:
#include <iostream>
using namespace std;
int main()
{
char c, s[50] = {'\0'};
int num;
cout << "Select cin method:" << endl;
cout << "1. cin.get(c);" << endl;
cout << "2. cin.get(s, 10);" << endl;
cout << "3. cin.get(s, 10, '*');" << endl;
cout << "4. cin.getline(s, 10);" << endl;
cout << "5. cin.read(s, 10);" << endl;
cout << "Select: " << flush;
cin >> num;
switch (num) {
case 1:
cin.get(c); // cin >> c;
break;
case 2:
cin.get(s, 10); // cin >> s; max length 10, '\n' as string terminator
break;
case 3:
cin.get(s, 10, '*'); // cin >> s; max length 10, '*' as string terminator
break;
case 4:
cin.getline(s, 10); // cin >> s; max length 10, '\n' as string terminator
break;
case 5:
cin.read(s, 10); // cin >> s; max length 10, records '\n'
break;
default:
break;
}
if (num == 1)
cout.put(c); // cout << s;
if (num >= 2 && num <= 5)
cout.write(s, 15); // cout << s; max length 15
}
Aucun commentaire:
Enregistrer un commentaire