gpt4 book ai didi

c - 我的gets函数有问题吗?我们还没有在类里面考虑过 fgets。只得到

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

例如,当我输入字符串“Hello”时,当我按“a”时,它应该打印“There are 2 vowels”。相反,它说有 0 个。我是编程新手,这是我学习的第一门语言。帮忙?

    /*
Student: Josiah Eleazar T. Regencia
Course: BSIT 1
Subject: SCS 101
Professor: Daniel B. Garcia


Problem definition:
Write a menu program that will count the vowels and consonants in the string

*/

#include <stdio.h> //printf and scanf functions
#include <string.h> //string functions
#include <stdlib.h>
#include <conio.h>

#define PROMPT "Type in a word, a phrase, or a sentence." //instucts the user

/*
Declaring the function for the user's menu
*/
void menu();

/*
Declaration of function needed to count the vowels in the string
*/
int vowel_count(char *stringInput); //declaring the function to count the vowels sounds

/*
Declaration of function needed to count the consonants in the stringn
*/
int consonant_count(char *stringInput); //declaring the functions to count the consonant sounds

/*
Declaring the function needed to convert the streeting to uppercase
*/
int uppercase(char *stringInput);

/*
Declaring the function needed to convert the streeting to uppercase
*/
int lowercase(char *stringInput);

int main () {


char userInput[100]; // the string the user inputs

char commandKey[1]; //this key is for the menu

char newInput[100]; //this is for the new input to user will put in

int stringLength; //to identify the length of the string

/*
Variables for counting the vowels and consonants
*/

int consonantCount;

printf("%s\n\n", PROMPT); //instucts the user

gets(userInput);

stringLength = strlen(userInput); //gets the length of the string

//fgets(userInput, 100, stdin);

/*if(stringLength > 0 && userInput[stringLength - 1] == '\n') {

userInput[stringLength - 1] ='\0';
}*/

menu(); //prints out the menu for the user to pick his options

/*
The loop will run what the user asks the program to run while at the same time also asking
what the programmer wants next
*/
while(*commandKey != 'X' || *commandKey != 'x') {

//int commandLength; //length of the command key

printf("Enter your menu selection: ");

gets(commandKey);

/*commandLength = strlen(commandKey);

fgets(commandKey, 100, stdin);

if(commandLength > 0 && commandKey[commandLength - 1] == '\n') {

commandKey[commandLength - 1] ='\0';
}*/

if(*commandKey == 'A' || *commandKey == 'a') {

int vowelCount;

vowelCount = vowel_count(userInput);

printf("There are %d vowels.\n\n", vowelCount);

}

if(*commandKey == 'B' || *commandKey == 'b') {

consonantCount = consonant_count(userInput);

printf("There are %d consonants.\n\n", consonantCount);
}

if(*commandKey == 'C' || *commandKey == 'c') {

/*
This condition simply converts the input string to all lowercase letters
*/

lowercase(userInput);
}

if(*commandKey == 'D' || *commandKey == 'd') {

/*
This condition simply converts the input string to all lowercase letters
*/

uppercase(userInput);
}

if(*commandKey == 'E' || *commandKey == 'e') {

/*
Prints the current string
if the string was converted in lowercase letters, the outcome would be all lowercase letters
if the string was converted in uppercase letters, the outcome would be all uppercase letters
*/

printf("%s\n\n", userInput);
}

if(*commandKey == 'F' || *commandKey == 'f') {

/*
When the user wants to test a new string, this is the condition for the user
to automatically ask of it
*/

printf("%s\n", PROMPT);
gets(newInput);
strcpy(userInput, newInput);
}

if(*commandKey == 'M' || *commandKey =='m') {

/*
In case the user forgets, this will serve as a reminder of the menu
*/

menu();
}

if(*commandKey == 'X' || *commandKey == 'x') {
printf("Goodbye!\n");
break;
}
}
}

//Function that displays the menu.
void menu() {

/*
These are the set of command keys given to the user
*/
printf("\n\n\n");
printf("PRESS:\n\n");
printf(" A - Count the number of vowels in the string.\n");
printf(" B - Count the number of consonants in the string.\n");
printf(" C - Convert the string to uppercase.\n");
printf(" D - Convert the string to lowecase.\n");
printf(" E - Display the current string.\n");
printf(" F - Enter another string.\n");
printf("\n");
printf(" M - Display this menu.\n");
printf(" X - Exit the program.\n");
printf("\n\n");
}

/*
Defining the function for the vowel counting
*/

int vowel_count(char *stringInput) {

if ( *stringInput == '\0')
return 0;
else
return vowel_count(stringInput + 1) + (*stringInput == 'a' || *stringInput == 'A')
+ (*stringInput == 'e' || *stringInput == 'E')
+ (*stringInput == 'i' || *stringInput == 'I')
+ (*stringInput == 'o' || *stringInput == 'O')
+ (*stringInput == 'u' || *stringInput == 'U');
}

/*
Defining the function for the vowel counting
*/
int consonant_count(char *stringInput) {

if (*stringInput == '\0')
return 0;
else
return consonant_count(stringInput + 1) + (*stringInput == 'b' || *stringInput == 'B')
+ (*stringInput == 'c' || *stringInput == 'C')
+ (*stringInput == 'd' || *stringInput == 'D')
+ (*stringInput == 'f' || *stringInput == 'F')
+ (*stringInput == 'g' || *stringInput == 'G')
+ (*stringInput == 'h' || *stringInput == 'H')
+ (*stringInput == 'j' || *stringInput == 'J')
+ (*stringInput == 'k' || *stringInput == 'K')
+ (*stringInput == 'l' || *stringInput == 'L')
+ (*stringInput == 'm' || *stringInput == 'M')
+ (*stringInput == 'n' || *stringInput == 'N')
+ (*stringInput == 'p' || *stringInput == 'P')
+ (*stringInput == 'q' || *stringInput == 'Q')
+ (*stringInput == 'r' || *stringInput == 'R')
+ (*stringInput == 's' || *stringInput == 'S')
+ (*stringInput == 't' || *stringInput == 'T')
+ (*stringInput == 'v' || *stringInput == 'V')
+ (*stringInput == 'w' || *stringInput == 'W')
+ (*stringInput == 'x' || *stringInput == 'X')
+ (*stringInput == 'y' || *stringInput == 'Y')
+ (*stringInput == 'z' || *stringInput == 'Z');
}

/*
Defining the function for the conversion of the string to all uppercase letters
*/
int uppercase(char *stringInput) {

while(*stringInput) {

*stringInput = toupper(*stringInput);

stringInput++;
}

}

/*
Defining the function for the conversion of the string to all uppercase letters
*/
int lowercase(char *stringInput) {

while(*stringInput) {

*stringInput = tolower(*stringInput);

stringInput++;
}

}

最佳答案

这个循环永远不会停止:

while(*commandKey != 'X' || *commandKey != 'x')

当条件为假时,循环停止。对于 *commandKey != 'X' || *commandKey != 'x' 为假,逻辑上等同于 *commandKey == 'X' && *commandKey == 'x' 为真,但不是相当工作。 *commandKey 不能同时是 'X''x'

相反,您想要:

while(*commandKey != 'X' && *commandKey != 'x')

*commandKey'X''x' 时将停止。

此外,您从未初始化过 commandKey,因此您无法在循环中测试它(使用未初始化变量的值是未定义的行为)。如果你想把它当作一个字符串,你需要声明它至少包含 2 个字符,因为需要空字符:

char commandKey[2] = { 0 };

这会将 commandKey 初始化为一个空字符串。请记住 gets() 将空终止字符串,因此,即使您的命令只是一个键,gets() 也将需要至少 2 个位置来填充 -字符命令和空终止字节。或者,您可以将 commandKey 保持原样(前提是您对其进行了初始化),而改用 getchar(),它会读取一个字符。

关于c - 我的gets函数有问题吗?我们还没有在类里面考虑过 fgets。只得到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22209860/

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