Question

Can someone help me fix my C code. I am getting segmentation fault along with missing...

Can someone help me fix my C code. I am getting segmentation fault along with missing termination "

/* These are the included libraries. */
#include
#include
#include
#include

void printTriangle(char *str);

int main() {
char sentence[81];
int done = 0;
while(done != 1) {
printf("Please enter the sentence or quit: ");
fgets(sentence, 80, stdin);
if(strcmp(sentence, "quit") == 0)
done = 1;
else
printTriangle(sentence);
}
}

void printTriangle(char *str) {
int index = 0;
int line = 1;
int i;
while(str[index] != ") {
for(i = 1; i <= line; index++) {
if(str[index] = ' ')
continue;
else if(str[index] == ")
break;
printf("%c",str[index]);
i++;
}
line++;
printf("");
}
printf("");
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have answered you question earlier. In the question you have posted here , the while loop inside the printTriangle() seems to be missing the condition. I only see the quotes. I have fixed the code and giving you. I am giving you a screen shot of the code as well to show you the indentation. When we upload code in Chegg site, the indentation gets lost. I hope the screen shot will help you. Please take the code below and before you try to indent it, run it once to see that it actually works. Then you can start indenting it, as shown in screen shot. Let me know in case you still face any issues.

#include <stdio.h>

#include <string.h>

void printTriangle(char *str);

int main(){

char sentence[81];

int done = 0;

while(done != 1){

printf("Enter a sentence(type quit to stop) ");

fgets(sentence, 80, stdin);

if(strcmp(sentence, "quit ") == 0) //comparing along with since fgets() returns string with

done = 1;

else

printTriangle(sentence);

}

}

void printTriangle(char *str){

int index = 0;

int line = 1;

int i;

while(str[index] != ''){

for(i = 1; i <= line; index++){

if(str[index] == ' ')

continue;

else if(str[index] == '')

break;

printf("%c",str[index]);

i++;

}

line++;

printf(" ");

}

printf(" ");

}

Add a comment
Know the answer?
Add Answer to:
Can someone help me fix my C code. I am getting segmentation fault along with missing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to figure out why my C code is outputting "exited, segmentation fault". The...

    I am trying to figure out why my C code is outputting "exited, segmentation fault". The game is supposed to generate 4 random numbers and store them in the "secret" array. Then the user is suppose to guess the secret code. The program also calculates the score of the user's guess. For now, I printed out the random secret code that has been generated, but when the game continues, it will output "exited, segmentation fault". Also, the GetSecretCode function has...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any...

    I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any help would be appreciated. /**** main.c ****/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define WORD_LEN 6 #define TOP 10 char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r"; struct Word { char word[30]; int freq; }; int threadCount; int fileDescriptor; int fileSize; off_t chunk; struct Word* wordArray; int arrIndex = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...

  • When I run this code use ./main How to fix it when program reminds segmentation fault...

    When I run this code use ./main How to fix it when program reminds segmentation fault (core dumped)? #include <iostream> void set_num(int* i_prt, int num) { *i_prt = num; } int main(int argc, char** argv) { int x; set_num(&x, 13); std::cout << "x: " << x << std::endl; int* y; set_num(y, 4); std::cout << "*y:" << *y << std::endl; }

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • Please help modify my C program to be able to answer these questions, it seems the...

    Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...

  • Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include...

    Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include <fcntl.h> static char* args[512]; pid_t pid; int command_pipe[2]; static void waiting(int n); static int command(int input, int first, int last) { int fd[2]; int flag=0; pipe( fd );   pid = fork(); if (pid == 0) { for(int i=0;args[i]!=0;i++) { if(args[i][0]=='>') { fd[1]=open(args[i+1], O_CREAT|O_TRUNC|O_WRONLY, 0644); flag=1; } if(args[i]=='>>' ) { fd[1]=open(args[i+1],...

  • Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression...

    Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...

  • I'm getting errors that i can't figure out. I need help fixing them. particularly focus on...

    I'm getting errors that i can't figure out. I need help fixing them. particularly focus on the errors they are highlighted in bold on the list code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <ctype.h> #include "stack.h" #include "booleanEvaluation.h" #include "booleanWithError.h" /* evaluatePostfix * input: a postfix expression * output: T, F, or E * * Uses a stack to evaluates the postfix expression and returns the result as a string where "T" denotes true and "F" denotes...

  • C Language! Please, comment what each line of code does in the code below and answer...

    C Language! Please, comment what each line of code does in the code below and answer this question: (sentence is fine) 1. Aside from pressing ctrl-(d/z) at the beginning of a line causing the getnchar() function to return NULL, is there any way to enter less than 9 characters? Code: void exer1(void) {    char input[LEN];    char *check; getchar(); // Clearing character buffer. printf("Please enter 9 characters: "); // Prompting the user to enter // a specific input.   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT