Validation problem

hi, i'm making a database system for my module project. my database system is about a gaming store where records can be stored if there is any games rental

i have a problem in validating the games console input section. logically, i want it to validate inputs such as PS3, XBOX 360 etc. only. And only those inputs and its lowercase counterparts are acceptable. however, my code didn't seem to work. even if i inputted the correct item, it still displays the warning.

here is the code:

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
void getconsole(void)
{
  int valid;
		do
		{
			fflush(stdin);
			gotoxy(12,6);
			clreol();
			gets(record.console);

			if
			((toupper(record.console[5])=='PS3') || (toupper(record.console[5])=='X360') || (toupper(record.console[5])=='WII') || (toupper(record.console[5])=='PSP') || (toupper(record.console[5]=='NDS')))
			valid=1;

			else
			{
			gotoxy(26,21);
            textcolor(RED);
			cprintf("Accepted consoles are PS3/X360/WII/PSP/NDS only!");
            textcolor(RED);
			getch();
			gotoxy(12,6);
			clreol();
			valid=0;
			}
		}while(!valid);


thanks for any help.
Of what type is record.console?

toupper takes a single character and converts it to uppercase.

'PS3' is not valid, because single quotes indicate a single character, but PS3 is multiple characters.
record.console is of char type, which i have put in the structure section of the program.

so what do i need to do to make PS3 valid? because i want to reserve the case that there might be an input of a lowercase ps3 too.
1
2
3
for (int i = 0; i < 4; ++i)
    record.console[i] = toupper(record.console[i]); // converts character by character to upper
if (strcmp(record.console, "PS3") || strcmp(record.console, "X360") ...
Last edited on
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
void getconsole(void)
{
  int valid;
  int i;
		do
		{
		fflush(stdin);
		gotoxy(13,6);
		clreol();
		gets(record.console);

			for (i=0; i<4; i++)
    		{
				record.console[i] = toupper(record.console[i]);
				if (strcmp(record.console, "PS3") || strcmp(record.console, "X360") || strcmp(record.console, "WII") || strcmp(record.console, "PSP") || strcmp(record.console, "NDS"))
				valid=1;

				else
				{
				gotoxy(26,21);
				textcolor(RED);
				cprintf("Accepted consoles are PS3/X360/WII/PSP/NDS only!");
				textcolor(RED);
				getch();
				gotoxy(12,6);
				clreol();
				valid=0;
				}
			}
		}while(!valid);
}



i still get an error =(
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
void getconsole(void) {
    int valid;
    int i;
    do {
        fflush(stdin);
        gotoxy(13,6);
        clreol();
        gets(record.console); // this isn't safe for input over 4 chars (plus the null)

        for (i=0; i<4; i++) // the for loop is just used to convert the array
            record.console[i] = toupper(record.console[i]);
        if (strcmp(record.console, "PS3") || strcmp(record.console, "X360") || strcmp(record.console, "WII") || strcmp(record.console, "PSP") || strcmp(record.console, "NDS"))
            valid=1;
        else {
            gotoxy(26,21);
            textcolor(RED);
            cprintf("Accepted consoles are PS3/X360/WII/PSP/NDS only!");
            textcolor(RED);
            getch();
            gotoxy(12,6);
            clreol();
            valid=0;
        }
    } while(!valid);
}

and, what's the error?
Last edited on
the validation still doesn't work. an input of 'ps3'/'PS3' still results in the warning sign to appear. what do you propose i change in my code?
strcmp() returns 0 on equality. Your code seems to expect non-zero on equality.
wow, you're right. Guess memory fails me :P
thank you, i just made it return to 0 and it's all right.
Topic archived. No new replies allowed.