简体   繁体   中英

Hi, I tried to make this program run multiple times(using for(;;) and do…while) but every time it stops at reading the string

I created this program to read a string from the user and to sort it's word alphabetically, finally I tried to add a funtion for helping me running this program as much as the user wants, but never works. I used do...while, but every time the program stops before reading the string.

char *words[L];
char *word;
char sentence[100];
int i = 0, nrCuvinte = 0;
    int j, k, ok, n, lung;


for(j=0;j<L;++j)
{
    words[j] = (char*)malloc(L*sizeof(char));
}
printf("Enter any sentence you want: \n");

fgets(sentence,99,stdin);
lung = strlen(sentence);

if(sentence[lung-1] == '\n')
{
    sentence[lung-1] = '\0';
}
printf("\n");

word = strtok(sentence, " .,-;/?!");
for(j=0;j<(strlen(word)+1);j++)
{
    word[j] = tolower((unsigned char) word[j]);
}
while(word != NULL)
{
    for(j=0;j<(strlen(word)+1);j++)
    {
        word[j] = tolower((unsigned char) word[j]);
    }
    strcpy(words[i],word);
    word = strtok(NULL, " .,-;/?!");
    ++i;
    ++nrCuvinte;

}

n = nrCuvinte-1;
do{
    ok =1;
    for(k=0;k<n;++k)
    {
        if(strcmp(words[k],words[k+1])>0)
        {
            char *aux;
            aux = words[k];
            words[k] = words[k+1];
            words[k+1]= aux;
            ok = 0;
        }
    }
    --n;
}while(n>0&&(ok==0));

for(j=0;j<nrCuvinte;++j)
{
    puts(words[j]);
}
for(j=0;j<L;++j)
{
    free(words[j]);
    words[j]=0;
}

I added the #includes , a #define for L, wrapped everything inside a main function and placed a while(1) loop inside the main function. At the start of the while(1) loop I added nrCuvinte = 0; and i = 0; in order to reset those variables. I also added the ability to break out of the while(1) loop by typing in 'yes' when asked to do so.

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

#define L 500

int main(){

  char user_input[4];
  char *words[L];
  char *word;
  char sentence[100];
  int i = 0, nrCuvinte = 0;
  int j, k, ok, n, lung;

  while(1){
    i = 0;
    nrCuvinte = 0;
    for(j=0;j<L;++j){
      words[j] = (char*)malloc(L*sizeof(char));
    }
    printf("Enter any sentence you want: \n");

    fgets(sentence,99,stdin);
    lung = strlen(sentence);

    if(sentence[lung-1] == '\n'){
      sentence[lung-1] = '\0';
    }
    printf("\n");

    word = strtok(sentence, " .,-;/?!");
    for(j=0;j<(strlen(word)+1);j++){
      word[j] = tolower((unsigned char) word[j]);
    }
    while(word != NULL)
    {
      for(j=0;j<(strlen(word)+1);j++){
        word[j] = tolower((unsigned char) word[j]);
      }
      strcpy(words[i],word);
      word = strtok(NULL, " .,-;/?!");
      ++i;
      ++nrCuvinte;
    }

    n = nrCuvinte-1;
    do{
      ok =1;
      for(k=0;k<n;++k){
        if(strcmp(words[k],words[k+1])>0)
        {
          char *aux;
          aux = words[k];
          words[k] = words[k+1];
          words[k+1]= aux;
          ok = 0;
        }
      }
      --n;
    }while(n>0&&(ok==0));

    for(j=0;j<nrCuvinte;++j){
      puts(words[j]);
    }
    for(j=0;j<L;++j){
      free(words[j]);
    }
    printf("If you want to exit type 'yes', else press enter.\n");
    fgets(user_input,4,stdin);
    if(strcmp(user_input, "yes") == 0)
      break;
  }
}

I also wrote another version, this time wrapping your code inside a function and calling this function from the main function inside a while(1) loop:

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

#define L 500

void sort_sentence(){
  char *words[L];
  char *word;
  char sentence[100];
  int i = 0, nrCuvinte = 0;
  int j, k, ok, n, lung;

  for(j=0;j<L;++j)
  {
    words[j] = (char*)malloc(L*sizeof(char));
  }
  printf("Enter any sentence you want: \n");

  fgets(sentence,99,stdin);
  lung = strlen(sentence);

  if(sentence[lung-1] == '\n'){
    sentence[lung-1] = '\0';
  }
  printf("\n");

  word = strtok(sentence, " .,-;/?!");
  for(j=0;j<(strlen(word)+1);j++){
    word[j] = tolower((unsigned char) word[j]);
  }
  while(word != NULL){
    for(j=0;j<(strlen(word)+1);j++){
      word[j] = tolower((unsigned char) word[j]);
    }
    strcpy(words[i],word);
    word = strtok(NULL, " .,-;/?!");
    ++i;
    ++nrCuvinte;

  }

  n = nrCuvinte-1;
  do{
    ok =1;
    for(k=0;k<n;++k){
      if(strcmp(words[k],words[k+1])>0){
        char *aux;
        aux = words[k];
        words[k] = words[k+1];
        words[k+1]= aux;
        ok = 0;
      }
    }
    --n;
  }while(n>0&&(ok==0));

  for(j=0;j<nrCuvinte;++j){
    puts(words[j]);
  }
  for(j=0;j<L;++j){
    free(words[j]);
  }

}

int clean_stdin()
{
  while (getchar()!='\n');
  return 1;
}

int main(){

  int user_input;
  char c;

  while(1){
    sort_sentence();
    do{
      printf("If you want to exit type '2', if you want to continue type '1'.\n");
    }while(((scanf("%d%c", &user_input, &c) != 2 || c!='\n') && clean_stdin()) ||( user_input != 2 && user_input != 1));
    if(user_input == 2)
      return 0;
  }
}

Edit: Now in the second code example the user will be prompted with a message telling her/him to enter 1 or 2 until he/she inputs 1 or 2, every other input results in getting prompted with the same message again. If 1 is entered the next sort_sentence() function call is executed, if 2 is read the program terminates. Credits go to MOHAMED for his elegant solution on the community wiki. I used his idea here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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