• Notice regarding image hosts and attachments. See here.

What's wrong with this C code?

Caporegime
Joined
12 Mar 2004
Posts
29,964
Location
England
Code:
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char * argv[]) {
	if (argv[0] == "" || argv[1] == "" || argv[2] == "") {
		printf("ERROR! Missing command line arguments");
	} else {
		int graduationage = graduateAge(atoi(argv[2]));
		printf("Name: %s\nAge: %s\nAge upon graduation: %d\n", argv[1], argv[2], graduationage);

		int i;
		for (i = 0; i < graduationage; i++) {
			printf("%s\n", argv[1]);
		}
	}
	system("pause");
        return 0;
} 

int graduateAge(int age) {
	return age + 2;
}

Keeps saying "i" is undeclared. :mad:
 
Last edited:
Thanks everyone, moving it to the start of the block fixed it. The program I am using is Microsoft Visual Studio 2008 which is awful but we can't use any other program on the uni pc's like devc++.
 
The problem is visual studio has caused a number of problems with dissappearing files. Does it really matter if it's a little out of date when using a language decades old?
 
Also, why are you checking to see if argv[0], argv[1] and argv[2] are empty?

argv[0] will never be empty - it's the executable you are running - and if either argv[1] or argv[2] are empty argc will be less than three and therefore the wrong number of arguments.

I removed it later, it was just for some diagnostic thing that I forgot to clean up.
 
The uni seem to want it according to the intranet
Returning 0 indicates success. A program should return 0 when it has finished successfully. Note: the 'main' function in C is declared as int main not void main as it is in Java, therefore an integer value should always be returned upon completion of the program.
 
Back
Top Bottom