month_name problems

month_name problems

Page 1 of 1

14 Replies - 926 Views - Last Post: 03 November 2009 - 08:51 AM Rate Topic: -----

#1 erekose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 09

month_name problems

Post icon  Posted 01 November 2009 - 06:51 PM

Was hoping someone could help me out here. Trying to write a prog in C that reads months in from an array using pointers but I can't seem to get it to work. Two things are stopping me. 1) It keeps telling me void main() is wrong when I try to compile and 2) it just plain doesn't seem to want to work (even though I have stared at this for hours and think it should!) Any help or points in the right directions would be greatly appreciated :) Here's the code!

#include <stdio.h>
void main()
{
		int n;
		char *y;
		int i;
		char *month_name(int n);
		{
		static char *name[] = {
				"Illegal month",
				"Jan",
				"Feb",
				"Mar",
				"Apr",
				"May",
				"Jun",
				"Jul",
				"Aug",
				"Sep",
				"Oct",
				"Nov",
				"Dec"
		};
		return (n < 1 || n > 12) ? name[0] : name[n];
}
		printf("Enter month or -1 to quit\n");
		scanf("%d", &i);
		while (i != -1)
				{
						y=month_name(i);
						printf("%d %s \n", y, y);
						printf("Enter month or -1 to quit\n");
						scanf("%d", &i);
				}
}




OH PS: Here's the exact error when I try to compile!
(121) unixs1 $ cc mname.c
"mname.c", line 24: void function cannot return value
"mname.c", line 26: warning: statement not reached
cc: acomp failed for mname.c

This post has been edited by erekose: 01 November 2009 - 07:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: month_name problems

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: month_name problems

Posted 01 November 2009 - 06:57 PM

You can't define a function within main()

Move this block:
char *month_name(int n);
{
static char *name[] = {"Illegal month","Jan","Feb","Mar",
"Apr","May","Jun","Jul",
"Aug","Sep","Oct","Nov","Dec"};
return (n < 1 || n > 12) ? name[0] : name[n];
}


Outside the main method

This post has been edited by erik.price: 01 November 2009 - 06:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,662
  • Joined: 23-August 08

Re: month_name problems

Posted 01 November 2009 - 06:57 PM

void main IS wrong...it's int main().
You're attempting to define a function WITHIN main, and you've got a ; at the end of the definition.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: month_name problems

Posted 01 November 2009 - 06:59 PM

Quote

It keeps telling me void main() is wronf when I try to compile
That's because void main is wrong. There is only one acceptable return value for main and that is int. Pedantically, the only two correct function signatures are int main(void) and int main(int argc, char **argv).

Quote

it just plain doesn't seem to want to work
The computer doesn't have feelings. You wrote code. That code does something. What it does is not what you want. To fix it, you need to gather data on what it does. Then figure out why it does what it does (what code causes such results). Once you have isolated the code that does something different from what you desire, you can then attempt to fix it.

You already gathered data. You have compile errors. Now, you need to read them. Do you know how to read errors?
Was This Post Helpful? 0
  • +
  • -

#5 erekose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 09

Re: month_name problems

Posted 01 November 2009 - 07:51 PM

View Posterik.price, on 1 Nov, 2009 - 05:57 PM, said:

You can't define a function within main()

Move this block:
char *month_name(int n);
{
static char *name[] = {"Illegal month","Jan","Feb","Mar",
"Apr","May","Jun","Jul",
"Aug","Sep","Oct","Nov","Dec"};
return (n < 1 || n > 12) ? name[0] : name[n];
}


Outside the main method



That actually made it worse hehe :)
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: month_name problems

Posted 01 November 2009 - 07:52 PM

Cant be as worst as having a nested function definition
Was This Post Helpful? 0
  • +
  • -

#7 erekose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 09

Re: month_name problems

Posted 01 November 2009 - 08:00 PM

OK so I moved the stuff as suggested outside MAIN and here's how it looks now and following that the compile errors I'm now getting (way more of them hehe).

#include <stdio.h>
int main()
{
		char *y;
		char *month_name();
		int i;
		printf("Enter month or -1 to quit\n");
		scanf("%d", &i);
		while (i != -1)
				{
						y=month_name(i);
						printf("%d %s \n", y, y);
						printf("Enter month or -1 to quit\n");
						scanf("%d", &i);
				}
}
		{
		static char *name[] = {
				"Illegal month",
				"Jan",
				"Feb",
				"Mar",
				"Apr",
				"May",
				"Jun",
				"Jul",
				"Aug",
				"Sep",
				"Oct",
				"Nov",
				"Dec"
		};
		return (n < 1 || n > 12) ? name[0] : name[n];
}



Compile errors:
(42) unixs1 $ cc mname.c
"mname.c", line 18: syntax error before or at: static
"mname.c", line 32: warning: syntax error: empty declaration
"mname.c", line 33: warning: old-style declaration or incorrect type for: n
"mname.c", line 33: zero or negative subscript
"mname.c", line 33: warning: old-style declaration or incorrect type for: name
"mname.c", line 33: syntax error before or at: :
"mname.c", line 33: can not declare variably modified type at file scope
"mname.c", line 33: warning: old-style declaration or incorrect type for: name
"mname.c", line 33: identifier redeclared: name
current : array[-2] of int
previous: array[-2] of int : "mname.c", line 33
"mname.c", line 34: syntax error before or at: }
"mname.c", line 34: cannot recover from previous errors
cc: acomp failed for mname.c
Was This Post Helpful? 0
  • +
  • -

#8 gronk  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 168
  • Joined: 28-October 09

Re: month_name problems

Posted 02 November 2009 - 03:04 AM

Well, it's not just a case of moving the code outside main. All code has to be in named functions (otherwise there's no way to call it). And that's what you aren't doing, hence the errors.
Perhaps you want to add
char *month_name(int n)
before the brace just before "static", and since this is after main you'll need to [a] prototype the function before main, or [b] move the main function to the end. This is because the compiler needs to know how something is defined before it can use it.

This post has been edited by gronk: 02 November 2009 - 03:06 AM

Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: month_name problems

Posted 02 November 2009 - 05:47 AM

#include <stdio.h>

// to exist outside of main, it needs to be in another function.
// eric's function was perfectly valid.
// still, I'll mess with it a little
const char *getMonthName(unsigned int n) {
	static char *monthNames[] = {
		"Jan","Feb","Mar","Apr",
		"May","Jun","Jul","Aug",
		"Sep","Oct","Nov","Dec"};
	if (--n > 11) { return "Illegal month"; }
	return monthNames[n];
}


int main() {
	// char *y;
	// char *month_name(); huh?
	int i = -2;
	while (i != -1) {
		printf("Enter month or -1 to quit\n");
		scanf("%d", &i);
		if (i!=-1) { printf("%d %s \n", i, getMonthName(i)); }
	}
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 erekose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 09

Re: month_name problems

Posted 02 November 2009 - 11:04 AM

While this code is nice and indeed works I think it's missing the point of what my teacher is trying to teach me about pointers using *y, etc. I truly appreciate the input though :) Still trying to get the original one working. I'm slowly narrowing down compile errors and testing putting the month_name function in different places in the program outside of main as everyone suggested. Nothing has worked yet but I'm still chipping away at it!

This post has been edited by erekose: 02 November 2009 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,662
  • Joined: 23-August 08

Re: month_name problems

Posted 02 November 2009 - 11:27 AM

You don't seem to get FUNCTIONS yet, never mind pointers; you may want to go over how to prototype and define functions first.

Post your current code.
Was This Post Helpful? 0
  • +
  • -

#12 erekose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 09

Re: month_name problems

Posted 02 November 2009 - 06:11 PM

I totally agree....something elemental is eluding me for sure! I am a dumba$$ regarding C there's no doubting that! :)

Here's the current(est) code!

#include <stdio.h>
int main()
{
char *y;
char *month_name();
int i;
printf("Enter month or -1 to quit\n");
scanf("%d", &i);
while (i != -1)
{
y=month_name(i);
printf("%d %s \n", y, y);
printf("Enter month or -1 to quit\n");
scanf("%d", &i);
}
}
{
static char *name[] = {
"Illegal month",
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
return (n < 1 || n > 12) ? name[0] : name[n];
}


Was This Post Helpful? 0
  • +
  • -

#13 gronk  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 168
  • Joined: 28-October 09

Re: month_name problems

Posted 03 November 2009 - 02:00 AM

You're not a dumbass, just new to all this and it can be very confusing.

View Posterekose, on 2 Nov, 2009 - 05:11 PM, said:

Here's the current(est) code!

#include <stdio.h>
int main()
{
char *y;


OK, let me introduce you to the concept of INDENTING CODE. It's extremely important so let's do it
properly from the start. When you have a list of statements, align them to the left. When you have
an open brace, start the next line with 4 spaces and align the statements in that block of code to the
same indentation. If you have another open brace, indent by another 4 spaces. At a close brace,
indent by 4 spaces less. I'll do it for you this time, just so you can see.

By the way, indenting code isn't a beginner thing that you're expected to get past. I've been
programming since 1981 including professionally, and I always make sure I indent code.
#include <stdio.h>
int main()
{
	char *y;
	char *month_name();
	int i;
	printf("Enter month or -1 to quit\n");
	scanf("%d", &i);
	while (i != -1)
	{
		y=month_name(i);
		printf("%d %s \n", y, y);
		printf("Enter month or -1 to quit\n");
		scanf("%d", &i);
	}
	// by the way, main needs to return an int. Try "return 0;"
}

{
	static char *name[] = {
		"Illegal month",
		"Jan","Feb","Mar","Apr",
		"May","Jun","Jul","Aug",
		"Sep","Oct","Nov","Dec"};

	return (n < 1 || n > 12) ? name[0] : name[n];
}



See how much clearer that is? There's another benefit to this: by indenting properly, you just won't see
a lot of bugs that other people will struggle with, because it'll be obvious. If some code that you think is in
a loop is not at the same indentation level as the rest of the code in that loop, then something is wrong -
either the indentation, or the location of the code relative to the loop's open and closing braces.

Also a lot of programmer's editors will automatically handle indenting for you - if not guessing about changes
of indentation, then they'll at least guess that you want to stay at the same level.

Now let's go back a bit to the end of main:
		printf("%d %s \n", y, y);
		printf("Enter month or -1 to quit\n");
		scanf("%d", &i);
	}
}

{
	static char *name[] = {


OK, stop there. See the close brace on column 1, the empty line, and the open brace?
This is where one function stops and another function starts.

A function is a named block of code that starts with the datatype returned by the function, the
function name, and a list of parameters. You already have one of these, called main: it returns
int, its name is "main" and it has no parameters.

Within main you call a function:
	char *y;
	int i;
	// ...
		y=month_name(i);


So this means that somewhere in the code there should be a function named "month_name", that
returns a char*, and that takes one argument that is of type int. This will look like:
char *month_name(int n)
{
	//...
}


and we'll call the parameter n because that's the name used by the function code.

The block you have that starts by defining "static char *name[]" is the function, but it's
missing its header. See the open brace on column 1 before "static char *name[]"? On the line
before that is where you need to place "char *month_name(int n)" in order to complete the function.


Finally if you do all this and compile the code, you'll still get errors. This is because the compiler
needs to know about something before it can use it. (Whether that makes sense or not, you have to accept
that fact.) So the compiler needs to know about month_name() before you use it. There are two ways
of achieving that, firstly by rearranging the code:
char *month_name(int n)
{
	//...
}

int main()
{
	// ...
}


which is fine until you have two functions that call each other:
void a()
{
	b(); // error because compiler doesn't know b()
}
void b()
{
	a(); // works until you swap the functions around
}


then which goes first? The answer here is to PROTOTYPE the function, where you declare the first line
of the function, and instead of the code in braces you append a semicolon:
void b(); // Function PROTOTYPE.

void a()
{
	b(); // This now works because of the prototype
}
void b()
{
	a(); // Again this works until you swap the functions around
	// because a() hasn't been prototyped. You don't need to
	// prototype all functions.
}



So the other solution for your code is to PROTOTYPE the function "month_name()":
char *month_name(int n);

int main()
{
	// ...
		y=month_name(i); // this now works because the compiler knows what month_name is.
	// ...
}

char *month_name(int n)
{
	//...
}


Was This Post Helpful? 1
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,662
  • Joined: 23-August 08

Re: month_name problems

Posted 03 November 2009 - 06:13 AM

Nice explanation gronk!
Was This Post Helpful? 0
  • +
  • -

#15 erekose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 09

Re: month_name problems

Posted 03 November 2009 - 08:51 AM

All I can say is 2 things, Gronk:
1) Thank youthankyouthankyouthankyouthankyou!
2) I wish YOU were my C professor and not the cranky bitter old man I have teaching me now! :P This helped me immensely! I wasn't kidding whe I said it....I think I learned more from reading your (and the other guys here) posts than I've learned in 7 weeks of class with my prof! You guys rock!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1