How do you pass a parameter between different functions? I.e, how can I pass it from my main to the SetupRoom function?

04-28-2012

smokeyangel

The same way as you pass it to getuser.

Assuming you have corrected both the declaration and the prototype of SetupRoom to accept a char* parameter, it should just be:

Code:

//Main function
void main(){
printf("A few few years ago you had found a book. Nothing special, but it kept his attention for a week or so. These days it was hard to find time to read, but you considered this the only way to cling onto civilisation. The book, in hindsight, was dull. You had no real interest interest in religion and waded through the pages with a sense of irony.");
printf("\n\nYour memory of the past few days was somewhat confused. After so long it became hard to keep track of the passing of time. Events became blurred and actions became instinct. You stood at the top of a road, hands clutching the straps of your backpack, feet echoing that recognisable ache.\n\n\n\n");
char user[SIZE];
get_username( user, SIZE );
unsigned int RoomNumber = 0;
unsigned int Direction;
SetupRooms(user); // here
while(1){

04-28-2012

ShotgunChimp

You're an apsolute star! I got it working with the name. The only minor anoyance is that fgets seams to create a line space. Almost as if it is adding \n at the end of the username. However, if this cannot be solved that wont be too bad.

I have to say, if we only had lecturers that were willing to help and guide us through this programming, like you have, then we might actually be able to improve at this. Thank you for your time and patients!

04-28-2012

quzah

It doesn't add it, you do when you hit enter. It simply leaves it there.

Quzah.

04-28-2012

ShotgunChimp

Quote:

Originally Posted by quzah

It doesn't add it, you do when you hit enter. It simply leaves it there.

That makes more sense. Is there any way to get around this?

04-28-2012

smokeyangel

Great!! You're welcome :) There's really no substitute for practice with learning to program -- if it's any consolation my lecturers were no good either. They knew it all, but I guess they didn't have the inclination or time to explain things. On the plus side the more you can figure out for yourself the better -- and apart from a special few I think everyone feels a bit lost to begin with. It's all good, just keep coding!

About the newline: When you type a name and hit enter the newline character gets read in by fgets along with the rest of the string. So the end of the string will have a '\n' followed by 0, the terminator. You can get the length of the string with strlen, and just replace the '\n' with a 0 to chop it off the end (you can access individual elements in a string like any other array, with []).

04-28-2012

ShotgunChimp

Quote:

Originally Posted by smokeyangel

About the newline: When you type a name and hit enter the newline character gets read in by fgets along with the rest of the string. So the end of the string will have a '\n' followed by 0, the terminator. You can get the length of the string with strlen, and just replace the '\n' with a 0 to chop it off the end (you can access individual elements in a string like any other array, with []).

I have been trying to research this. We have very littler material given to us to actually find out what new things are and the internet has a habit of confusing the matter. I understand the principle. You use strlen() to find out the string length of the username and then remove the last characters/as a 0 to stop the enter.
How is this done in practice?

Wow, ok. I wasn't thinking of anything so general! That's pretty cool. I think that'll work fine to remove newline or more. I tested it with some random strings and it seemed to work well. So I guess you don't owe me a beer!
You should check that neither pointer is NULL of course. Maybe consider having a "char* dest" parameter (which can be str) to store the result in -- might be useful to be able to remove things from strings without modifying the original (though you probably don't care about that for newlines).

Pretty cool. I was thinking much more dumb: a specific "throw away pesky newline at end of string" function, like perl's "chomp".

Code:

void chomp(char * s)
{
if (s == NULL)
return;

if (s[strlen(s)-1] == '\n')
s[strlen(s)-1] = 0;
}

04-28-2012

ShotgunChimp

It's almost all there. Minus some tweeks and annotations.

I cannot thank you enough. There is a sense of satisfaction having spent MANY hours glued to a screen and then finaly getting it done. I will try my best not to flood the forum with amature questions again in the future :biggrin:.