User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 12 Feb 2003, 12:28   #1
Mirai
Child Eating Zombie Clown
 
Join Date: Apr 2001
Posts: 1,450
Mirai is on a distinguished road
Another lab program that doesn't work.

Code:
#include <stdio.h>

void intro();
void calc(float);
void note();

main()
{
	/* Declare Variables */
	char letter[0];
	float grade;

	intro();
	printf("Enter the Numerical Grade of the Student:");
	scanf("%d", grade);
	calc(grade);
	note(letter);
}

void intro(void)
{
	printf("Name\n");
	printf("Class\n");
	printf("Section\n\n");
}

void calc(float grade)
{
	if (grade < 60) { letter = 'F'; }
	if (grade < 70 && grade > 60) { letter = 'D'; }
	if (grade < 80 && grade > 70) { letter = 'C'; }
	if (grade < 90 && grade > 80) { letter = 'B'; }
	if (grade <= 100 && grade > 90) { letter = 'A'; }
	return letter;
}
void note(char letter)
{
	switch (letter)
	{
		case 'A': printf("Excellent!\n");
			break;
		case 'B': printf("Very Good!\n");
			break;
		case 'C': printf("Average\n");
			break;
		case 'D': printf("You should probably study more..\n");
			break;
		case 'F': printf("Game Over.\n");
	}
return letter;
}
This is my first program that actually uses functions (other than main()), so I probably have done something wrong with the fuction declaration, and it also whines at me about some of my variables. I'm stuck at this point, because as many referece programs I've looked at, they all say I'm doing this correctly.

So if anyone (cyp) could tell me what I'm doing wrong, I'd be grateful.

the text of the lab assignment is here if you want to see exactly what I need to do.
__________________
Mirai - An Astral Being From Outer Space

Die You Bitch Minister of Insanity - "Timete Nostrum Piscem Furoris"

My fellow Americans, I'm pleased to tell you today that I've signed legislation that will outlaw Russia forever, we begin bombing in 5 minutes - President Ronald Reagan, in a radio check where he did not realize the microphone was on and the station broadcasting
Mirai is offline   Reply With Quote
Unread 12 Feb 2003, 12:43   #2
zenopus
Xenoc
 
Join Date: Feb 2001
Location: Great Britain
Posts: 297
zenopus is an unknown quantity at this point
Re: Another lab program that doesn't work.

Originally posted by Mirai
Code:
	char letter[0];
An array with no elements? - Just remove "[0]".

And you probably meant
Code:
	letter = calc(grade);
	note(letter);
Code:
void calc(float grade)
{
	if (grade < 60) { letter = 'F'; }
	if (grade < 70 && grade > 60) { letter = 'D'; }
	if (grade < 80 && grade > 70) { letter = 'C'; }
	if (grade < 90 && grade > 80) { letter = 'B'; }
	if (grade <= 100 && grade > 90) { letter = 'A'; }
	return letter;
}
"letter" is not in scope... define it locally or globally, or just return the char values with "return 'F';" etc.
cannot return value from void function... declare the function to return char instead of void.

Code:
void note(char letter)
{
   ...
  return letter;
}
cannot return value from void function... just do remove the return statement.
zenopus is offline   Reply With Quote
Unread 12 Feb 2003, 17:17   #3
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Re: Re: Another lab program that doesn't work.

Well, since this is your first proggy with functions, I assume you just started with programming, so I will keep this as simple as possible.

The first mistake you make is having a void function return a value.
When you write:
void bla() { /*stuff*/ }
means that the return value of that function is "void", thus actually nothing....

If you want your function to return something, you have to declare it as such. You want to return an integer:

int bla() { return 1; }

This function will return 1, thus:

int a;
a = bla();

"a" will have the value 1 after these two lines.

You can also return other objects, such as classes or floats or characters etc etc, but I won't overwhelm you with that

To fix your two functions that return values:
char calc(int grade) //you want to return a character
char note(char letter) //same here



Zenopus said that the variable is "not in scope", which means that the variable does not exist at that place:

void bla()
{
int a;
//here I can use "a"
}

//here I can't, because I closed the curly brackets

"a" is only available in the function "bla", because you declared it inside those curly brackets.
To fix your application you need to make the "letter" and "grade" variable "global", meaning that you put it outside any curly brackets. For example at the top of your file. This way it will be available to all functions in that file.




The array thingy:
char letter[0] will create an array in which you can put 0 characters. This is not what you want. You want to store one single character, thus you write:
char letter;
or if you want to assign an initial value to it:
char letter = 'a';





Another thing:
scanf("%d", grade);
will probably not work (I hope this works the same as fscanf(char*,...))
Why? The second parameter you give, is of the type float, while the parameter you give HAS TO BE of the type float* (a pointer to a float)
To fix this:
scanf("%d",&grade)

&grade is in this case a "reference to the variable grade" (& indicates a reference), or a pointer. Your teacher will prolly bug you with this in a later chapter.






One little note, why does note(char) have a return value? The input is the same as the return value. Ofcourse, this won't break anything, but it looks a tad unlogical to me.

Structural Integrity is offline   Reply With Quote
Unread 12 Feb 2003, 18:59   #4
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Re: Re: Re: Another lab program that doesn't work.

Quote:
Originally posted by Structural Integrity

Another thing:
scanf("%d", grade);
will probably not work (I hope this works the same as fscanf(char*,...))
Why? The second parameter you give, is of the type float, while the parameter you give HAS TO BE of the type float* (a pointer to a float)
Since it's %d, it should be int*. %f would scan into a float*.
queball is offline   Reply With Quote
Unread 12 Feb 2003, 19:07   #5
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Re: Re: Re: Re: Another lab program that doesn't work.

Quote:
Originally posted by queball
Since it's %d, it should be int*. %f would scan into a float*.
true.... my bad....
Structural Integrity is offline   Reply With Quote
Unread 12 Feb 2003, 21:45   #6
Cyp
∞+♪˛
 
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
Cyp is an unknown quantity at this point
Re: Another lab program that doesn't work.

Quote:
Originally posted by Mirai
...
the text of the lab assignment is here if you want to see exactly what I need to do.
This works (actually tested it), and fits the specification as far as I can tell.

Code:
#include <stdio.h>
/*This is the obligatory comment, http://www.cs.nmt.edu/~cs111/lab/lab2.txt said I have to add comment(s).*/
void a(void);char b(float);void c(char*,char*,char);
void main(){char z[10000],y[10000];float x;a();printf("If I asked you your first name, what would you answer?\n");scanf("%s",z);printf("If I didn't ask you your last name, what wouldn't you answer?\n");scanf("%s",y);printf("What grade do you want us to think you got?\n");scanf("%f",&x);c(y,z,b(x));}
void a(void){printf("Cyp 666\n\n");}
char b(float z){if(z>=80)if(z>=100)return'!';else if(z>=90)return'A';else return'B';else if(z>=60)if(z>=70)return'C';else return'D';else if(z>=0)return'F';return'G';}
void c(char*s,char*h,char z){int q=0;static char*n[]={"Imaginary numbers not supported in grades. Please use real grades, or upgrade to the registered version of this software","Congratulations on exceeding perfection","Perfect","Normal","Stupid","Pathetic","Good sense of humour","Better than teacher, teacher jealous"};switch(z){case'G':++q;case'F':++q;case'D':++q;case'C':++q;case'B':++q;case'A':++q;case'!':++q;}printf("%s, %s:\n%c: %s.",s,h,z,n[q]);}
Edit: PA forums strange... Remove the line break between "pri" and "ntf" on the last line, I didn't add the line break.
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics...
<!--Last incarnation: Nov 2000-->
Cyp is offline   Reply With Quote
Unread 12 Feb 2003, 22:02   #7
Cyp
∞+♪˛
 
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
Cyp is an unknown quantity at this point
From | Subject | Date | Size
Intro Comp Science | AutoResponse- Received Your Lab Pgm | Feb 13 | 1k



Hope I pass...



Grrr... Should have called it Cyp2.c, not Cyp666.c... Didn't realize what the lab number was...
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics...
<!--Last incarnation: Nov 2000-->
Cyp is offline   Reply With Quote
Unread 13 Feb 2003, 00:30   #8
Mirai
Child Eating Zombie Clown
 
Join Date: Apr 2001
Posts: 1,450
Mirai is on a distinguished road
thank you cyp. Now I have to go to the lab in order to find out what the TA thinks.
__________________
Mirai - An Astral Being From Outer Space

Die You Bitch Minister of Insanity - "Timete Nostrum Piscem Furoris"

My fellow Americans, I'm pleased to tell you today that I've signed legislation that will outlaw Russia forever, we begin bombing in 5 minutes - President Ronald Reagan, in a radio check where he did not realize the microphone was on and the station broadcasting
Mirai is offline   Reply With Quote
Unread 13 Feb 2003, 01:24   #9
Cyp
∞+♪˛
 
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
Cyp is an unknown quantity at this point
Quote:
Originally posted by Mirai
thank you cyp. Now I have to go to the lab in order to find out what the TA thinks.
Sent it with no explanation, didn't mention any names, by the way.
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics...
<!--Last incarnation: Nov 2000-->
Cyp is offline   Reply With Quote
Unread 13 Feb 2003, 09:26   #10
Mirai
Child Eating Zombie Clown
 
Join Date: Apr 2001
Posts: 1,450
Mirai is on a distinguished road
Nifty.

I may not get the chance to, however..

Seems the prof learned that I was in the class without having satasfied the math corequisite. (Math 131. I suck at math)

He seems pretty hardline on me not being in the class, as if to say "No, I won't sign your prerequsite waiver form".

Only problem is this lowers me to 7 credit hours. I still need 5 more to satasfy my Heath Plan requirements. Otherwise I have to pay 60 grand if my heart decides to fail. Which I'd rather not do.

[edit]

oh by the way cyp:

cyp.c:1: parse error before `<'
cyp.c: In function `main':
cyp.c:5: warning: return type of `main' is not `int'
cyp.c: At top level:
cyp.c:10: warning: type mismatch with previous implicit declaration
cyp.c:5: warning: previous implicit declaration of `a'
cyp.c:10: warning: `a' was previously implicitly declared to return `int'
__________________
Mirai - An Astral Being From Outer Space

Die You Bitch Minister of Insanity - "Timete Nostrum Piscem Furoris"

My fellow Americans, I'm pleased to tell you today that I've signed legislation that will outlaw Russia forever, we begin bombing in 5 minutes - President Ronald Reagan, in a radio check where he did not realize the microphone was on and the station broadcasting

Last edited by Mirai; 13 Feb 2003 at 09:34.
Mirai is offline   Reply With Quote
Unread 13 Feb 2003, 20:13   #11
Cyp
∞+♪˛
 
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
Cyp is an unknown quantity at this point
Quote:
Originally posted by Mirai
Nifty.

I may not get the chance to, however..

Seems the prof learned that I was in the class without having satasfied the math corequisite. (Math 131. I suck at math)

He seems pretty hardline on me not being in the class, as if to say "No, I won't sign your prerequsite waiver form".

Only problem is this lowers me to 7 credit hours. I still need 5 more to satasfy my Heath Plan requirements. Otherwise I have to pay 60 grand if my heart decides to fail. Which I'd rather not do.
I didn't mean to cause any problems. Sorry if I have, can't tell from the above whether you mean that I did, and if so, how much.

Didn't think it could cause any trouble, as they wouldn't know anyone to blaim, anyway.

As far as I know, the teachers here don't decide who goes to which classes, and certainly don't complain about (specific) people not coming to class. (They tend to notice, if only 2 or 3 out of 20 or 30 people arrive. Can happen.)

Quote:

[edit]

oh by the way cyp:

cyp.c:1: parse error before `<'
cyp.c: In function `main':
cyp.c:5: warning: return type of `main' is not `int'
cyp.c: At top level:
cyp.c:10: warning: type mismatch with previous implicit declaration
cyp.c:5: warning: previous implicit declaration of `a'
cyp.c:10: warning: `a' was previously implicitly declared to return `int'
Compiled in MSVC... Maybe I should look at GCC some time...
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics...
<!--Last incarnation: Nov 2000-->
Cyp is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 05:04.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018