0

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

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

Thank you.

PS: The code is tested on OpenVMS

Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
Mitro
  • 1,230
  • 8
  • 32
  • 61

3 Answers3

5

It's because the scanf call at the end of the loop doesn't read the newline. Instead this newline is read by your gets call.

A simple solution is to add a space to the end of the scanf format string, like so:

scanf("%d ", &j);

This will make scanf skip trailing whitespace in the input.

Another solution is to put an extra fgets after the scanf, but then don't add the extra space in the format string:

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

Or use fgets to get the line, and then use sscanf to extract the answer:

fgets(testo, sizeof(testo), stdin);
sscanf(testo, "%d", &j);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • If I put the space, when I execute the programme everything Ok, but after that I type 1 e give enter, Write the text isn't shown. – Mitro Mar 11 '13 at 14:01
  • And If I type 2 after I have to give enter again (twice) – Mitro Mar 11 '13 at 14:02
  • @AlessioMTX I'm not sure I follow what you mean? – Some programmer dude Mar 11 '13 at 14:17
  • To exit from the programme I must type a number different from 1, so I type 2, the I press enter but I must type two times to terminate the programme – Mitro Mar 11 '13 at 14:23
  • Now with I tried with sscanf(testo, "%d", &j); (that I'd never used) but now the programme if I type a number different from 1 terminates easily and correctly, but if I type 1 I can't write the text, I've already the message You haven't witten any word – Mitro Mar 11 '13 at 14:26
  • @AlessioMTX If you don't use the solutions provided in my answer, the `gets` at the top of the loop will read the remaining newline in the buffer. I suggest you run your program in the debugger, and step through the code line by line while watching e.g. the variables `j` and `testo`. – Some programmer dude Mar 11 '13 at 14:30
  • ok I'll try with debug, however, now to do a test I put gets(testo); twice, the first time of the loop I must write twice, instead the second time (second loop) only once – Mitro Mar 11 '13 at 14:38
  • I was going to upvote, but then I saw you proposed not one, but two solutions that use `gets`... :| – netcoder Mar 11 '13 at 14:56
  • @netcoder I knew it would come back and bind me in the... back. – Some programmer dude Mar 11 '13 at 14:59
  • 1
    @JoachimPileborg: That's exactly what `gets()` does to people, and why it must be avoided. ;-) – netcoder Mar 11 '13 at 15:00
  • So is better don t use gets?? What should I use? Now I m a bit confused – Mitro Mar 12 '13 at 11:45
  • @AlessioMTX If you see my updated answer, you see what you should be using instead. `fgets` works almost like `gets`, but is safer. The "almost" part is that `fgets` puts the newline into the buffer, while `gets` does not. – Some programmer dude Mar 12 '13 at 12:01
1

Or you can try using the function flushall() just before gets()

SureshS
  • 589
  • 8
  • 23
1

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

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() 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.


Other issues of note:

  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
Mike
  • 47,263
  • 29
  • 113
  • 177
  • Thank you, I' m going to try. For the third point I forget to put %d during the translation ;) – Mitro Mar 14 '13 at 14:23
  • 1
    @AlessioMTX - Yes, I assumed the missing `%d` was something forgot in translation, seems to obvious to be missed. Good luck getting it working, let me know if you have any follow up questions. Ciao. – Mike Mar 14 '13 at 14:46