-1

So, i'm new to coding in general and i'm learning C, i'm trying to make a program that executes an action according to the number the person types in (For example type 1 to open the browser), and i'm having some trouble with the notepad part. The notepad consists in: you will be able to write anything, and when you press enter it's gonna save it as a txt file. Now the problem: when i execute the code, i can't write when i was suppose to, but yeah, it creates the txt file but without nothing because i couldnt type.

Btw i copy pasted the notepad code into an empty C file and it worked fine


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

#define DATA_SIZE 1000

int main()
{
    char data[DATA_SIZE];
    int selection = 0;
    printf("\t\t\t\t\HELLO, WELCOME TO THE ACTION CENTER\n");
    printf("Select what you want by typing the number according to the list below\n");
    printf("\t1. Type something on notepad\n");
    printf("\t2. Open browser\n");
    printf("\t3. Open Zoom\n");
    scanf("%i", &selection);
    //char data[DATA_SIZE];

    if (selection == 1)
    {
        //char data[DATA_SIZE];
        printf("Opening Notepad...\n");
        FILE * fPtr;
        fPtr = fopen("txtfiles/text1.txt", "w"); // it needs to be in the same folder as your code 
        if (fPtr == NULL)
        {
            printf("Unable to create your txt file\n");
            exit(EXIT_FAILURE);
        }

        printf("NOTEPAD - TYPE WHAT YOU WANT:\n");
        fgets(data, DATA_SIZE,stdin);
        fputs (data, fPtr);
        fclose(fPtr);
        printf("File created successfully.\n");
    }

    if (selection == 2)
    {
        ShellExecute(NULL, "open", "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe" , NULL, NULL, SW_SHOWDEFAULT);
    }

    return 0;
}
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Does this answer your question? [fgets doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf) – Retired Ninja Aug 29 '21 at 02:54
  • 1
    "but without nothing" - I don't know what you mean by this. – Dai Aug 29 '21 at 02:54
  • Don't use notepad, at least download [notepad++](https://notepad-plus-plus.org/downloads/) or [geany](https://www.geany.org/download/releases/) or [VScode](https://code.visualstudio.com/download) -- but note, hacking the `.json` config files to make VScode work can be frustrating. A proper editor will make life much, much easier. – David C. Rankin Aug 29 '21 at 04:43
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 31 '21 at 12:26

1 Answers1

0

When you got the menu, you probably typed "1" and then "enter". The "1" selected the notepad function. Then the "enter" supplied an empty line.

If you want to put in "test" for the notepad, you need to enter "1test" and then hit enter. That will supply "1" for the menu and "test" followed by enter for the line.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278