#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);


     }

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);

@thanhvy
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.