字符串、获取和执行 while

2023-12-25

我正在用 C 语言做一个练习,但我在 处遇到问题,我想重复 cicle (做 while),事实上,如果我输入 1,程序会从顶部再次启动,但它不会在gets(testo);。我尝试了很多方法都没有解决该错误,有人可以帮助我吗?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

main(){
        int cch, cw, i, j, w, ord, f; //counter and index
        char testo[80];
        char alfa[50][25];
        char swap[25];

        do{     
                cch=0;
                cw=0;
                j=0;
                w=0;
                f=0;

                for(i=0;i<80;i++){
                        testo[i]='\0';
                }
                printf("Write the text:\n");
                gets(testo);

                //initialization 2d array
                for(i=0;i<50;i++){
                        for(j=0;j<25;j++){
                                alfa[i][j]='\0';
                        }
                }

                j=0;
                //Count word and characters
                if(testo[0]!='\0'){
                        cw=1;   
                        for(i=0;testo[i]!='\0';i++){
                                cch++;
                                if(testo[i]==' '){
                                        cw++;
                                        j++;
                                }
                        }
                }

                if(cch==j){
                        printf("You haven't written any word\n\n");
                }
                else{
                        //Don't count double space
                        for(i=0;i<cch;i++){
                                if(testo[i]==' ' && testo[i+1]==' '){
                                        cw--;
                                }
                        }

                        //Don't count as word if the text start with a space
                        if(testo[0]==' '){
                                cw--;
                                w--;
                        }

                        printf("\nThe text is composed by %d characters\n", cch);
                        printf("The text is composed by %d words\n", cw);

                        if(cw>0){
                                printf("\nUsed words:\n");
                                for(j=0;j<cch;j++){
                                        if(testo[j]==' ' && testo[j+1]==' '){
                                                //nothing to do        
                                        }
                                        else{
                                                if(testo[j]!=' '){
                                                        alfa[w][f]=testo[j];
                                                        f++;
                                                }
                                                else if(testo[j]=='\0'){
                                                        alfa[w][f]='\0';
                                                        f=0;
                                                        w=0;
                                                }
                                                else{
                                                        alfa[w][f]='\0';
                                                        w++;
                                                        f=0;
                                                }
                                        }
                                }

                                for(i=0;i<cw;i++){
                                        printf("%d> %s\n", i+1, &alfa[i]);
                                }

                                //order
                                f=1;
                                printf("\nWords used in alphabetical order:\n");
                                while(f==1){
                                        f=0;
                                        for(i=0;i<cw-1;i++){
                                                ord=strcmp(alfa[i],alfa[i+1]);
                                                if(ord>0){
                                                strcpy(swap,alfa[i]);
                                                strcpy(alfa[i],alfa[i+1]);
                                                strcpy(alfa[i+1],swap);
                                                f=1;
                                                }       
                                        }
                                }

                                for(i=0;i<cw;i++){
                                        printf("%d> %s\n", i+1, alfa[i]);
                                }
                        }
                }

        printf("\nDo you want write another text? (1=yes) -> ");
        scanf("%d", &j);

        }while(j==1);
}

我知道目前代码还不是很优化并且存在其他错误,但我在这方面遇到了问题。

谢谢。

PS:代码在OpenVMS上测试


这是因为scanf循环末尾的调用不会读取换行符。相反,这个换行符是由您读取的gets call.

一个简单的解决方案是在末尾添加一个空格scanf格式化字符串,如下所示:

scanf("%d ", &j);

这将使scanf跳过输入中的尾随空格。

另一种解决方案是添加一个额外的fgets之后scanf,但不要在格式字符串中添加额外的空格:

scanf("%d", &j);
fgets(testo, sizeof(testo), stdin);

Or use fgets获取线路,然后使用sscanf提取答案:

fgets(testo, sizeof(testo), stdin);
sscanf(testo, "%d", &j);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

字符串、获取和执行 while 的相关文章

随机推荐