This program is meant as practice. However, it is not working as intended. The for loop is meant to take the argument from the user and use it as the amount of times to loop. Instead it simply outputting: . It is supposed to output i++ but does not. Any help is much appreciated.
You are not using the command line parameters correctly. Why are you doing something radically different to normal conventions?
The first one is an argument count (argc), including the program name - it is always at least 1. You should always test this at the start - what if argc is 1?
The second one is an array of pointer to char - which holds all the actual arguments. Maybe this form is better:
1 2 3 4
int main (int argc, char *argv[]){
return 0;
}
argv[0] is the program name.
The idiom for doing something n times is this:
1 2 3 4 5
int a; //variable a stays in scope after the for loop
int n = 10;
for(a=0; a < n; a++){
//do something
}
If you use the <= operator, you run the risk of going past the end of an array, or doing 1 more operation than intended. This is a very common source of error.
Can you tell us what the atoi call does, and specifically why it doesn't work with a string literal containing alpha chars?
I would be happier if you changed your code to this:
int main(int argc, char *argv[])
just to keep with the normal convention. Conventions are somewhat important because everyone immediately knows what you mean. I could change it to this:
int main(int Fred, char *Ginger[])
but that wouldn't be very helpful would it?
It would also be better to have your else if & if swapped around - do error checking & validation first.
It would also be better to have your else if & if swapped around - do error checking & validation first.
This in other words: expr1 && expr2
This evaluates expr1 first. Not only that, if expr1 is false it already knows that the AND (&&) will fail and lazily does not even bother to evaluate expr2.
Now, in your code you do dereference arg1[1] and check that arg > 2. arg1[1] exists only if arg > 1. If arg is 1 (you did not give any arguments to the program), there would be no arg1[1] and dereferencing it would be an error. Therefore one should check that value exists before you try to use it, and not the other way around.
1 2 3 4 5
if ( 1 < argc ) {
if ( argv[1] == ...
}
// Shorter, with logical AND:
if ( 1 < argc && argv[1] == ... )
Both are ok, because neither is an assignment. Lets look at the other usual comparison:
1 2 3
if ( 1 == argc ) ..
// or
if ( argc == 1 ) ..
Still fine. Now, I make a typo writing them:
1 2 3
if ( 1 = argc ) ..
// or
if ( argc = 1 ) ..
The compiler will bark about the first, but accept the second and the code probably does unexpected things. Therefore, it is safer to systematically have the const expression on the left side.
The second thing is that for more complex types one probably defines operator< and operator== and then relies templates in <utility> to get the other relational operators. http://www.cplusplus.com/reference/utility/rel_ops/
That makes < teeny weeny bit nicer than >.
@ostar2:
"-c" is a string value. Converting it into std::string enables the use of std::string::operator== (const char *).
The compiler will bark about the first, but accept the second and the code probably does unexpected things. Therefore, it is safer to systematically have the const expression on the left side.
#include <stdio.h>
#include <stdlib.h>
int main(constint arg, constchar *arg1[])
{
if (arg == 3)
{
/* convert both to int to avoid
compilation error and make command match */
if (atoi(arg1[1]) == atoi("-c"))
{
for (int i = 0; i < atoi(arg1[2]); ++i)
{
printf("%i\n",i);
}
}
}
else
{
printf("Usage: %s -c 10\n", arg1[0]);
}
return 0;
}
Always read the reference material about the functions you use.
The atoi (ascii to int) function converts a string literal representing an integer into an int. if you send it something that is not an integer then it returns 0. So the condition is always true when you send both atoi functions alpha chars. So this would be true: