How to automatically go to the next textbox if the input char reached its limit?

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

void gotoxy(int x,int y);

void gets_ex(char*, int max_chars);

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

{

char urname[16], surname[16], fullname[32];

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_ex(urname, 16);

gotoxy(45, 12);

gets_ex(surname, 16);

gotoxy(45, 14);

printf("%s %s",urname,surname);

gets_ex(fullname, 32);

{

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* input, int max_chars)
{

int ch;

int nchar = 0;

do

{

ch = getch();

if(ch != VK_RETURN && nchar < max_chars - 1)



{

putch(ch);

input[nchar] = ch;

nchar++;

}

}

while(ch != VK_RETURN);

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include<windows.h>
#include<conio.h>

void gotoxy(int x,int y);

void gets_ex(char*, int max_chars);

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

{

char urname[16], surname[16], fullname[32];

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_ex(urname, 16);

gotoxy(45, 12);

gets_ex(surname, 16);

gotoxy(45, 14);

printf("%s %s",urname,surname);

gets_ex(fullname, 32);

{

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* input, int max_chars)
{

int ch;

int nchar = 0;

do

{

ch = getch();

if(ch != VK_RETURN && nchar < max_chars - 1)
      
      

{

putch(ch);

input[nchar] = ch;

nchar++;

}

}

while(ch != VK_RETURN);

}
[code]
Last edited on
closed account (3qX21hU5)
You need to do more then just paste your code. Tell us what is wrong with it. What it is doing that it shouldn't be. What you want it to do. Anything you don't understand. Anything you need help with. ect. ect.

You have a baaaaaaaad indentation :)!

new version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# include <windows.h>
# include <conio.h>
# include <cstdio>

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

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

    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_ex(urname, 16);

    gotoxy(45, 12);
    gets_ex(surname, 16);

    gotoxy(45, 14);
    printf("%s %s",urname,surname);
    gets_ex(fullname, 32);

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

    return 0;
}

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

void gets_ex(char* input, int max_chars)
{
    int ch;
    int nchar = 0;

    do
    {
        ch = getch();
        if(nchar==max_chars-1)
        {
            input[nchar] = '\0';
            return;
        }
        if(ch==VK_RETURN && nchar < max_chars - 1)
            input[nchar] = '\0';
        if(ch != VK_RETURN && nchar < max_chars - 1)
        {
            putch(ch);
            input[nchar++] = ch;
        }
    }
    while(ch != VK_RETURN);
}


Hope that helps
I don't know what to do next... it should be?

1
2
3
4
5
else if(nchar == max_chars)
{putch(ch);
input[nchar] = VK_RETURN;
nchar++;
}


I don't know how to call a char as a VK_RETURN -_-
I'm just a newbie :'(
did you compile my code, I think I wrote
new version
,which containes the solution of your problem

check I changed your gets_ex function
Last edited on
wait....ill try to compile :)
thanks...Techno01 :)

but my only problem now is that...why is it, when it show the full name

I can still type some char on that fullname.

is there any way to solve that problem?
I asked my self why you need to get the full name when you will get both the first and the last name ,so that why...

Yes thats because you ask the user to write his full name (after he entered the first and last name !)

check the line 69 gets_ex(fullname, 32); delete this one ,that's it
Last edited on
ok... I got it :)

thanks again!!.. I've learn a lot from you :)

thank you sir!! :))
Topic archived. No new replies allowed.