Thread: problem with string and array

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    problem with string and array

    Ive got a problem with a program im trying to create

    I have a file with

    Name then 12 real number values continuing down until it reaches maybe a terminator of say xxxxx

    eg

    Jimmy 12.1 1.2 19.4 19.1 12.1 13.14 74.1 17.8 19.2 1.8 11.6
    barry 12.7 14.2 29.4 14.2 19.9 18.5 4.8 14.2 1.2 11.9 12.0
    xxxxx

    at the moment i can get it to read in the numbers in to an array and calculate the averages for each column

    i just cant get it to read in the name at the start of the row.

    would any1 be able to advise me on this.

    or provide a location to an example

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What's your current code?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Code:
    #include <stdio.h>
    #include <string.h>
    const int rows=4;
    const int cols=12;
    float num[rows][cols];
    float total, arraytotal;
    
    FILE *fp;
    
    main()
    {
          if((fp = fopen("array.txt","r"))==NULL)
                 printf("error opening file\n");
          else
                 {
                     for(int i=0;i<rows;i++)
                     {
                     total=0;
                     printf("\n\n\t\t");
                 
                     for(int j=0; j<cols; j++)
                     {
                             fscanf(fp,"%f",&num[i][j]);
                             printf("%7.2f",num[i][j]);
                             total=total+num[i][j];
                     }
                     printf("%7.2f",total);
                     }
                     
                     arraytotal=0;
                     printf("\n\n\n\t\t");
                     for(int j=0; j<cols; j++)
                     {
                             total=0;
                             for(int i=0; i<rows; i++)
                             total=total+num[i][j];
                             printf("%7.2f",total);
                             arraytotal=arraytotal+total;
                     }
                             printf("%7.2f", arraytotal);
                             }
                             
                             getchar();
    }

    as it stands this reads in the numbers into the array , calculates the row total and the column total

    as for getting it to read a string of letters at the start im completely stumped

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    your array type is a float - it cant store characters. you will need arrays of type char to store strings - a float array just wont.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    yes i know that, i just dont understand how it will differentiate the characters in the string from the values that are to be totaled, this is the reason i havent attempted this yet

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    well here's an application of a function that stops reading at white space - i'm more of a c coder, just started learning c++ in college, but i think that since you're already using c code here, you could use fscanf. this will read in text as far as a space - after which, you change the function you are using to handle floats. maybe that will give you an idea of how to proceed...
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    version 2: ive got it to read the string of characters, but it wont read the values horizontally it will only read them down vertically

    Code:
    #include <stdio.h>
    #include <string.h>
    
    const int rows=100;
    const int cols=100;
    float num[rows][cols];
    float total, arraytotal;
    const int maxchars=12;
    char name[rows][maxchars];
    static char fake[maxchars];
    
    
    FILE *fp;
    
    main()
    {
          int i=0;
          int j=0;
          if((fp = fopen("array.txt","r"))==NULL)
                 printf("error opening file\n");
          else
                 {
                    do
                    {
                     printf("\t");
                     fscanf(fp,"%s",&fake);
                     if(strcmp(fake,"ZZZZZ")!=0)
                     {
                             strcpy(name[i],fake);
                             fscanf(fp,"%f",&num[i][j]);
                             printf("%5s\n",&name[i]);
                             }
                             i++;
                             j++;
                             
                     }
                     while((strcmp(fake,"ZZZZZ")!=0)and(i<rows));
                     }
                     fclose(fp);
                     printf("\n");
    getchar();
    }

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    ive got it to read it untill it gets to my terminator zzzzz and it does the name and the value

    but they are listed vertically instead of horizontally

  9. #9
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    i took your code and fiddled with it and now it prints all the data across the console, each individual piece separated by a tab character. the issue with it printing every piece of information on a new line was because you had a printf with a new line character inside your loop. here is your code back again.

    if you wanted to save the numerical values into a float array, you would have to figure out some code to detect the change from text to numbers and work from that. im not sure if any of this is of use, you'll have to find out i guess.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    const int rows=100;
    const int cols=100;
    float num[rows][cols];
    float total, arraytotal;
    const int maxchars=12;
    char name[rows][maxchars];
    static char fake[maxchars];
    
    
    FILE *fp;
    
    main()
    {
          int i=0;
          int j=0;
          if((fp = fopen("array.txt","r"))==NULL)
                 printf("error opening file\n");
          else
                 {
                    do
                    {
                     fscanf(fp,"%s",&fake);
                     if(strcmp(fake,"xxxxx")!=0)
                     {
                             strcpy(name[i],fake);
                             fscanf(fp,"%f",&num[i][j]);
                             printf("%s\t",name[i]);
                             }
                             i++;
                             j++;
                             
                     }
                     while((strcmp(fake,"xxxxx")!=0)and(i<rows));
                     }
                     fclose(fp);
                     printf("\n");
    getchar();
    }
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you want to use a single array to represent the data you could use a 3 dimensional array of char, which could also be called a multicolumn table of strings if everything is done right.

    char table[numberOfRowsInTable][numberOfColumnsInTable][maxCharPerColumn];

    so

    char table[10][9][8];

    would be a table with 10 rows, each row has 9 columns, and each column could have up to 8 characters. If each column is a string, rather than a raw char array, then there could up to 7 useable char and 1 null terminating char per column. table[0][0] is the name in the first row. table[0][1] is the string representing a first numerical value in the first row, etc. To convert strings representing numerical values into numerical values so you can do mathematical operations on them you could use atoi() or atof() or atol() as appropriate, though it is recommended by people I respect that you use strtox() funtions (where x may be i, l, f, and maybe d) etc. instead of the respectivie atox() functions. If the numerical values were of type long then:

    long result = strtol(table[0][1]) + strtol(table[1][1]);

    represents the sum of the first two numerical values in the second column.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM