Home > Software engineering >  C program don't do all the instructions from main
C program don't do all the instructions from main

Time:12-13

Program logic: the program reads the text entered character by character, saving it into a dynamic array of unique sentences. (this part of the program works fine). Then it displays a prompt and waits for one of the numbers to be entered. After that, it should display all the sentences.

For some reason, my program skips the scanf instruction, and also skips the for loop with the printing of sentences, but at the same time it correctly prints a line with sents number. Why is this happening?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wctype.h>
#include <wchar.h>
#include <locale.h>

#define MEM_STEP 5 * sizeof(wchar_t)


struct Text {
    struct Sentence **text;
    int size;
    int n;
};


struct Sentence {
    wchar_t *str;
    int size;
};


struct Sentence *read_sentence(){
    int size = MEM_STEP;
    wchar_t *buf = (wchar_t*)malloc(size * sizeof(wchar_t));
    wchar_t temp = getwchar();
    int n = 0;
    do{
        if (n >= size - 2 * sizeof(wchar_t)) {
            buf = realloc(buf, (size   MEM_STEP) * sizeof(wchar_t));
            size  = MEM_STEP;
        }
        
        buf[n] = temp;
        temp = getwchar();
        n  ;
    }while (temp != '.' && temp != '\n');
    
    buf[n] = temp;
    buf[n   1] = '\0';
    if (buf[0] == '\n') {
      buf  ;
    }
    struct Sentence *sentence = malloc(sizeof(struct Sentence));
    sentence->str = buf;
    sentence->size = size;
    return sentence;
}


int is_sent_unique(struct Sentence** txt, struct Sentence* sent, int n){
    for(int i = 0; i < n; i  ){
        int k = 0;
        for(int j = 0; j < wcslen(sent->str); j  ){
            if (towupper(txt[i]->str[j]) == towupper(sent->str[j]))
                k  ;
            
        }
        if(k == wcslen(sent->str) && k == wcslen(txt[i]->str))
            return 0;
        
    }
    return 1;
}



struct Text read_text(){
    int size = MEM_STEP;
    struct Sentence **text = malloc(size * sizeof(struct Sentence*));
    struct Sentence *temp;
    int n = 0;
    int nlcount = 0;

    do{
        temp = read_sentence();
        if (n >= size - 2 * sizeof(struct String*)) {
            text = realloc(text, (size   MEM_STEP)* sizeof(struct Sentence *));
            size  = MEM_STEP;
        }

        if(temp->str[0] == '\n' && temp->str[1] == '\0'){
            nlcount  ;
        }else{
            while (temp->str[0] == '\t' || temp->str[0] == ' ' || temp->str[0] == '\n') {
                temp->str  ;
            }
            
            if (is_sent_unique(text, temp, n)){        
                text[n] = temp;
                n  ;
                
            }
            nlcount = 0;
        }    
    }while (nlcount < 2);
    
    struct Text txt;
    txt.text = text;
    txt.size = size;
    txt.n = n;
    return txt;

}



int main()
{
    
    setlocale(LC_ALL, "");
    int func_numb = 0;
    
    
    puts("Enter text:");
    struct Text main_text = read_text();
    
    
    puts("Enter the number of function:\n1.\n2.\n3.\n4.");
   
    scanf("%d", &func_numb);
    
    
    switch (func_numb)
    {
    case 1:
        
        break;
    case 2:
        
        break;
    case 3:
        
        break;
    case 4:
        
        break; 
   
    default:
        printf("Incorrect data\n");
    }
  
 
    
    for (int i = 0; i < main_text.n; i  ) {
        wprintf(L"%s\n", main_text.text[i]->str);
    }
  
  
    printf("Number of sents %d", main_text.n);
  
    for (int i = 0; i < main_text.size; i  ) {
        free(main_text.text[i]);
    }
    free(main_text.text);
    return 0;
}

CodePudding user response:

From your accent I understand you live in a country where "wchar" as opposed to "char" might be useful (understatement. :-) )

But somehow it's an interaction between reading wchars and scanf not. When I change getwchar to getchar in "read sentence" near the top, it starts to work.

I tried using "getchar ()" where your "scanf" was, and it returns "EOF": getwchar in read_sentence closes off the option of using "getchar" and "scanf".

I'm not familiar with getwchar, so I'd say you have to look into how to switch back from "wchar" behaviour so that getchar and scanf work again.

Here is the minimal program that shows this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wctype.h>
#include <wchar.h>
#include <locale.h>



int main()
{
    setlocale(LC_ALL, "");
    
    puts("Enter char:");
    wchar_t ch1 = getwchar();
    
    int ch2 = getchar ();
    printf ("ch1 = %d, ch2=%d.\n", ch1, ch2);
    return 0;
}
  •  Tags:  
  • c
  • Related