Thread: Here's a problem, any one helps me?

  1. #1
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    Arrow Here's a problem, any one helps me?

    My programme is about a ticket system, I can compile and run with help of a nice person, but it is strange that when I have the first choice and the programme will print the message and have no response at all:

    "Please merge the segments toger eg. type Bei Jing as Beijing"

    The programme contains 4 files:
    fatal.h fatal erros
    tree.h tree header file
    tree.c tree implementation file
    project.c main file
    Last edited by NightWalker; 06-09-2003 at 09:46 PM.

  2. #2
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    Files1

    fata.h

  3. #3
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    files2

    tree.h

  4. #4
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    Files3

    tree.c

  5. #5
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    Files4

    project.c

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Can you rephrase what it is you want us to do?

  7. #7
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    Yes, help me to figure out the problem:
    why the programme will only print a message and have no response?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is amazingly rude, and isn't the first time I've seen you do this. You cannot expect people to just grab your whole project, compile it, debug it, and tell you what's wrong.

    Do some trouble shooting. C is fairly linear. It's a simple process. If you don't know how to use a debugger, then just add printf lines:

    printf("1 -- got this far\n"); fflush( stdout );
    ... some code line ...
    printf("2 -- got this far\n"); fflush( stdout );

    Also, you don't need to make a ton of posts to include multiple files or links.

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

  9. #9
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    This is amazingly rude, and isn't the first time I've seen you do this. You cannot expect people to just grab your whole project, compile it, debug it, and tell you what's wrong.
    I am very sorry about that. I have tried your method but it is so strange that why the printf statement not works, although it is on the top of a do-while loop?
    Code:
    do
       {
         printf("Please enter the city you want to set off:");
         gets(cityf, sizeof(cityf), stdin);
         status = check(cityf, cityfc);
       } while (status != 1);
    Code:
    check(char cityi[], char tmp[])
    {
      int i = 0;
      char city[21][10] = {.................A lot of things here};
      char citycd[21][10] = {................A lot of things here};
      while (i < 21)
        {
          if (strcmp(cityi, city[i]) == 1)
    	{
    	  strcpy(tmp, citycd[i]);	
    	  return 1;
    	}
        }
      return 0;
    }
    Last edited by NightWalker; 06-09-2003 at 10:52 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by NightWalker
    I am very sorry about that. I have tried your method but it is so strange that why the printf statement not works, although it is on the top of a do-while loop?
    Code:
    do
       {
         printf("Please enter the city you want to set off:");
         gets(cityf, sizeof(cityf), stdin);
         status = check(cityf, cityfc);
       } while (status != 1);
    Here are a few things:
    1) Try flushing the output stream:

    printf("Please enter the city you want to set off:");
    fflush( stdout );

    2) gets doesn't take that many arguments. Perhaps you were thinking of fgets?

    3) Check the return value of gets or fgets to see what's happening.
    Code:
    if( fgets( ... ) == NULL )
    {
        printf("Oops!");
        fflush( stdout );
    }
    The reason you want to flush the output stream after your printf statements here is to make sure you are actually seeing the error. Your printf statement in your loop is probably happening, but the buffer is retaining the information rather than displaying it.

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

  11. #11
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    sizeof(cityf)
    Wouldn't this be sizeof(char *)? Were you intending to find the length of the string?

  12. #12
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    Unhappy

    what's the meaning of the fflush statement??
    I have add the fflush statement to the programme, it will print the message now, but the fgets still not works, no response after the message were printed, I cannot input anything:
    Please enter the city you want to set off:
    Last edited by NightWalker; 06-10-2003 at 12:05 AM.

  13. #13
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    I have chaged these problems already, yep, I was refused by the return value of the strcmp(), I chaged and compile it, but when the programme print:
    "Please enter the......"
    There is no response at all.
    BTW, What is a debugger? From where I can get it?
    This is the new project.c I have. Thanks

    And I use WINXP/gcc complier
    The virtual environment of UNIX, the software cygwin
    Last edited by NightWalker; 06-10-2003 at 01:51 AM.

  14. #14
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    fortunately, I can run most part of my programme now, but still a little bit problem, I meet it frequently. Segmentation fault <core dumped> it seems like some problem like this, but I cannot find it easily, it must be happened in this two functions.
    Code:
    scanf("%d", num);
    Code:
    create(T1, i);
    getserial(cityfc, citytc, sn, i);
    
    void
    create(SearchTree T, int num)
    {
      Position P;
      char na[15], in[20], nn[15];
      printf("Please enter you name:");
      gets(na);
      printf("Please enter your personal ID number:");
      gets(in);
      printf("Please enter your nationality:");
      gets(nn);                             <=========Since here no problem
      Insert(num, T);
      P = Find(num, T);
      strcpy(P->Information.name, na);
      strcpy(P->Information.id, in);
      strcpy(P->Information.nal, nn);
    }
    
    void
    getserial(const char cityfc[], const char citytc[], char sn[], int i)
    {
      int array1[2], array2[2], counter;
      for (counter = 0; counter < 3; counter++)
        {
          array1[counter] = (int) cityfc[counter];
          array2[counter] = (int) citytc[counter];
        }
      sprintf(sn, "%d%d%d%d%d%d%d", array1[0], array1[1], array1[2], array2[0], array2[1], array2[2], i); 
    }
    After that statement, segmentation fault <core dumped>
    Last edited by NightWalker; 06-10-2003 at 05:01 AM.

  15. #15
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    But I think the problem as follows:

    Originally posted by NightWalker
    My programme always have the segmentation fault. Why? could you help me?

    stucture of the tree in the header file:
    Code:
    typedef int ElementType;
    typedef struct {
      char name[15];
      char id[20];
      char nal[15];
    } Info;
      
      
      /* START: fig4_16.txt */
    #ifndef _Tree_H
    #define _Tree_H
    
    
      typedef struct TreeNode *Position;
      typedef struct TreeNode *SearchTree;
    
    struct TreeNode
    {
      ElementType Element;
      Info Information;
      SearchTree  Left;
      SearchTree  Right;
    };
    the related part in the main programme:
    Code:
    void      <======I know the problem happened in this function
    create(SearchTree T, int num)
    {
      Position P;
      Insert(num, T);     <=======Perhaps here
      P = Find(num, T);    <=======Or here
      printf("Please enter you name:");
      gets(P->Information.name);
      printf("Please enter your personal ID number:");
      gets(P->Information.id);
      printf("Please enter your nationality:");
      gets(P->Information.nal);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM