繁体   English   中英

我的程序在到达 `cin.getline()` 函数时突然退出

[英]my program suddenly exiting when it gets to the `cin.getline()` function

我使用getline()函数编写了一个程序。

代码

我认为问题出在这一部分:

    int encrypt(){
    int opt,limit;
    char inp[100],enc[100];
     cout<<"Choose the encryption type\n"
        <<"[1]Simple\n"
        <<"[2]complex\n"
        <<">>>";
    cin>>opt;
    if(opt==1){
    cout<<"Enter string to be encrypted :";
    cin.getline(inp,100);
    limit=strlen(inp);
    for(int i=0;i<limit;i++)
        {
        switch(inp[i])
                {
                case 'a' :  enc[i]='q';
                            cout<<enc[i];
                            break;

                ..........

它打印enter string to be encrypted并突然退出:p

提前致谢

cin>>opt 之后的 cin.ignore() 应该可以完成工作(快速修复)。 @Algirdas 引用了一个链接,该链接可为您提供更多详细信息,这是因为您因 opt 的输入而进入流中的换行符。

int encrypt(){
int opt,limit;
char inp[100],enc[100];
 cout<<"Choose the encryption type\n"
    <<"[1]Simple\n"
    <<"[2]complex\n"
    <<">>>";
cin>>opt;
if(opt==1){
cout<<"Enter string to be encryped :";
cin.ingore() #this can help in clearing the '/n' in stream
cin.getline(inp,100);
limit=strlen(inp);
for(int i=0;i<limit;i++)
    {
    switch(inp[i])
            {
            case 'a' :  enc[i]='q';
                        cout<<enc[i];
                        break;

            ..........

试试看。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM