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

void gotoxy(int x,int y);

int main(int argc, char *argv[])


    {


    char urname[1],surname[1],fullname[1];

    start:

    system("COLOR 1c");

    gotoxy(11,9);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");

    gotoxy(11,10);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");

    gotoxy(11,11);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");   

    gotoxy(11,12);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");

    gotoxy(11,13);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");   

    gotoxy(11,14);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");

    gotoxy(11,15);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");   



    gotoxy(14,10);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),121);
    printf(" Please enter your name:      ");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),151);
    printf("                         ");





    gotoxy(14,12);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),121);
    printf(" Please enter your surname:   ");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),151);
    printf("                         ");


    gotoxy(14,14);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),121);
    printf(" Your name is:                ");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),151);
    printf("                         ");



    gotoxy(45,10);
    gets(urname);


    gotoxy(45,12);
    gets(surname);

    gotoxy(45,14);
    printf("%s %s",urname,surname);
    gets(fullname);
{    
     char choice;

   gotoxy(35,11);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
    printf(" Do you want to enter  ");
   gotoxy(35,12);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
    printf(" another entry?        ");
   gotoxy(35,13);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
    printf(" YES                  ");
   gotoxy(40,13);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
    printf("          NO      ");
    choice= getche();
   if (choice=='y'){
   goto start;
}
   else
   printf("BYE");
}





}


void gotoxy(int x, int y){
     COORD coord = {x,y};
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);


     }

Dani AI

Generated

Quick diagnosis and the immediate fixes you need

The core problems in ’s original code are (1) declaring name buffers as char x[1] (no room for characters plus the terminating '\0'), and (2) using gets() which performs no bounds checking. gets() is explicitly unsafe and was removed from the C standard (do not use it). (en.cppreference.com)

Practical, safer approaches

  • Allocate a buffer large enough for the longest expected input plus one byte for the NUL terminator, or use a dynamic reader that grows the buffer for you. On POSIX systems consider getline() which will allocate/resize a buffer automatically; on other systems pick a sensible maximum and enforce it. (man7.org)
  • Prefer fgets() (reads at most N-1 chars and leaves a trailing newline which you should strip) or the C11/Microsoft safer alternative gets_s() where available. Always check the function return value before using the buffer. (en.cppreference.com)

Notes about the thread answers

  • was correct pointing out the tiny arrays and the need for fgets/stdio.h. That is the simplest immediate fix. (en.cppreference.com)
  • ’s gets_ex approach (reading keystrokes with getch) is a workable way to limit visible input, but it omits proper editing support (backspace, arrow keys), and it does not handle Unicode or redirected stdin. If you need richer console editing or Unicode on Windows, use the console APIs such as ReadConsole/ReadConsoleW instead. (learn.microsoft.com)

Quick checklist before shipping

  • Stop using gets(); allocate or obtain a properly sized buffer; read with fgets, getline, or a secure alternative; strip the trailing newline; validate character content and length; and check return values. If you implement raw key-by-key input, explicitly handle backspace and control characters and document that behavior. (en.cppreference.com)

These changes will make the program safe against buffer overruns and behave predictably for users typing and editing their names.

Recommended Answers

All 5 Replies

You can't limit characters by using gets
I create simple function to limits input string

#include <Windows.h>
#include <WinCon.h>
#include <conio.h>
#include <stdlib.h>

void gotoxy(int x,int y);
void gets_ex(char*, int max_chars);

int main(int argc, char *argv[])
{
    char urname[10], surname[10], fullname[20];

start:

    system("COLOR 1c");

    gotoxy(11,9);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");

    gotoxy(11,10);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");

    gotoxy(11,11);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");   

    gotoxy(11,12);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");

    gotoxy(11,13);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");   

    gotoxy(11,14);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");

    gotoxy(11,15);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),291);
    printf("                                                             ");   

    gotoxy(14,10);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),121);
    printf(" Please enter your name:      ");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),151);
    printf("                         ");

    gotoxy(14,12);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),121);
    printf(" Please enter your surname:   ");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),151);
    printf("                         ");


    gotoxy(14,14);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),121);
    printf(" Your name is:                ");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),151);
    printf("                         ");

    gotoxy(45, 10);
    // gets(urname);
    // fgets(urname, 10, stdin);
    gets_ex(urname, 10);

    gotoxy(45, 12);
    //gets(surname);
    gets_ex(surname, 10);

    gotoxy(45, 14);
    printf("%s %s",urname,surname);
    //gets(fullname);
    gets_ex(fullname, 20);

    {    
        char choice;

        gotoxy(35,11);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
        printf(" Do you want to enter  ");
        gotoxy(35,12);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
        printf(" another entry?        ");
        gotoxy(35,13);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
        printf(" YES                  ");
        gotoxy(40,13);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
        printf("          NO      ");
        choice= getche();
        if (choice=='y')
        {
            goto start;
        }
        else
            printf("BYE");
    }
}

void gotoxy(int x, int y)
{
    COORD coord = {x,y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

void gets_ex(char* pszOut, int max_chars)
{
    int ch;
    int nchar = 0;
    memset(pszOut, '\0', max_chars);

    do
    {
        ch = getch();

        if(ch != VK_RETURN && nchar < max_chars - 1)
        {
            putch(ch);
            pszOut[nchar] = ch;
            nchar++;
        }
    }
    while(ch != VK_RETURN);
}

char urname[1],surname[1],fullname[1];

How many people do you know who have a first, middle and last name that consists of only 1 character??? gets() doesn't allocate new memory, you have to do that yourself. You sort of have to guess about how big to make those variables then use fgets(), not gets(), to limit user input like this:

fgets(urname, sizeof(urname), stdin);


thank you sir... it works :)

even if there's no

#include<WinCon.h>
#include<stdlib.h>

>

fgets() is declared in stdio.h not stdlib.h. Your program doesn't need wincon.h because it isn't using anything from that file.

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.