전체C++ Beginner - While 반복 반복 첫 번째 반복
그래서 나는이 문제에 대해 정말로 두뇌를 포기하고 있습니다. 필자는 사용자 지정 문자열에서 공백/모음/문자를 계산해야하는 프로그램 섹션을 가지고 있습니다. 이것은 "이미 수업에서 다루었던 것만 사용할 수 있기 때문에 아무도하지 않을 방법을 가르쳐줍니다"라는 과제 중 하나입니다. 그래서 나는 사용자에게 sentinel char (이 경우에는 '#')로 끝나는 텍스트를 입력하게했습니다. 루프는 센티넬이 발생할 때 빠져 나가는 것과 관련하여 멋지게 작동하지만 문자열 [0]에 두 번 반복됩니다. 여기에 코드입니다 : 그래서
i = 0;
characterToBeProcessed = userInputText.at(i);
while (characterToBeProcessed != LOOP_SENTINEL)
{
fout << characterToBeProcessed;
// Convert to lowercase
characterToBeProcessed =
static_cast<char> (tolower(characterToBeProcessed));
// Increment character counters
switch (characterToBeProcessed)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
totalVowelCount++;
totalCharacterCount++;
break;
case ' ':
totalSpaceCount++;
totalCharacterCount++;
break;
default:
totalCharacterCount++;
break;
}
characterToBeProcessed = userInputText.at(i++);
}
이 때 프롬프트에서 입력 :
"Please input a text to be analyzed, ending with the # character: "
Hi there, my friend!#
출력은 : 나는이 프로그램 출력을 .at에 대한 문자를 했어
Below is the text entered by the user:
HHi there, my friend!
Total characters: 21
Total vowels: 5
Total blank spaces: 3
(0) 및 .at (1), 그리고 그 날 올바른 문자를 줄, 난 그냥 루프를 두 번 첫 번째 char 반복하고 그 두 번째 시간 후에 잘 작동하는지 알아낼 수 없습니다. 그렇지 않으면 count/output은 첫 번째 문자가 복제되는 것을 제외하고는 정확합니다. 감사의 마음을 크게 전할 것입니다.
이러한 문제를 해결하는 올바른 도구는 디버거입니다. 스택 오버플로를 묻기 전에 코드를 단계별로 실행해야합니다. 자세한 도움말은 [작은 프로그램 디버깅 방법 (Eric Lippert 작성)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 참조하십시오. 문제를 재현하는 [최소, 완료 및 확인 가능] (http://stackoverflow.com/help/mcve) 예제와 함께 해당 질문을 \ [편집]해야합니다. 디버거. –
스택 오버플로에 오신 것을 환영합니다. [The Tour] (http://stackoverflow.com/tour)를 읽으신 후 [Help Center] (http://stackoverflow.com/help/asking)의 자료를 참조하십시오. 여기에 물어보십시오. –
i ++와 ++ i의 차이점을 고려하십시오. –