Pep*_*791 2 c c++ string input
在C中,假设我需要从字符串中获取输入
int num,cost;
char *name[10];
printf("Enter your inputs [quantity item_of_name at cost]");
scanf("%d%*c%s%*c%*s%*c%d",&num,name[0],&cost);
printf("quantity of item: %d",num);
printf("the cost of item is: %d",cost);
printf("the name of item is: %d",name[0]);
Run Code Online (Sandbox Code Playgroud)
INPUT
12本书1本
OUTPUT
项目数量为:1
物品的成本是:12
项目名称为:book
现在我想在C++中做同样的事情.我不知道如何接近.gets()返回整个字符串.是否有任何我错过的特定函数?请帮忙.
int num,cost;
std::string name;
std::cout << "Enter your inputs [quantity item_of_name at cost]: ";
if (std::cin >> num >> name >> cost)
{ } else
{ /* error */ }
Run Code Online (Sandbox Code Playgroud)
您将需要添加错误处理