J
Jobs'R'Us
Hello,
I have a bug in a simple validation for an argument that must be an
int from 1 to 99.
If in the command prompt I enter the following, the program behaves
perfectly:
programname filename.txt doghouse 5
If in the command prompt I enter the following, the program still
behaves perfectly, it explains that the digit ("5all") is incorrect:
programname filename.txt doghouse 5all
The bug is, if I enter the following, the program incorrectly accepts
"5a" and interprets it exactly like like "5":
programname filename.txt doghouse 5a
Someone kindly point out what I'm doing wrong?
Also, I would like to know why gcc gives me a warning about the line
with atoi() being used implicitely?
The code:
// too much in a hurry to remove the ones that do not apply to this
code snippet:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//arguments expected: <a file name> <a string> <a small int>
int main(int argc, char *argv[])
{
FILE *file;
char searchedword[50];
int searcheddigit;
if(argc != 4)
{
printf("Sorry, wrong number of parameters...\n\n");
printf("Usage: blah blah blah\n\n");
return 1;
}
else if(strlen(argv[2]) > 50)
{
printf("Sorry, the searched word is too long...\n\n");
printf("Usage: blah blah blah\n\n");
return 2;
}
else if(strlen(argv[3]) > 2)
{
printf("Sorry, the number is wrong, must be a digit from 1 to
99...\n\n");
printf("Usage: blah blah blah\n\n");
return 3;
}
else
{
file = fopen(argv[1], "r");
if(file == NULL)
{
printf("Sorry, can't open the indicated file.\n\n");
printf("Usage: blah blah blah\n\n");
return 4;
}
else
{
strcpy(searchedword,argv[2]);
searcheddigit = atoi(argv[3]);
if((searcheddigit < 1) || (searcheddigit > 99))
{
printf("Sorry, the number of words to the left and to the right,
needs to be a positive digit between 1 and 99...\n\n");
printf("Usage: blah blah blah\n\n");
return 5;
}
/* Program continues... */
Thank you for the help and please keep your explanations geared for a
seven year old!
Kirst
I have a bug in a simple validation for an argument that must be an
int from 1 to 99.
If in the command prompt I enter the following, the program behaves
perfectly:
programname filename.txt doghouse 5
If in the command prompt I enter the following, the program still
behaves perfectly, it explains that the digit ("5all") is incorrect:
programname filename.txt doghouse 5all
The bug is, if I enter the following, the program incorrectly accepts
"5a" and interprets it exactly like like "5":
programname filename.txt doghouse 5a
Someone kindly point out what I'm doing wrong?
Also, I would like to know why gcc gives me a warning about the line
with atoi() being used implicitely?
The code:
// too much in a hurry to remove the ones that do not apply to this
code snippet:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//arguments expected: <a file name> <a string> <a small int>
int main(int argc, char *argv[])
{
FILE *file;
char searchedword[50];
int searcheddigit;
if(argc != 4)
{
printf("Sorry, wrong number of parameters...\n\n");
printf("Usage: blah blah blah\n\n");
return 1;
}
else if(strlen(argv[2]) > 50)
{
printf("Sorry, the searched word is too long...\n\n");
printf("Usage: blah blah blah\n\n");
return 2;
}
else if(strlen(argv[3]) > 2)
{
printf("Sorry, the number is wrong, must be a digit from 1 to
99...\n\n");
printf("Usage: blah blah blah\n\n");
return 3;
}
else
{
file = fopen(argv[1], "r");
if(file == NULL)
{
printf("Sorry, can't open the indicated file.\n\n");
printf("Usage: blah blah blah\n\n");
return 4;
}
else
{
strcpy(searchedword,argv[2]);
searcheddigit = atoi(argv[3]);
if((searcheddigit < 1) || (searcheddigit > 99))
{
printf("Sorry, the number of words to the left and to the right,
needs to be a positive digit between 1 and 99...\n\n");
printf("Usage: blah blah blah\n\n");
return 5;
}
/* Program continues... */
Thank you for the help and please keep your explanations geared for a
seven year old!
Kirst