字符串,获取并做而 [英] Strings, gets and do while

查看:184
本文介绍了字符串,获取并做而的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做C中的练习,但是当我在有一个问题,我想重复cicle(做一会儿),事实上,如果我键入1节目由顶部再次启动,但它不会停止在获取(德图); 。我试过很多方法可以解决这个bug没有一个解决方案,任何人都可以帮我吗?

I'm doing an exercise in C but I have a problem when at the and I want to repeat the cicle (do while), infact if I type 1 the programme starts again by the top, but it doesn't stop at the gets(testo); . I tried plenty of ways to solve the bug without a solution, can anyone help me?

#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);
}

我知道这是不是因为code此刻非常优化并具有其他错误,但我对这个有问题。

I know that isn't very optimized as code at the moment and has other errors, but I'm having problem on this.

感谢您。

PS:在code是OpenVMS的测试

PS: The code is tested on OpenVMS

推荐答案

您首先最明显的问题是与遗留换行符。当您使用 scanf()的这里:

Your first and most obvious problem is with the left over newline. When you use scanf() here:

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

和使用%d个格式specificer,功能正在寻找一个数字,当你输入一个号码真的是你正在进入一个号码和一个换行符

and you use the %d format specificer, the function is looking for a number, when you enter a number really you're entering a number and a newline character

> 1<enter key>   // which means on stdin you're getting   '1''\n'

scanf()的只有拿起1和叶换行,你的获得()函数,那么拿起,所以它看起来像它的跳跃输入。所有你需要做的是消费的换行符,一是快速修复将与来消费它的getchar()

scanf() only picks up the 1 and leaves the newline which your gets() function then picks up, so it looks like it's skipping the input. All you need to do is consume that newline character, one quick fix would be to consume it with getchar():

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

现在你的程序可以作为你所期望的。

Now your program works as you'd expect.

请注意的其他问题:


  1. 您的主真的应该返回一个 INT 键入,哪怕它只是一个返回0

  2. 您不应该使用得到(),即使在当时的>获得()说的从不使用gets()。这通常是一个很好的迹象都不到。 ;)所以替换与fgets(德图,的sizeof(德图),标准输入),这条线;

  3. 您在这里错过了性能specificer:的printf(,CCH\\ n该文本由%字符\\ n组成的); 所以你得到的垃圾输出,即本来应该是%d个

  1. Your main should really be returning an int type, even if it's just a return 0
  2. You shouldn't be using gets(), even then man page for gets() says Never use gets(). That's usually a good indication not to. ;) So replace that line with fgets(testo, sizeof(testo), stdin);
  3. You missed a performance specificer here: printf("\nThe text is composed by % characters\n", cch); so you're getting garbage output, that should have been %d

这篇关于字符串,获取并做而的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆