Like Tree1Likes

Confussion with Username

This is a discussion on Confussion with Username within the C Programming forums, part of the General Programming Boards category; How do you pass a parameter between different functions? I.e, how can I pass it from my main to the ...

  1. #31
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    How do you pass a parameter between different functions? I.e, how can I pass it from my main to the SetupRoom function?

  2. #32
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    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){

  3. #33
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    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!

  4. #34
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't add it, you do when you hit enter. It simply leaves it there.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #35
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    Quote Originally Posted by quzah View Post
    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?

  6. #36
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    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 []).

  7. #37
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    Quote Originally Posted by smokeyangel View Post
    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?

    Something like this?:

    Code:
    //remove specified chunks from a string 
    void remcnks(char *str, char *cnk) 
    { 
    char *pos; 
    int clen = strlen(cnk); 
    while(pos = strstr(str, cnk)) 
    memmove(pos, pos + clen, strlen(pos) - clen + 1); 
    }
    P.S - I owe you a pint after this!

  8. #38
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    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;
    }

  9. #39
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    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 .

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. username to uid
    By chrismiceli in forum Linux Programming
    Replies: 1
    Last Post: 07-30-2007, 01:23 PM
  2. Birthyear in username? Why?
    By brewbuck in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-28-2007, 06:48 PM
  3. username collection
    By algi in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2005, 01:20 PM
  4. Username function
    By jamie85 in forum C Programming
    Replies: 6
    Last Post: 03-19-2004, 06:33 PM
  5. getting username from Windows NT
    By Flucas in forum Windows Programming
    Replies: 1
    Last Post: 10-29-2001, 06:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21