Thread: i need help of my seatselling function.i dont know where am i do wrong!

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    8

    Unhappy i need help of my seatselling function.i dont know where am i do wrong!

    Code:
    #include<stdio.h>
    void DisplayFunc(int r,int w,int SeatF[7][10]){
         int i,j;
         printf("\n-----------------------------SCREEN------------------------------\n\n\n");
         for(i=0;i<r;i++){
           printf("%c",65+i);
            for(j=0;j<w;j++){
              if(SeatF[i][j]==0){             
                 printf("   |0%d|",j);                               
                 }
            else
              printf("   |**|");                                  
            }
            printf("\n");
           }
    }
    
    
    int seatselling(){
        int i,j,count=0,seat;
        char s1[3],s2[3],SeatF[7][10],flag[10][10],n;
        
        printf("input your seat range 1:");
        scanf("%s",&s1);
        printf("input your seat range 2:");
        scanf("%s",&s2);
        
        
        for (i=s1[1];i<=s2[1];i++)
        SeatF[s1[0]-65][i-49]==1;        
        DisplayFunc(7,10,SeatF);
        
        
      }
    int main(){ 
     int i,j,SeatF[7][10]; 
           for(i=0;i<7;i++){
                 for(j=0;j<10;j++){
                      SeatF[i][j]=0;   
                                   }
                            }         
            DisplayFunc(7,10,SeatF); 
            seatselling();
           system("PAUSE");
    }

    how can i get the right position to my seat?
    Last edited by danielchs; 09-07-2019 at 05:43 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    1. Learn how to indent code.
    Code:
    #include<stdio.h>
    void DisplayFunc(int r, int w, int SeatF[7][10])
    {
      int i, j;
      printf("\n-----------------------------SCREEN------------------------------\n\n\n");
      for (i = 0; i < r; i++) {
        printf("%c", 65 + i);
        for (j = 0; j < w; j++) {
          if (SeatF[i][j] == 0) {
            printf("   |0%d|", j);
          } else
            printf("   |**|");
        }
        printf("\n");
      }
    }
    
    int seatselling()
    {
      int i, j, count = 0, seat;
      char s1[3], s2[3], SeatF[7][10], flag[10][10], n;
    
      printf("input your seat range 1:");
      scanf("%s", &s1);
      printf("input your seat range 2:");
      scanf("%s", &s2);
    
      for (i = s1[1]; i <= s2[1]; i++)
        SeatF[s1[0] - 65][i - 49] == 1;
    
      DisplayFunc(7, 10, SeatF);
    }
    
    int main()
    {
      int i, j, SeatF[7][10];
      for (i = 0; i < 7; i++) {
        for (j = 0; j < 10; j++) {
          SeatF[i][j] = 0;
        }
      }
      DisplayFunc(7, 10, SeatF);
      seatselling();
      system("PAUSE");
    }
    2. Get a decent compiler and turn up the warning level.
    Code:
    $ gcc -Wall -Wextra foo.c
    foo.c: In function ‘seatselling’:
    foo.c:24:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[3]’ [-Wformat=]
       scanf("%s", &s1);
             ^
    foo.c:26:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[3]’ [-Wformat=]
       scanf("%s", &s2);
             ^
    foo.c:29:5: warning: statement with no effect [-Wunused-value]
         SeatF[s1[0] - 65][i - 49] == 1;
         ^
    foo.c:31:22: warning: passing argument 3 of ‘DisplayFunc’ from incompatible pointer type [-Wincompatible-pointer-types]
       DisplayFunc(7, 10, SeatF);
                          ^
    foo.c:2:6: note: expected ‘int (*)[10]’ but argument is of type ‘char (*)[10]’
     void DisplayFunc(int r, int w, int SeatF[7][10])
          ^
    foo.c:21:50: warning: unused variable ‘n’ [-Wunused-variable]
       char s1[3], s2[3], SeatF[7][10], flag[10][10], n;
                                                      ^
    foo.c:21:36: warning: unused variable ‘flag’ [-Wunused-variable]
       char s1[3], s2[3], SeatF[7][10], flag[10][10], n;
                                        ^
    foo.c:20:24: warning: unused variable ‘seat’ [-Wunused-variable]
       int i, j, count = 0, seat;
                            ^
    foo.c:20:13: warning: unused variable ‘count’ [-Wunused-variable]
       int i, j, count = 0, seat;
                 ^
    foo.c:20:10: warning: unused variable ‘j’ [-Wunused-variable]
       int i, j, count = 0, seat;
              ^
    foo.c: In function ‘main’:
    foo.c:44:3: warning: implicit declaration of function ‘system’ [-Wimplicit-function-declaration]
       system("PAUSE");
       ^
    foo.c: In function ‘seatselling’:
    foo.c:32:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    In particular, you need to pay attention to the type of SeatF you have all over the place, and "warning: statement with no effect".
    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.

  3. #3
    Registered User
    Join Date
    Sep 2019
    Posts
    8
    thanks for reply,but i wonder where am i actually wrong in these codes.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    Did you write this code, or is it a "fix the errors homework"

    > char s1[3], s2[3], SeatF[7][10], flag[10][10], n;
    vs
    > int i, j, SeatF[7][10];
    What types does SeatF have?
    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.

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    147

    Post

    Code:
    if(SeatF[i][j]==0){                
    printf("   |0%d|",j);                               
       }
       else
       printf("   |**|");                                  
      }
    You are missing a bracket in this statement.
    It should look something like this...

    Code:
    if(SeatF[i][j]==0){                
      printf("   |0%d|",j);     
    } else {
      printf("   |**|");
    };
    I would also escape the pipes but i don't think that necessary.
    You should go through and make sure all your statements/functions are bracketed properly.
    Definitely learn how to indent code it makes a huge difference, but on the other hand when you post code sometimes it just goes wonky.
    Last edited by Structure; 09-07-2019 at 04:52 PM.


  6. #6
    Registered User
    Join Date
    Sep 2019
    Posts
    8
    that is my homework , i need to get a right positions to my seats.My teacher said that seatselling() has no parameter… i cannot update SeatF in main function. SeatF in main is different from SeatF in seatselling.
    what that means?
    here is my update code
    Code:
    
    
    Code:
    #include<stdio.h>
    void DisplayFunc(int r,int w,int SeatF[7][10]){
         int i,j;
         printf("\n-----------------------------SCREEN------------------------------\n\n\n");
         for(i=0;i<r;i++){
           printf("%c",65+i);
            for(j=0;j<w;j++){
              if(SeatF[i][j]==0){             
                 printf("   |0%d|",j);                               
                 }
            else
              printf("   |**|");                                  
            }
            printf("\n");
           }
    }
    
    
    int seatselling(){
    	int i,j,count=0,SeatF[7][10];
        char s1[3],s2[3];
        printf("input your seat range 1:");
        scanf("%s",&s1);
    	printf("input your seat range 2:");
        scanf("%s",&s2);
        for (i=s1[1];i<=s2[1];i++){
          count++;
          SeatF[s1[0]-65][i]==1;
        }
        DisplayFunc(7,10,SeatF);
        printf("Fee is $%d",count*80);
    }
    int main(){ 
     int i,j,SeatF[7][10]; 
           for(i=0;i<7;i++){
                 for(j=0;j<10;j++){
                      SeatF[i][j]=0;   
                                   }
                            }         
           SeatF[1][1]=1;
           SeatF[5][3]=1;  
            DisplayFunc(7,10,SeatF); 
            seatselling();
           system("PAUSE");
    }
    please help me!!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,065
    What exactly is seatselling supposed to do if it doesn't take any parameters? It looks like all the main function should do is call seatselling and return 0, then all the work starts from seatselling. If you have a SeatF local to main and another local to seatselling, and seatselling takes no parameters, then unless you do some kind of hack with global variables that you should not do, you will never be able to update the SeatF in main.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2019
    Posts
    8
    seatselling() is use to ask user input two seats and mark the SeatF[][] into 1 ,then call the displayfunc() to update the seat table.
    how can i add some parameters to make it works?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    > My teacher said that seatselling() has no parameter… i cannot update SeatF in main function. SeatF in main is different from SeatF in seatselling.
    The only way out of that mess is to make SeatF a global variable.

    But IMO, your teacher is feeding you bad advice.

    Oh, and you need to change
    SeatF[s1[0]-65][i]==1;
    to
    SeatF[s1[0]-65][i]=1;
    or better
    SeatF[s1[0]-'A'][i]=1;

    On the whole, it should be
    Code:
    void DisplayFunc(int r,int w,int SeatF[7][10]){
         int i,j;
         printf("\n-----------------------------SCREEN------------------------------\n\n\n");
         for(i=0;i<r;i++){
           printf("%c",65+i);
            for(j=0;j<w;j++){
              if(SeatF[i][j]==0){             
                 printf("   |0%d|",j);                               
                 }
            else
              printf("   |**|");                                  
            }
            printf("\n");
           }
    }
     
     
    int seatselling(int r,int w,int SeatF[7][10]){
       /// 
       DisplayFunc(r,w,SeatF);
    }
    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.

  10. #10
    Registered User
    Join Date
    Sep 2019
    Posts
    8
    Like this?
    Code:
    #include<stdio.h>
    
    
    void DisplayFunc(int r,int w,int SeatF[7][10]){
         int i,j;
         printf("\n-----------------------------SCREEN------------------------------\n\n\n");
         for(i=0;i<r;i++){
           printf("%c",65+i);
            for(j=0;j<w;j++){
              if(SeatF[i][j]==0){             
                 printf("   |0%d|",j);                               
                 }
            else
              printf("   |**|");                                  
            }
            printf("\n");
           }
    }
    
    
    int seatselling(int SeatF[7][10]){
    	int i,j,count=0;
        char s1[3],s2[3];
        printf("input your seat range 1:");
        scanf("%s",&s1);
    	printf("input your seat range 2:");
        scanf("%s",&s2);
        for (i=s1[1];i<=s2[1];i++){
          count++;
          SeatF[s1[0]-65][i]==1;
        }
        printf("%d",SeatF);
        DisplayFunc(7,10,SeatF);
        printf("Fee is $%d",count*80);
    }
    int main(){ 
     int i,j,SeatF[7][10]; 
           for(i=0;i<7;i++){
                 for(j=0;j<10;j++){
                      SeatF[i][j]=0;   
                                   }
                            }         
           SeatF[0][0]=1;
           SeatF[0][1]=1;  
            DisplayFunc(7,10,SeatF); 
            seatselling(SeatF);
           system("PAUSE");
    }
    but is still does not work.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    > SeatF[s1[0]-65][i]==1;
    This is still wrong!

    See above.

    > for (i=s1[1];i<=s2[1];i++)
    What do you actually type in?

    Because anything other than say "2A" and "2C" (you ignore the first character) will break your code.

    > int seatselling(int SeatF[7][10])
    By convention, always pass the sizes of arrays to functions (like your display function).
    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.

  12. #12
    Registered User
    Join Date
    Sep 2019
    Posts
    8
    the program is work right now,i wll fix the other bugs.
    but i think there may be wrong because the 29 statement look so weird.
    Code:
    int seatselling(int r,int w,int SeatF[7][10]){
    	int i,j,count=0;
        char s1[3],s2[3];
        printf("input your seat range 1:");
        scanf("%s",&s1);
    	printf("input your seat range 2:");
        scanf("%s",&s2);
        for (i=s1[1];i<=s2[1];i++){
          count++;
          SeatF[s1[0]-'F'][i+2]=1;
        }
        DisplayFunc(7,10,SeatF);
        printf("Fee is $%d",count*80);
    }
    are there something i need to do better?
    thanks for reply.
    Last edited by danielchs; 09-08-2019 at 04:14 AM.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    > are there something i need to do better?
    yeah, call DisplayFunc(r,w,SeatF);

    Also, use your range parameters.
    Code:
    int rpos = s1[0]-'F';
    int wpos = i+2;
    if ( rpos >= 0 && rpos < r && wpos >= 0 && wpos < w ) {
        SeatF[rpos][wpos]=1;
    } else {
        // error message?
    }
    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.

  14. #14
    Registered User
    Join Date
    Sep 2019
    Posts
    8
    here is another question.
    how can i check the inputs is at the same row and in ascending order.
    if the inputs is wrong . how can i ask them to input again.
    Code:
        printf("input your seat range 1:");
        scanf("%s",&s1);
        printf("input your seat range 2:");
        scanf("%s",&s2);
        if(s1[0]!=s2[0])
          printf("input wrong!");
        if(s1[1]>s2[1])
          printf("input wrong!");
    while loop ? do while loop?
    Last edited by danielchs; 09-08-2019 at 07:34 AM.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    You need a do-while loop, because you need the body of the code to run at least once.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dont know where im going wrong
    By africanwizz in forum C Programming
    Replies: 5
    Last Post: 10-06-2013, 08:32 AM
  2. I dont know what is wrong???
    By amroto in forum C Programming
    Replies: 3
    Last Post: 05-18-2012, 06:19 AM
  3. dont know whats wrong
    By otchster in forum C Programming
    Replies: 2
    Last Post: 11-02-2005, 11:14 AM
  4. i dont know what is wrong with it (help plz)
    By abuna in forum C++ Programming
    Replies: 16
    Last Post: 09-08-2005, 12:03 PM
  5. help......i dont understand what i am doing wrong?
    By Grifftt in forum C Programming
    Replies: 2
    Last Post: 10-18-2002, 11:37 PM

Tags for this Thread