First of all, I'd like to say that this is my first post. I have indeed searched this forum for code snippets and help, but never thought I'd have to post anything, since I could easily search for someone who's had the same problem and fix my own code.

Secondly, I am a newbie programmer. I started learning C this year at my high-school as a preparation course for the Brazilian Programming Olympics. After some time, I decided I really wanted to learn more, so I self-taught C++ by reading books and tutorials. That means: don't go rough on me, step-by-step methods will for sure help me out more then just give me the code.

Finally, regarding the code. I know I am not supposed to use system(); , but I'm just making this code for my fellow friends from school. The code is supposed to be short and preferably a standard library function/command.

I tried to make this code as short as possible. It's part of a bigger project I'm building, but this part is the only one I'm having problems with.

It's supposed to memorize the choice the user inputs. In this case, the console color, greeting and username by writing them down on a .txt file. For the setColor function, one needs to input color ## ("color F0" for example).

The problem is: after I run any of the User or Greet functions, they end up skipping all the functions they were supposed to run. The Color functions work fine (I decided including them on my code since they might be affecting the other functions.

Thanks in advance,


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

using namespace std;

int createColor(void)
    char setColor[10];



    FILE * setColorSettings;

    setColorSettings = fopen("colorSettings.txt", "w");

    if(setColorSettings != NULL)
      fputs(setColor, setColorSettings);


int getColor(void)

    char readColor[10];

    FILE * readColorSettings;

    readColorSettings = fopen ("colorSettings.txt", "r");

    if(readColorSettings != NULL)
      fgets(readColor, 10, readColorSettings);



int setColor()
    char opcColorString[5];

    char yesColorString[] = "yes";
    char noColorString[] = "no";


    printf("Change color? (yes/no)");



    if(strcmp(opcColorString, yesColorString) == 0) createColor();

    if(strcmp(opcColorString, noColorString) == 0) return false;

    return true;


int changeGreet(void)

    char setGreetingString[50];

    printf("Type your new Greeting. To confirm, press Enter.\n\n");


    FILE * setGreetingSettings;

    setGreetingSettings = fopen("setGreeting.txt", "w");

    if(setGreetingSettings != NULL)

        fputs(setGreetingString, setGreetingSettings);



int checkGreet(void)

    char readGreetingString[50];

    FILE * readGreetingSettings;

    readGreetingSettings = fopen("setGreeting.txt", "r");

    if(readGreetingSettings != NULL)

        fgets(readGreetingString, 50, readGreetingSettings);



int setGreet()

    char opcGreetString[10];

    char changeGreetString[] = "change";
    char checkGreetString[] = "check";

    char yesnoGreetString[5];

    char yesGreetString[] = "yes";
    char noGreetString[] = "no";

    char cancelGreetString[] = "cancel";

    printf("Configuration & Settings - Greeting\n\n\tchange = changes the greeting message\n\tcheck = checks the greeting message\n\tcancel = cancels the current procedure\n\n");


    if(strcmp(opcGreetString, changeGreetString) == 0)

        printf("\n\nAre you sure you want to proceed? (yes/no): ");



        if(strcmp(yesnoGreetString, yesGreetString) == 0) changeGreet();
        if(strcmp(yesnoGreetString, noGreetString) == 0) return false;


    if(strcmp(opcGreetString, checkGreetString) == 0) checkGreet();

    if(strcmp(opcGreetString, cancelGreetString) == 0) return false;

    return true;


int checkUser(void)

    char readUserString[30];

    FILE * readUserSettings;

    readUserSettings = fopen("userSettings.txt", "r");

    if(readUserSettings != NULL)

        fgets(readUserString, 30, readUserSettings);



int changeUser(void)
    char setUserString[30];

    FILE * setUserSettings;

    setUserSettings = fopen("userSettings.txt", "w");

    if(setUserSettings != NULL)

        fputs(setUserString, setUserSettings);



int setUser()

    char opcUserString[10];

    char changeUserString[] = "change";
    char checkUserString[] = "check";

    char yesnoUserString[5];

    char yesUserString[] = "yes";
    char noUserString[] = "no";

    char cancelUserString[] = "cancel";

    printf("Configuration & Settings - Username\n\n\tchange = changes the Username\n\tcheck = checks the current username\n\tcancel = cancels current procedure\n\n");


    if(strcmp(opcUserString, changeUserString) == 0)

        printf("\n\nAre you sure you want to proceed? (yes/no): ");



        if(strcmp(yesnoUserString, yesUserString) == 0) changeUser();
        if(strcmp(yesnoUserString, noUserString) == 0) return false;


    if(strcmp(opcUserString, checkUserString) == 0) checkUser();

    if(strcmp(opcUserString, cancelUserString) == 0) return false;

    return true;


int main()

    char opcString[10];

    char setColorString[] = "color";
    char setGreetString[] = "greet";
    char setUserString[] = "user";

    char exitString[] = "exit";


    return false;

    //Took those off for testing purposes.



        if(strcmp(opcString, setColorString) == 0) setColor();
        if(strcmp(opcString, setGreetString) == 0) setGreet();
        if(strcmp(opcString, setUserString) == 0) setUser();

        if(strcmp(opcString, exitString) == 0) return false;



EDIT: I'm using CodeBlocks and running Windows Vista.

Edited by CrazyPixel: Forgot some silly stuff

6 Years
Discussion Span
Last Post by jonsca

As an aside: never use gets. You can input as many characters as you want, regardless of the size of the buffer, potentially knocking a hole into another part of your program.

I had flagged this yesterday to go into C (as the only Cplusplussy thing about it is the using namespace std; .

Can you be more specific about what functions it is skipping?


Like jonsca said, never use gets. It easily overflows. I don't know what your system calls are doing. I also noticed your errors are passing silently. What specific functions aren't being called or aren't running properly?


First, I'd like to thank you all for replying. The functions that are not being called are changeUser, checkUser, changeGreet and checkGreet. The system line is supposed to change the color of the console's background by typing "color ##", as mentioned in here.

I tried using scanf, but it wouldn't work (crashed constantly). And the only option I knew was gets();.


I tried using scanf, but it wouldn't work (crashed constantly). And the only option I knew was gets();

Well, if you're going to make this a C++ program you can use iostream's methods (cin, etc). Since you're doing this for personal use only, you could switch all of your C-strings to std::strings.

What I'm also getting at is that the functions are not being called because you don't seem to invoke them anywhere (through a cursory glance over your program).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.