尝试限制字符串中的字符数
Trying to limit the number of characters in the string
我试图将字符串 item1、item2 和 item3 限制为十个字母,如果它们长于 10 个字母,我希望程序生成一个语句并终止程序。我也想用 price1、price2、price3 来做到这一点,但我还没有尝试过这个,因为我被困在第一部分。我认为它应该类似于限制第一项。如何使用条件语句执行此操作? 这是我到目前为止所拥有的:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
string item1, item2, item3;
int ten;
ten=10;
item1<"10";
item2<"10";
item3<"10";
cout << "Enter names of 3 one-word items to purchase (each name must be less than 10 letters long): " <<endl;
cin >> item1 >> item2 >> item3;
if (item>=10)
{
cout<< "This item is more than 10 letters long" <<endl;
return 0;
}
float price1, price2, price3, thousand;
thousand=1000;
cout << "You have purchased 3 items. Enter their prices in US Dollars (must be less than $1,000): " <<endl;
cin >> price1 >> price2 >> price3;
}
std::string::size()
就是你要找的:
if (item1.size() > 10)
{
std::cout << "This item is more than 10 letters long" << std::endl;
return 0;
}
你可能想要这个:
#include <iostream>
#include <string>
using namespace std;
int main() {
string item1, item2, item3;
cout << "Enter names of 3 one-word items to purchase (each name must be less than 10 letters long): " << endl;
cin >> item1 >> item2 >> item3;
if (item1.size() >= 10 || item2.size() >= 10 || item3.size() >= 10)
{
cout << "One of the items is more than 10 letters long" << endl;
return 0;
}
}
或者也许是这个:
#include <iostream>
#include <string>
using namespace std;
int main() {
string item1, item2, item3;
cout << "Enter names of 3 one-word items to purchase (each name must be less than 10 letters long): " << endl;
cin >> item1;
if (item1.size() >= 10)
{
cout << "This item is more than 10 letters long" << endl;
return 0;
}
cin >> item2;
if (item2.size() >= 10)
{
cout << "This item is more than 10 letters long" << endl;
return 0;
}
cin >> item3;
if (item3.size() >= 10)
{
cout << "This item is more than 10 letters long" << endl;
return 0;
}
}
但这很尴尬。
此解决方案使用数组,这可能是您真正想要的:
#include <iostream>
#include <string>
using namespace std;
const int nbofitems = 3; // modify here for changing the number of items
int main() {
string items[nbofitems];
cout << "Enter names of 3 one-word items to purchase (each name must be less than 10 letters long): " << endl;
for (int i = 0; i < nbofitems; i++)
{
string item;
cin >> item;
if (item.size() >= 10)
{
cout << "This item is more than 10 letters long" << endl;
return 0;
}
items[i] = item;
}
cout << "Items entered:n";
for (int i = 0; i < nbofitems; i++)
{
cout << "item " << i << ": " << items[i] << "n";
}
}
但是,仍有很大的改进空间,例如使用std::vector
而不是原始数组。
相关文章:
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 将 NULL 与 C 的字符* 字符串一起使用
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- 无法将常量字符字符串传递给模板类
- 如何组合一个宽字符字符串,中间插入一些空字符
- 如何将二维数组类型字符(字符串)作为函数参数传递?
- 从给定索引返回子字符字符串的函数
- 字符 [](c 字符串)的初始化标准
- 如何将字符字符串用作数学运算符
- 将 Unicode 字符/字符串写入文件
- C++,字符* 字符串修改
- 如何有效地用不同的整数元素替换字符字符串的元素
- C++:如果我们在字符串中添加一些整数,为什么它会从开头删除该数量的字符?(字符串 + 整数)
- strcpy正在复制sth字符i字符串.如何解决此错误
- 在C++中将双精度转换为字符*/字符串
- C 中的宽字符字符串
- 如何提取C 中的字符/字符串之间的字符串
- 无法将字符/字符串转换为int
- 带有指针的反转字符字符串
- 反转字符串中的 n 个字符(字符串中没有空格),而不使用 c++ 中的内置函数