Noob question

This is a discussion on Noob question within the C Programming forums, part of the General Programming Boards category; Howdy, I have read about 4 begginer c book and I am now on Advanced c, by herbert schildt, but ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    14

    Noob question

    Howdy,

    I have read about 4 begginer c book and I am now on Advanced c, by herbert schildt,
    but you know how it goes, yes, I have been given the knowledge of how to do it but that doesnt mean i can, lol, My long term goal is to be able to code my own mud, but for the next few years I just want to do smaller stuff, till i get good enough to move own, at the moment I am writing a building program, I know there are alot of there, but I am just trying to do smaller projects as i said earlier,.

    What I would like to ask of yall pros, is, I have came to a point in the code:
    Code:
    main(void)
    {
    
    	WF roomdescp;
    	char answer;
    
    	roomdescp = DataReceive(roomdescp);
    	printf("Here is what you entered:\n");
    	printf("%d\n", roomdescp.v_num);
    	printf("%s\n", roomdescp.rname);
    	printf("%s\n", roomdescp.rdescp);
    	printf("%c\n", roomdescp.sqwigly);
    	printf("%d %d %d\n",roomdescp.zone_num, roomdescp.room_biv, roomdescp.sect_type);
    	printf("Is this correct Y or N?\n");
    	   answer = getchar();
    	   switch(answer){
    		case 'Y':
    		case 'N':
    		   printf("Please re-enter 
    		default:
    		   ;
    when I use a switch for the answer, under 'N', I want to expand it so that if the entered say the v_num wrong, they can go back in and just change that, so they dont have to repeat, I dont want yall to do it, just tell me what is the best Keyword to do it, I was thinking a switch inside case 'N', if it is possible to nest one in another

    Thank you very much for your time,
    P.S, I am still having trouble applying what I have learned, could anyone suggest another book to help with that, or someother kind of learning source?

    Thanks agin for all your time!

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    14
    Here is the full code, it is nowhere even remotly close to done, but here it is:

    Code:
    /********************************************************************
    *  By: 
    *  Virtual room builder
    *  Version 1.0     12/11/08
    ********************************************************************/
    #include <stdio.h>
    
    struct wld_file {
    	int v_num;  /* Virtual Number*/
    	char rname; /* Room Name */
    	char rdescp; /* Room Description */
    	char sqwigly; /* ~ */
    	int zone_num, room_biv,  sect_type; /* <Zone Number> <Room Bivector> <Sector Type> */
    };
    
    
    /* create synonym */
    typedef struct wld_file WF;
    
    
    /* function decleration */
    WF DataReceive(WF w);
    
    
    main(void)
    {
    
    	WF roomdescp;
    	char answer;
    
    	roomdescp = DataReceive(roomdescp);
    	printf("Here is what you entered:\n");
    	printf("%d\n", roomdescp.v_num);
    	printf("%s\n", roomdescp.rname);
    	printf("%s\n", roomdescp.rdescp);
    	printf("%c\n", roomdescp.sqwigly);
    	printf("%d %d %d\n",roomdescp.zone_num, roomdescp.room_biv, roomdescp.sect_type);
    	printf("Is this correct Y or N?\n");
    	   answer = getchar();
    	   switch(answer){
    		case 'Y':
    		case 'N':
    		   printf("Please re-enter 
    		default:
    		   ;
    
    	return 0;
    }
    
    /* function defintion */
    WF DataReveive(WF w)
    {
    	printf("Enter Virual Number:\n");
    	   scanf("%d"\n, &s.v_num);
    	printf("Enter Room Name(YOU MUST ADD ~ TO THE END):\n");
    	   gets(s.rname);
    	printf("Enter Room Description:\n");
    	   gets(s.rdescp);
    	printf("Enter ~ (Nothing Else)\n");
    	   gets(s.sqwigly);
    	printf("Enter Zone Number: Room Bivector: Sector Type: (A space between each one)\n");
    	   scanf("%d %d %d\n", &s.zone_num, &s.room_biv, &s.sect_type);
    	return s;
    }

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Just use a loop... ie

    Code:
    int in = 0;
    
    do
    {
       scanf("&#37;d", &in);
    } while(in != <what I expected>);
    [edit]
    * Don't use gets() -- http://cpwiki.sf.net/gets
    * Don't use implicit main -- see the FAQ

    struct wld_file {
    int v_num; /* Virtual Number*/
    char rname; /* Room Name */
    char rdescp; /* Room Description */
    char sqwigly; /* ~ */
    int zone_num, room_biv, sect_type; /* <Zone Number> <Room Bivector> <Sector Type> */
    };

    ...

    gets(s.rname);
    BTW you're trying to read strings into a single character -- doesn't it crash?
    [/edit]
    Last edited by zacs7; 12-11-2008 at 11:24 PM.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Code:
    main(void)
    Typical Herb. I don't think he ever caught up with modern standards.

    My first C book was "Teach Yourself C" by Herbie. Still have it - it's at arm's length as I type.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    What Dino is so eloquently saying is that void main() is a bad idea.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  6. #6
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    I think what he says is a book with othur named Herbie rote a book by the name of "Teach Yourself C"

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    14
    getting a error, and I am soo lost with it. If anyone could have a quick look, I would be very thankful.


    Code:
    /tmp/ccSlHuD2.o: In function `main':
    build1.c:(.text+0x23): undefined reference to `DataReceive'
    collect2: ld returned 1 exit status
    Code:
    /********************************************************************
    *  By: 
    *  Virtual room builder
    *  Version 1.0     12/11/08
    ********************************************************************/
    #include <stdio.h>
    
    struct wld_file {
    	int v_num;  /* Virtual Number*/
    	char sqwigly; /* ~ */
    	int zone_num, room_biv,  sect_type; /* <Zone Number> <Room Bivector> <Sector Type> */
    	char roomname[45]; /* Room Name */
    	char rdescp[]; /* Room Description */
    };
    
    typedef struct wld_file WF;
    
    void DataReceive(WF *ptr_wld);
    
    
    main()
    {
    
    	WF rmdescp;
    
    	DataReceive(&rmdescp);
    	printf("Here is what you entered:\n");
    	printf("%d\n", rmdescp.v_num);
    	printf("%s\n", rmdescp.roomname);
    	printf("%s\n", rmdescp.rdescp);
    	printf("%c\n", rmdescp.sqwigly);
    	printf("%d %d %d\n",rmdescp.zone_num, rmdescp.room_biv, rmdescp.sect_type);
    
    	return 0;
    }
    
    /* function defintion */
    WF DataReveive(WF *ptr_wld)
    {
    	printf("Enter Virual Number:\n");
    	   scanf("%d\n", &ptr_wld->v_num);
    	printf("Enter Room Name(YOU MUST ADD ~ TO THE END):\n");
    	   gets(ptr_wld->roomname);
    	printf("Enter Room Description:\n");
    	   gets(ptr_wld->rdescp);
    	printf("Enter ~ (Nothing Else)\n");
    	   gets(ptr_wld->sqwigly);
    	printf("Enter Zone Number: Room Bivector: Sector Type: (A space between each one)\n");
    	   scanf("%d %d %d\n", &ptr_wld->zone_num, &ptr_wld->room_biv, &ptr_wld->sect_type);
    }

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    14
    nm I am a idoit, sorry yall, i guess i need to really look at my spelling :O(
    Last edited by clb2003; 12-12-2008 at 11:33 PM.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    14
    Program hangs on zone number: room biv: sector type:

    Does it look wrong or is it something like spelling

    Thank you all agin for your help and sorry for bothering yall.
    Code:
    /********************************************************************
    *  By: 
    *  Virtual room builder
    *  Version 1.0     12/11/08
    ********************************************************************/
    #include <stdio.h>
    
    struct wld_file {
    	int v_num;  /* Virtual Number*/
    	char sqwigly; /* ~ */
    	int zone_num, room_biv,  sect_type; /* <Zone Number> <Room Bivector> <Sector Type> */
    	char roomname[45]; /* Room Name */
    	char rdescp[]; /* Room Description */
    };
    
    typedef struct wld_file WF;
    
    void DataReceive(WF *ptr_wld);
    
    
    main()
    {
    
    	WF rmdescp;
    
    	DataReceive(&rmdescp);
    	printf("Here is what you entered:\n");
    	printf("%d\n", rmdescp.v_num);
    	printf("%s\n", rmdescp.roomname);
    	printf("%s\n", rmdescp.rdescp);
    	printf("%c\n", rmdescp.sqwigly);
    	printf("%d %d %d\n", rmdescp.zone_num, rmdescp.room_biv, rmdescp.sect_type);
    
    	return 0;
    }
    
    /* function defintion */
    void DataReceive(WF *ptr_wld)
    {
    	printf("Enter Room Description:\n");
    	   gets(ptr_wld->rdescp);
    	printf("Enter Room Name(YOU MUST ADD ~ TO THE END):\n");
    	   gets(ptr_wld->roomname);
    	printf("Enter Zone Number: Room Bivector: Sector Type: (A space between each one)\n");
    	   scanf("%d %d %d\n", &ptr_wld->zone_num, &ptr_wld->room_biv, &ptr_wld->sect_type);
    	printf("Enter ~ (Nothing Else)\n");
    	   gets(ptr_wld->sqwigly);
    	printf("Enter Virtual Number:\n");
    	   scanf("%d\n", &ptr_wld->v_num);
    }

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    scanf("&#37;d %d %d\n", &ptr_wld->zone_num, &ptr_wld->room_biv, &ptr_wld->sect_type);
    Not sure, but shouldn't you use the (&) address of operator like this:
    Code:
       scanf("%d %d %d\n", ptr_wld->&zone_num, ptr_wld->&room_biv, ptr_wld->&sect_type);
    I'm new to C, so I'm not 100% on this, but I'm pretty sure the way you have it you are telling it to get the address of an address. ptr_wld comes into the function as a pointer, so that means that all your function sees is an address where the ptr_wld data can be found. I don't think you can use the address operator on something that's already an address...

    -Dave

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > char rdescp[]; /* Room Description */
    So where is the space allocated?

    Oh, and read the FAQ on using gets(), ie, DON'T
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Quote Originally Posted by mutandis View Post
    Not sure, but shouldn't you use the (&) address of operator like this:
    Code:
       scanf("&#37;d %d %d\n", ptr_wld->&zone_num, ptr_wld->&room_biv, ptr_wld->&sect_type);
    I'm new to C, so I'm not 100% on this, but I'm pretty sure the way you have it you are telling it to get the address of an address. ptr_wld comes into the function as a pointer, so that means that all your function sees is an address where the ptr_wld data can be found. I don't think you can use the address operator on something that's already an address...

    -Dave
    No, the original code is correct. Yours is a syntax error.

    And as for YOU, clb2003:
    You have YET to correct what people have told you to do. Now they've even pointed something out TWICE. So CORRECT it before you do anything else. Do NOT ignore what people write.
    If you don't understand or can't find the information, then MENTION so.

    Also note zacs7 in post #3 about reading a string into a single char.
    Last edited by Elysia; 12-13-2008 at 02:44 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Quote Originally Posted by Elysia View Post
    No, the original code is correct. Yours is a syntax error.
    Sorry. I'm confusing instead of helping...

    Quote Originally Posted by Elysia View Post
    Also note zacs7 in post #3 about reading a string into a single char.
    Aww cripes!

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    In the future, if in doubt, you can always try the code out with your compiler, before replying.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I'd suggest you also bump up the warning levels with your compiler, to maximum perhaps.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 12:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 07:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 08:28 PM

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