[英]Error in output in c++ program
該程序的目的是將文件中的短語讀入矢量並將該短語轉換為Pig Latin。 當在Pig Latin中輸出翻譯的短語時,在短語之后添加額外的“ay”(不應該發生)。 誰能發現為什么會這樣? 重要的是我修復它,因為它影響我需要輸出的豬拉丁短語的總字母和總字符數。 另外,我不是要求任何人為我編寫任何代碼,而是關於如何使我的代碼更少冗余的任何提示。 我的課程成績的一部分是效率,我通常會失去分數。
這是代碼:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <stdio.h>
#include <ctype.h>
using namespace std;
int main()
{
ifstream in;
string word, fileName;
vector <string> phrase;
int length = 0, index = 0;
int totalWords = -1, totalLetters = -3, totalChars;
cout << "PIG LATIN PROGRAM" << endl;
cout << "Which file are you accessing? : ";
cin >> fileName;
fileName += ".txt";
in.open(fileName);
if (in.fail()) cout << "\nFile not found!" << endl;
while(getline(in, word)) phrase.push_back(word);
cout << "Original Phrase: " << phrase[0] << endl;
istringstream iss(phrase[0]);
cout << "Pig Latin phrase: ";
do {
string OGword;
string PLword;
for (int i=0; i < phrase.size(); i++){
iss >> OGword;
totalWords++;
}
if (OGword[0]=='a' || OGword[0]=='A' || OGword[0]=='e' || OGword[0]=='E' || OGword[0]=='i' || OGword[0]=='I' || OGword[0]=='o' || OGword[0]=='O' || OGword[0]=='u' || OGword[0]=='U'){
cout << OGword << "way" << " ";
totalLetters += (OGword.size() + 3);
}
else {
PLword = OGword.substr(index);
length = PLword.length();
PLword.insert(length, "ay");
PLword.insert(length, 1, OGword[index]);
PLword.erase(0, 1);
if (isupper(OGword[0])){
transform(PLword.begin(), PLword.end(), PLword.begin(), ::tolower);
(toupper(PLword[1]));
char upper;
upper = toupper(PLword[0]);
PLword.erase(0, 1);
cout << upper;
}
cout << PLword << " ";
totalLetters += PLword.size();
}
} while (iss);
totalChars = totalLetters + 1;
cout << "\n\nTotal words: " << totalWords << endl;
cout << "Total Letters: " << totalLetters << endl;
cout << "Total Characters: "<< totalChars << endl;
}
該程序的核心循環看起來像這樣(在偽代碼中):
istringstream iss; // Contains line of text.
do {
string OGword;
get_OGword_and_count_totalWords(iss, OGword);
print_pig_latin_of_word(OGword);
} while (iss);
只要iss
沒有出現錯誤,循環就會運行。 特別是,在提取操作失敗之前, iss
不會出現錯誤。 所以事情發生在這樣的循環中:
while
子句經過測試。 iss
仍然很好,因為沒有錯誤發生,即使iss
位於字符串的末尾。 OGword
。 這會失敗,並將OGword
留空(“”)。 while
子句經過測試。 iss
處於錯誤狀態,循環結束。 一種可能的修正了許多的是測試iss
提取字之后立即發生了錯誤。
std::istringstream iss; // Contains line of text
std::string OGword;
while (iss >> OGword) {
increment_word_total();
print_pig_latin_of_word(OGword);
}
在此版本中,操作iss >> OGword
返回iss
,轉換為bool
。 如果在前一次提取過程中出現錯誤,則循環結束而不打印任何內容。
我認為提高可讀性的最佳方法是將代碼分解為更小的函數。 例如,使用格式化並打印Pig Latin的if / else塊,並將其實際放入函數中:
int print_pig_latin_of_word_and_return_total_letters(string_view word);
然后,該函數中的代碼可以進一步細分:
bool starts_with_vowel(std::string_view word);
int print_vowel_word_and_count_letters(std::string_view word);
int print_consonant_word_and_count_letters(std::string_view word);
int print_pig_latin_of_word_and_count_letters(std::string_view word) {
if (starts_with_vowel(word)) {
return print_vowel_word_and_count_letters(word);
} else {
return print_consonant_word_and_count_letters(word);
}
}
我會刪除using namespace std
並將所有std庫名稱寫為std::string
等。這清楚地說明了標准庫中的內容。
該程序對包含多行的輸入文件具有有趣的行為。 有一個for
循環遍歷phrase.size()
,這是輸入行的數量。 這會導致單詞被跳過並且totalWords
不正確。
此語句不執行任何操作,因為toupper
的結果被忽略:
(toupper(PLword[1]));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.