newbie needs help with creating html table

This is a discussion on newbie needs help with creating html table within the C Programming forums, part of the General Programming Boards category; What I'm trying to do is write a program that creates the HTML tags for an N by M table. ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    newbie needs help with creating html table

    What I'm trying to do is write a program that creates the HTML tags for an N by M table. Basically I prompt the user for the number of rows N and the number of columns M. Then the program prompts the user for the N times M items that will go into the table, basically a string, or number.

    Then the program writes out the HTML for a table that contains the items. I figured what I'll do I'll have any array for the max number of
    rows and columns. Then after the user enters the number of rows and columns, a while loop would prompt the user to enter something for that row + column, until they're all filled.

    That was my original intention, but all I keep getting is just a whole bunch of words the repeats itself like an never ending loop. And I'm
    at a complete loss trying to figure this out.





    Code:
    #include <stdio.h>
    
    /**
      
    */
    
    int
    main( void )
    {
          
          int rows;
          int columns;
          char string;
          int MaxRows[10];
          int MaxColumns[10];
          
          printf( "Enter the number of rows:  " );
          scanf( "%d" , &rows );
      
          printf( "Enter the number of columns:  " );
          scanf( "%d" , &columns );
    
          while( rows < MaxRows && columns < MaxColumns)
                 printf( "Enter the text that will go into this row  " );
               
    
    
          printf( "your table: %d" , rows , columns );
          
        
    }

    If anybody can get me on the right track, or at tell me where I'm royaly screwing up, it would sure be appreciated.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If you have, for example, a 5 rows by 10 columns table, you will need to ask the user to input 50 lines of text. Something more like:
    Code:
    int row, rows, column, columns;
    /* get a value for rows and columns from the user */
    for (row = 0; row < rows; row++)
    {
        for(column = 0; column < columns; columns++)
        {
            printf("Enter text for row %d, column %d: ", row, column);
            fflush(stdout);
            /* get the text for the specific cell */
        }
    }
    Then you can make a table in html with a similar loop.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    You seem to be getting the value of MAXRows and MAXColums mixed up. MAXRows and colums as it stands in your code is simply a pointer to an integer and at compile time, its still unitialized. when you do
    Code:
    while( rows < MaxRows && columns < MaxColumns)
                 printf( "Enter the text that will go into this row  " )
    your comparing a pointer to an integer and your compiler should tell you that.

    as for your question, the idea is that you want a loop to circulate up till the max row and clm the user enters and fill it with some info for each clm. so basically you'll be using a nested loop as in the following:i use gets() here only for a quick and dirty solution. use fgets when you actually implement your code
    Code:
    #include <stdio.h>
    #define ROW 10
    #define CLM 10
    int main(){
        char string[ROW];
        
        int i,j;
        
        for (i=0;i<ROW;i++)
        
          for(j=0;j<CLM;j++){
          printf("Enter data for row %d clm %d", i,j);
            gets(string);
            }
            
            
          getchar();
          return 0;
          }
    Last edited by caroundw5h; 10-30-2005 at 11:49 PM. Reason: forgot reference toMAXColums
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Thanks for your help guys, I have one more problem, I'm can't get it to print the entire input, only the vary last cell get printed. Here the updated code, thanks caroundw5h.

    Code:
    #include <stdio.h>
    #define ROW 2
    #define CLM 2
    int main(){
        
        
       
        char string[ROW];
        
        int i,j;
        
          
        
        for (i=0;i<ROW;i++)
        
          for(j=0;j<CLM;j++){
          printf("Enter data for row %d clm %d    ", i,j);
            gets(string);
            
            }
            
            printf(" < table border = 1 > \n\n" );
            puts(string);
            printf(" </table> \n\n" );
    }

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    the code i gave you was intended to show you how to capture input for each row and clm, not for you to actually implement. for example you shouldn't have constants for your rows and clms
    Code:
    #define ROW 2
    #define CLM 2
    you need to have the user specify these and then work with that.

    only the vary last cell gets printed
    Code:
    for (i=0;i<ROW;i++)
        
          for(j=0;j<CLM;j++){
          printf("Enter data for row %d clm %d    ", i,j);
            gets(string);
    gets updates the string array with the latest input. That is why you only print the last cell. Try tracing the program state and you'll see this.
    To have it print what you want, where you want is a logic problem and one that should be pretty easy to figure out...

    one way is this: think about it, you have a loop and gets() replaces the array buffer with the latest input, when do you want to print the value that is captured then?

    I won't give you the answer this is an exercise for you. And please don't use gets() when you actually code the program, i'll be ousted off the forum for giving you bad advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 06:06 PM
  3. Newbie question about creating a window
    By gozu in forum Windows Programming
    Replies: 1
    Last Post: 01-18-2007, 05:17 PM
  4. displaying the contents of an html file in a table
    By confuted in forum Tech Board
    Replies: 3
    Last Post: 08-02-2003, 08:50 PM
  5. help with creating a hash table
    By newbie40 in forum C++ Programming
    Replies: 3
    Last Post: 08-15-2002, 08:31 PM

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