Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code. I want to get 5 strings from the user and espeak reads each of them when user interred it. But I get `segmentation fault(core dumped)` message.



#include <string.h>
 #include <malloc.h>
 #include <espeak/speak_lib.h>

 int test()
 {

 espeak_POSITION_TYPE position_type;
 espeak_AUDIO_OUTPUT output;
 char *path=NULL;
 int Buflength = 500, Options=0;
 void* user_data;
 t_espeak_callback *SynthCallback;
 espeak_PARAMETER Parm;



 char Voice[] = {"English"};

 int i=0;
 char text[1000];
 unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;


     output = AUDIO_OUTPUT_PLAYBACK;

     espeak_Initialize(output, Buflength, path, Options );
     espeak_SetVoiceByName(Voice);
     const char *langNativeString = "en_US";
     espeak_VOICE voice={0};

         voice.languages = langNativeString;
         voice.name = "US";
         voice.variant = 2;
         voice.gender = 1;
        Size = strlen(text)+1;


 for (i=0; i<5; i++)
 {

 scanf("%s ", &text);

 printf("%s", text);

     espeak_Synth( text, Size, position, position_type, end_position, flags,
     unique_identifier, user_data );
     espeak_Synchronize( );
 fflush(stdout);

 }

 return 0;
 }






 int main(int argc, char* argv[] )
 {
     test();

     return 0;
 }


What I have tried:

I tried some modification but none of them worked. I want the program works like this:

> User input: hi
>
> espeak says: hi
>
> user input: one
>
> espeak says: one
>
> (for 5
> inputs)

But when I try to interring more than 4 characters as input,it gives `segmentation fault` error!
Posted
Updated 12-Dec-17 8:31am
v2

C++
scanf("%s ", &text);

Do not use the addressof operator to get the address of an array, just us the array name thus:
C++
scanf("%s ", text);
 
Share this answer
 
Also, the strlen call needs to be moved inside the loop :
for (i=0; i<5; i++)
{
    scanf( "%s", text );       // I don't think it needs the trailing space
    printf("%s", text);
    size = strlen(text)+1;     // <-- moved this line inside loop
    espeak_Synth( text, size, position, position_type, end_position, flags,
                  unique_identifier, user_data );
    espeak_Synchronize();
    fflush(stdout);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900