首页 > 解决方案 > 我似乎在用 C++ 计算元音和辅音时遇到问题,我应该如何让它工作?

问题描述

我必须编写一个程序,该程序使用数组来存储用户输入的字符串的值,并使用元音数组和辅音数组搜索元音和辅音。此外,一般情况下编写代码的技巧也值得赞赏。我遇到的问题是计数时似乎只做案例 A,但退出案例 E 似乎工作正常。当案例 A 发生时,它不算数。谢谢!

#include <iostream>
#include <iomanip>
#include <cstdlib>


int vowelsCount(char *); //count the vowels
int consonantCount(char *); //count the consonants
int totalString(char *); //count how big the word is
void endProgram(); //goodbye function

//Used as the main menu
int main(int argc, char** argv) {

const int VOWEL_SIZE = 10; //amount of vowels
const int CONS_SIZE = 44; //amount of consonants
const int USER_STRING_SIZE = 51; //max character limit

char userString[USER_STRING_SIZE]; //User entered string
char uEndInput = 'y';   
char uAns; // user menu answer



//Get a word from user
std::cout << "Please enter a string (up to 50 characters)" << std::endl;
std::cin.getline(userString, USER_STRING_SIZE); //get the sting from the console


//menu
std::cout <<  "\n\n Please select what you would like to do:\n";
std::cout << "\t A)  Count the vowels of a string.\n";
std::cout << "\t B)  Count the consonants of a string.\n";
std::cout << "\t C)  Count both vowels and consonants of a string.\n" ;
std::cout << "\t D)  Enter another string.\n"; 
std::cout << "\t E)  Exit Program.\n" << std::endl;

std::cin >> uAns; // user inputs answer 

switch(uAns){

case 'A': 
case 'a':{
    std::cout << "The total vowel count is: " << vowelsCount(userString) << " \n";
    break;
}
case 'B':
case 'b':{
    std::cout << "The total consonant count is: " << 
consonantCount(userString) << " \n";
    break;
}
case 'C':
case 'c':   
    std::cout << "The total word count is: " << totalString(userString) << " 
\n";
    break;

/*case 'd':
  case 'D': 
    return main();
    break;
}*/
case 'E':
case 'e':{
    return 0;
    break;
}

    }


return 0;
}

int vowelsCount(char *userVowelPtr ){
char vowelsArray[] = {'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u', 
'\0'};
char *vArrayPtr = vowelsArray;
int numVowels = 0;

while(*userVowelPtr != '\0')
{
    if (*userVowelPtr == *vArrayPtr ){

            numVowels++; //Add one to the count

    }
    userVowelPtr++; //point to next character
    vArrayPtr++; //point to next vowel
    vArrayPtr = vowelsArray; //set vowelsPtr to first element again
}
std::cout << "\n\n";
return numVowels;
}



int consonantCount(char *consPtr ){
char consonantArray[] = {'B', 'b', 'C', 'c', 'D', 'd', 'F', 'f', 'G', 'g', 'H', 'h', 'J', 'j', 'K', 'k', 'L','l', 'M', 'm', 'N', 'n', 
                        'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z', '\0'};
char *cArrayPtr = consonantArray;
int numCons = 0;


while(*consPtr != '\0')
{
    if (*consPtr == *cArrayPtr ){

            numCons++; //Add one to count

    }
    cArrayPtr++;//point to next consonant
    consPtr++;//point to next character
    cArrayPtr = consonantArray; //set vowelsPtr to first element again
}
std::cout << "\n\n";
return numCons;
}


int totalString(char *totalPtr ){
int letters = 0;

while (*totalPtr != '\0'){
    letters++;  
}
std::cout << "The total letter is:" << letters << std::endl;
return letters;
}



void endProgram(){
std::cout << "THank you for using the letter counter, have a good day." << 
std::endl;
}

标签: c++arraysfunctionpointersswitch-statement

解决方案


问题似乎是您每次运行时都会继续重置指针,而不是让它增加。我个人建议将代码更改为在函数中有两个 while 循环。

while(*userVowelPtr != '\0')
{

     while(*vArrayPtr != '\0')
           {
                if(*userVowelPtr == *vArrayPtr )
                {
                       numVowels++; //Add one to the count
                }

                vArrayPtr++
           }

     userVowelPtr++; //point to next character
     vArrayPtr -= sizeof(vArrayPtr); //set vowelsPtr to first element again
}

这样该函数将遍历 vArray 指针的所有值,并将其与 userVowelPtr 的每个单独值进行比较。


推荐阅读