It wont display my string

This is a discussion on It wont display my string within the C Programming forums, part of the General Programming Boards category; sorry about all the post but at least on my computer it is looking really weird, third times a charm ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    It wont display my string

    sorry about all the post but at least on my computer it is looking really weird, third times a charm


    after sending a string to my main struct it wont allow me to print what is in the struct outside of the function in which i assigned it.

    Code:
    int buildRooms(char filename[], struct Room list[])
    {
      char id[2];
      char roomname [20];
      char north[2];
      char east[2];
      char west[2];
      char south[2];
      char sentence [100];
      char space[4];
      FILE *f = fopen(filename, "r");
      if (f == NULL) {
        fprintf(stderr, "Error opening game file %s./n", filename);
        return 42;}              // failure                                                                   
      else{
          fscanf(f,"%s%s%s%s%s%s",id, roomname, north, east, west, south);
          fgets(space, sizeof(space),f);
          fgets(sentence, sizeof(sentence),f);
          struct Room list;
          strcpy(list.id, id);
      }
     fclose(f);
      return 0;                  // success                                                                   
    }
    void runGame(struct Room list[]){
      char input;
      printf("Welcome to the Haunted Mansion!\n");
      printf("Type c for current room, n/e/w/s for north/east/west/south.\n> ");
      printf("%s", Room->list->id);
    }
    why wont it print out what is in the struct, it just ends the program as if the command wasnt there

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Because id is a char, not a string.

    So use %c as the format.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Quote Originally Posted by Salem View Post
    Because id is a char, not a string.

    So use %c as the format.
    but it is a string, or so i think, i have it set as char id [2]

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Code:
    else{
          fscanf(f,"%s%s%s%s%s%s",id, roomname, north, east, west, south);
          fgets(space, sizeof(space),f);
          fgets(sentence, sizeof(sentence),f);
          struct Room list;
          strcpy(list.id, id);
      }
    The "list" in the scope of the else statement block takes precedence over the "list" in the parameter list. C has something called the rules of scope, which explain how scope works, and deals with name conflicts. A name conflict is what just happened to you.

    This page
    is one of the most uncomplicated, IMO, articles on the subject. Supplement this with C specific material that you find on the web.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Help using strcpy
    In here, your Room id is a single char.

    And to print it, it would be
    list[0].id
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 06:27 AM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 06:56 PM
  3. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  4. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

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