Мой код приведен ниже:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class object
{
public:
string s;
int a;
void add()
{
cout << "enter name and speed:" << endl;
cin >> s;
cin >> a;
cout << "done" << endl;
}
};
int main()
{
int i = 0;
object o[10];
string line;
string a[10];
// grab a line from standard input
while (getline(cin, line))
{
// break the input in to tokens using a space as the delimeter
istringstream stream(line);
string token;
while (getline(stream, token, ' '))
{
// convert string to all caps
// transform(token.begin(), token.end(), token.begin(), (int(*)(int)) toupper);
a[i++] = token;
// print each token on a separate line
cout << endl << token << endl;
}
}
if (a[0].compare("make") == 0)
{
if (a[1].compare("fan") == 0)
{
o[0].add();
cout << "lol" << endl;
}
}
}
Мой выход:
делать
поклонник
введите имя и скорость:
сделанный
лол
Проблема в том, что я не могу ввести значения name
и speed
. Он пропускает их, и печать done
и lol
. Скажите, как ввести значения name
и speed
.
Ниже представлен текстовый файл.
сделать вентилятор 5
сделать ac 4
Ваш код никогда не покидает первый
Может быть, это должно быть что-то вроде этого?
while (getline(cin, line)) {
// break the input in to tokens using a space as the delimeter
istringstream stream(line);
string token;
bool flag=false;
while (getline(stream, token,' ')) {
// convert string to all caps
//transform(token.begin(), token.end(), token.begin(), (int(*)(int)) toupper);
a[i++]=token;
if(i==10)
flag=true;
// print each token on a separate line
cout <<'\n'<< token << '\n';
}
if(flag)
break;
}
add()
над двумя извлечениями:std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');