Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm trying to make a program that checks the word I type in against the word I want and gives to different results, unfortunetly it doesn't work. I am not trying to make a password checker just a test to see if I can check to words against each other. Here is my program-Its written in C just in case you needed to know.
#include <stdio.h>
main()
{
int password, pa55w0rd;printf("Enter the password: ");
scanf("%ld", &password);
scanf("%u", &pa55w0rd);if (password == pa55w0rd)
printf("Welcome home sire.");else
printf("Get out you scamming burglar of yore!");
}

There are a few things wrong.
1. When the user enters in a string, like a password, you need a character array to store the string. You've declared a single 'int' variable which isn't suitable to hold the string.
2. When you need to compare strings, the best way to do it is with the strcmp() function.
3. It's spelled 'grammar' not 'grammer.'#include <stdio.h>
int main()
{
char password[32];
char password2[32];
printf("Enter the password: ");
gets(password);
printf("Enter the second password: ");
gets(password2);if(!strcmp(password,password2))
printf("Welcome home sire.\n");
else
printf("Get out you scamming burglar of yore!\n");return 0;
}Good luck.

My compiler told me that the gets function was dangerous and shouldn't be used (i'm serious word for word). is their a reason for this?

There certainly is. The reason is this: The gets function reads data from the keyboard into a string. But it makes NO restriction on the length. So consider this:
char imtooshort[5];
gets(imtooshort);What if I type in something longer than 5 characters? I overflow the buffer! But this isn't "dangerous", it's annoying. The real problem comes when you realize what is located somewhere near the beginning of your code (Where imtooshort is defined): the return code. When your C function ends, it takes some return data from the stack (if you don't know what that is don't worry about it). Basically, by overflowing imtooshort with a certain stream it would be possible to get the program to start some other code...like a virus. IT's also possible to execute certain superuser programs in linux without proper authorization.
Long chunk of information short: the gets function, if misused, could let Very Bad People do Very Bad Things to your computer system.
But I wouldn't worry about it too much. A better solution, however, would be to use a function like this:
void safegets(char *ptr,int length)
{
int n;
for(n=0;n<length;n++)
{
*ptr=getchar(); //I think this is the right function
if(*ptr==13) //again im guessing on the 13, ill check
break;
}
*ptr=0;
}This is a gets() function that stops after "length" characters. IT should be safe, as long as you choose a good length for your buffer size. I'll write a better function and post back later. But you get the idea..

Bird - rather than writing a new function, why not use the one already included in the standard C library, fgets()
char* = fgets(char *buf, int size, FILE *stream);
The trick is to pass 'stdin' as the FILE* so that you can read from the keyboard.
#define MAXBUF 100
char buffer[MAXBUF];fgets(buffer, MAXBUF, stdin);
This will read from the keyboard and stops on one of three conditions:
- end of file is reached
- a new line is reached
- when MAXBUF - 1 characters have been read

you had it right there!
you didnt say where the comparison word was comming from
if it came from the keyboard at the same time use this code below
if not you will have to create a text file and place the comparison word in it some way then read it back and compare it#include <stdio.h>
main()
{
int password, pa55w0rd;printf("Enter the password: ");
scanf("%ld", &password);
printf("Press enter: ");printf("Enter the password: ");
scanf("%u", &pa55w0rd);
printf("Press enter: ");if (%id == %u)
printf("Welcome home sire.");else
printf("Get out you scamming burglar of yore!");
}to be honest i skipped over C and went to C++
so my comparison may be off im used to ussing Cin And Cout not scanf and printf
but the basic logic is here
My Page has many quick explainations of basic commands
Feel free to contribute Or enlighten me with code examples that are not on my page
I am particularly interested in learning hardware periph

![]() |
Local variable issues
|
Where do i start??????
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |