Story Builder

mister-fett 0 Tallied Votes 180 Views Share

I just spent about an hour making this story builder to create book and short story outlines. It is written in C. If anyone has suggestions, please reply to this, PM or email me. Anyway, here is the code and .cpp, as well as the exe in a zip file. (This program is written in Ansi C)

EDIT: This was written several years ago, when I was a C newbie. It sucks.

#include <stdio.h>
#include <string.h>
int main()
{
	char name[50];
	char age[50];
	char character[50];
	char stat1[3000];
	char stat2[3000];
	char stat3[200];
	char stat4[300];
	char stat5[30];
	char stat6[30];
	char stat7[30];
	char stat8[250];
	char stat9[250];
	char stat10[250];
	char stat11[250];
	char s1[10];
	char exit[50];
	printf("Welcome to book/short story engine 1.1.\nThis program was made to assist\nusers in coming up with outlines/ideas for books.\nWhat is your name?\n");
	gets(name);
	printf("\nSo, %s, what will the name of your main character be?\n",name);
	gets(character);
	printf("\nSo your main characters name is %s.\nHow old will your main character be in years?\n",character);
	gets(age);
	printf("\nSeeing as your character is %s years old,\nWhat do you think they will look like? Be descriptive.\n",age);
	gets(stat1);
	printf("\nNow you know what your character looks like.\nWhat kind of personality do they have?\nWrite a paragraph that describes their personality,\nand could easily be cut and pasted into a story.\n");
	gets(stat2);
	printf("\nNow you have you characters information.\nList two characters that help them,\nwith a brief description of each,\npressing enter after each name.\n");
	gets(stat3);
	gets(stat4);
	printf("\nGood. Now make up the name of three bad guys,\npressing enter in between names.\nInclude brief descriptions.\n");
	gets(stat5);
	gets(stat6);
	gets(stat7);
	printf("\nGreat! Now, type up a list of the four main events in your story.\nAgain, press enter after each event.\nBe detailed, but not too detailed.\n");
	gets(stat8);
	gets(stat9);
	gets(stat10);
	gets(stat11);
	printf("Great job!\a You,%s,have just created a basic outline\nfor your book or short story.\n An outline by: %s \n %s \n %s : \n %s \n Good Guys:\n %s \n %s \n %s \nPress enter to scroll down.\nYou cannot scroll back up!",name,name,character,age,stat1,stat2,stat3,stat4);
	gets(s1);
	printf("Part Two:\n Bad Guys:\n %s \n %s \n %s \n Story plot: \n %s \n %s \n %s \n %s \n---Press Enter to Exit Program---\n\a",stat5,stat6,stat7,stat8,stat9,stat10,stat11);
	gets(exit);
	return 0;
}

Dani AI

Generated

Quick practical notes for — nice little learning project. The single biggest problem to fix is input handling: using gets() and many fixed char buffers makes the program crash- and exploit-prone. Replace unsafe line input with size-limited I/O or dynamic lines, check return values, and keep the logic that trims the trailing newline so subsequent reads behave correctly. SEI/CERT secure-coding notes. (sei.cmu.edu)

A small, low-friction C fix is to use fgets() and strip the \n. Also detect truncation and flush the remainder of the line so the next read starts clean. Example helper (C):

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

char *read_line(char *buf, size_t sz)
{
    if (!fgets(buf, (int)sz, stdin)) return NULL;
    size_t len = strlen(buf);
    if (len && buf[len-1] == '\n') buf[len-1] = '\0';
    else { int c; while ((c = getchar()) != '\n' && c != EOF); } /* discard rest */
    return buf;
}

See fgets() docs for behavior and corner cases. fgets — cppreference. (en.cppreference.com)

If you want a sturdier solution, use a dynamic reader (POSIX getline) or, in C++, std::string + std::getline. getline will realloc as needed; std::getline works cleanly with std::string and avoids manual buffer management. Example (C++):

#include <iostream>
#include <string>

std::string prompt(const char* q) {
    std::cout << q;
    std::string s;
    std::getline(std::cin, s);
    return s;
}

getline(3) — man page. std::getline — cppreference. (man7.org)

A few finishing tips: group related fields into a struct (character info, allies, villains, plot points) and use dynamic storage instead of dozens of fixed arrays; always check I/O return values; compile with high warnings (-Wall -Wextra) and test with sanitizers (e.g. AddressSanitizer -fsanitize=address) to catch overflows early. AddressSanitizer docs (Clang/GCC). (clang.llvm.org)

These changes keep the simple flow you built while making the program safe and maintainable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.