gpt4 book ai didi

c++ - 处理 c 字符串时如何用当前输入替换用户的第一个输入

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:11 24 4
gpt4 key购买 nike

我在尝试用其他用户输入替换用户输入时遇到问题。

例如,如果用户输入“我爱狗”,然后我们问他们是否要输入其他字符串,他们输入“我吃了很多冰淇淋”。

如何将用户第一次输入替换为当前输入?

// Function Prototype
int countVowels(char * str);
int countCons(char * str);

int main()
{

const int SIZE = 81; // Max size of the array
//const int V_SIZE = 6; // Size of the vowel array
char newSentence[SIZE];
char userString[SIZE];
//char vowels[V_SIZE] = {'a', 'e', 'i', 'o', 'u'};
char choice; // To hold the menu choice
char *strPtr = userString; // Declare and initialize the pointer
char *sentPtr = newSentence;
// Get the string from the user

cout << "Please enter a string. (Maximum of 80 characters) :\n\n";
cin.getline(userString, SIZE);
do{
// Display the menu and get a choice
cout << "\n\nA. Count the number of vowels in the string \n";
cout << "B. Count the number of consonants in the string \n";
cout << "C. Enter another string \n";
cout << "D. Exit the program \n\n";
cout << "Please make your selection: ";
cin >> choice;

// Menu selections
if (tolower(choice) == 'a')
{
countVowels(userString);
cout << "This string contains " << countVowels(userString);
cout << " vowels. \n";
}
else if (tolower(choice) == 'b')
{
countCons(userString);
cout << "This string contains " << countCons(userString);
cout << " consonants. \n";
}

else if (tolower(choice) == 'c')
{
cout << "Please enter other string: ";
cin.getline(newSentence, SIZE);
userString.replace();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

}
else
{
system("PAUSE");
return 0;
}

} while (choice != 'D');
}

我遇到了菜单选择 C ​​的问题。如何用 newSentence 替换 userString?

最佳答案

我希望您的目的不需要两个数组声明。而不是 cin.getline(newSentence, SIZE);使用 cin.getline(userString, SIZE);它将通过覆盖来保存新字符串。

else if (tolower(choice) == 'c')
{
cout << "Please enter other string: ";
cin.getline(userString, SIZE);
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

}

如果您要寻找其他东西,请定义它。

关于c++ - 处理 c 字符串时如何用当前输入替换用户的第一个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41949393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com