当我在函数中调用一个函数时,它会跳过代码并终止程序

When I call a function within a function it skips code and terminates program?

本文关键字:函数 代码 终止程序 调用 一个      更新时间:2023-10-16

这是一个大程序。我去掉了不必要的代码。我只留下了一个关键函数

当我在任何函数中调用ss();时,函数将控制权交还给main()而不接受字符串。如果我不使用函数来接受字符串,代码就会工作。我看不出有什么问题。

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void ss();
void casechange();
using namespace std;
char str[100];
int main (){
int choice;
cout<<"Make a choice"<<endl; 
cout<<"Press 1 to change the case of alphabets"<<endl;
cout<<"Press 2 to count number of vowels"<<endl;
cout<<"Press 3 to check if entered string is a palindrome or not"<<endl;
cout<<"Press 4 to reverse a string"<<endl;
cout<<"Press 5 to count number of words"<<endl;
cin>>choice;
switch(choice){
case 1: casechange();
    break; 
case 2: vowelcount();
        break;
case 3:pal();
    break;
case 4: rev();
    break;
case 5: wordcount();
    break;
default: cout<<"Wrong choice"<<endl;
 }
 return 0;
}
void casechange(){
ss();
for(int i=0;str[i]!='';i++)
 {
 if(isupper(str[i]))
 str[i]=tolower(str[i]);
 else str[i]=toupper(str[i]);
 }
 puts(str);
}
void ss()
{
cout<<"Enter a string"<<endl;
 gets(str);
}

注。我正在使用代码块。

您要求用户做出选择。用户输入数字 enter。然后你读一个字。enter仍然坐在缓冲中。当涉及到gets时,它将其作为空字符串读取。

还请注意所有关于IO, gets等的评论