【发布时间】:2020-04-03 13:09:46
【问题描述】:
我目前正在编写一个程序,将普通文本翻译成鲸语(如果你想知道鲸语是什么,它是用 u's 和 e's 加倍的辅音撕裂的语句)
我成功编写了程序。但我希望它再次重复并经历相同的过程,直到我们手动退出它。
为此,我使用了一个 while 循环并将 exit 作为变量。但它没有像第二次那样按预期工作,它没有要求输入,而是显示提示文本并跳到退出/不退出行。 你能告诉我我的程序有什么问题吗?这是我的代码(你可以尝试自己编译执行):
#include <iostream>
#include <string>
#include <vector>
int main(){
bool exit = false;
//Open
std::cout<<"==========\n";
std::cout<<"WHALE TALK\n";
std::cout<<"==========\n";
std::cout<<"\n";
//main loop
while (!exit){
//Variables
std::vector<char> whaletalk;
//input
std::string input;
std::cout<<"Type the text you want to translate to whale language: ";
getline(std::cin,input);
std::cout<<"\n\nWhale talk: ";
//vowels
std::vector <char> vowels = {'a','e','i','o','u'};
//sorter
//iterating through string
for (int i = 0; i < input.size(); i++){
//iterating through vowels
for (int j = 0; j < vowels.size(); j++){
//case of vowels
if(input[i] == vowels[j]){
//case of u and e
if ((input[i] == 'u') or (input[i] == 'e')){
whaletalk.push_back(input[i]);
whaletalk.push_back(input[i]);
}
//case of vowels other than u and e
else {whaletalk.push_back(input[i]);}
}
}
}
//Output
for (int k = 0; k < whaletalk.size(); k++ ){
std::cout<<whaletalk[k];
}
std::cout<<"\n";
// exit/no exit
std::string response;
std::cout<<'\n';
std::cout<<"Do you have more to translate?(yes/no)\n\nYour response: ";
std::cin>>response;
if (response == "NO" or response == "no" or response == "No"){
exit = true;
}
}
}
【问题讨论】:
-
这篇博文对你来说可能是一个好的开始:ericlippert.com/2014/03/05/how-to-debug-small-programs
-
@ThomasSablik 哦,谢谢 :) 我不知道!我会删除我的评论以免混淆其他人
标签: c++ loops c++11 input while-loop