0

I keep getting a segmentation fault whenever I run my program. If I enter menu option 1, it prompts me to enter my username than seg faults. Also it seg faults when I attempt to call other functions as well.

However yesterday when I ran the program identically, there was no issue.

This is the main: (cases 4 and 5 not done)

#include "headerA3.h"


int main () {

    tweet * tweetList;
    int choice;

    do {
        printf ("\n****** MENU ******\n");
        printf ("1. Create a new tweet\n");
        printf ("2. Display tweets\n");
        printf ("3. Search a tweet (by keyword)\n");
        printf ("4. Find how many words are 'stop words'\n");
        printf ("5. Delete the nth tweet\n");
        printf ("6. Save tweets to a file\n");
        printf ("7. Load tweets from a file\n");
        printf ("8. Sort the given linked list on userid\n");
        printf ("9. Exit\n");

        printf ("Choose a menu option: ");
        scanf ("%d", &choice);

        switch (choice) {
        case 1:
            {           
                printf ("Enter a username: ");
                scanf ("\n%[^\n]s", tweetList->user);

                printf ("Enter the user's tweet: ");
                scanf ("\n%[^\n]s", tweetList->text);
            
                createTweet(tweetList);
            }
            break;

        case 2:
            displayTweets(tweetList);
            break;
            
        case 3:
            searchTweetsByKeyword(tweetList);
            break;

        case 4:
            break;

        case 5:
            break;

        case 6:
            saveTweetsToFile(tweetList);
            break;

        case 7:
            loadTweetsFromFile(tweetList);
            break;

        case 8:
            // printf ("Not done");
            break;

        case 9:
            printf ("Thank you for using twitter!\n");
            exit(1);
            break;
        }
    }
    while (choice != 9);

    return 0; 
}

This is the createTweet.c function file:

#include "headerA3.h"

tweet * createTweet(tweet * tweetList) {
    int i;
    int length, ascii = 0;

    length = strlen(tweetList->user);

//  printf ("Username: %s\n", tweetList->user);
    for (i = 0; i < length; i++) {
        ascii = ascii + tweetList->user[i];
    }
//  printf ("ascii = %d", ascii);

    tweetList->id = strlen(tweetList->text) + ascii;

    printf ("\nYour computer generated userid is %d", tweetList->id);

    return tweetList;
}

And this is headerA3.h:

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

typedef struct microtweet{
    int id;                 //unique integer value  
    char user[51];          // the userid / username of the person who wrote the tweet
    char text[141];         // the text of the tweet
   
    struct microtweet *next;  //dynamic connection to the next tweet
} tweet;

// menu functions 
tweet * createTweet( tweet * tweetList);

void displayTweets(tweet * tweetList);

int searchTweetsByKeyword(tweet * tweetList);

void countStopWords(tweet * tweetList);

void deleteTweet(tweet ** tweetList);

void saveTweetsToFile(tweet * tweetList);

void loadTweetsFromFile(tweet ** tweetList);

void sortID (tweet ** head);

// linked list function - required after you create a tweet or load tweets from a file void 
addNodeToList(tweet**tweetList,tweet *  node);

//added a function to help find keywords
int keywordHelper (char *string, char * word);
Dai
  • 141,631
  • 28
  • 261
  • 374
  • You should be using `puts` instead of `printf` for your menu display, and you need to validate the return value of `scanf`. – Dai Apr 01 '22 at 19:24
  • 4
    Where do you initialize `tweetList`? – G.M. Apr 01 '22 at 19:27
  • `scanf ("\n%[^\n]s", tweetList->user);`, is worst than `gets()`. Research `fgets()`. – chux - Reinstate Monica Apr 01 '22 at 19:27
  • `char text[141];` <-- This is incorrect: [the 140 char limit on Twitter was removed years ago](https://arxiv.org/abs/2009.07661). The limit now is now 280, so you'll run into a buffer-overrun. Actually, I don't see you validating the length of tweets anywhere... – Dai Apr 01 '22 at 19:28
  • You don't need `s` after `%[^...]` -- it's not a prefix modifier, it's the whole format string by itself. – Barmar Apr 01 '22 at 19:29
  • 1
    If you turn on compiler warnings, it should warn you about using uninitialized variable... – hyde Apr 01 '22 at 19:49

0 Answers0