Arrays and arrays of strings problem 
Author Message
 Arrays and arrays of strings problem

Hello,

I am doing an assignment that needs two sets of arrays, an array of strings
for employee's names and an array of floats for the employee's wages.

I have two questions:

1.  When I try to put values into the array of strings, name[10][40], I
always get a general protection fault when I compile.  I have tried both
scanf() and gets() to no avail.  I want the user to type in the name of the
employee at a prompt or enter a blank line if there are no more employees.  
I know I am doing something wrong with the string array because when I just
use the float array by itself it works fine.

I tried this:

char util[81]
char name[10][40];
float wage[10];
int x, y;

printf("\nEnter employee name or press Enter to stop--> ");

for (x=0; x<10; x++) {
        scanf (name[x]);
        if (!*name[x]) break;
        printf (Enter employee wages--> ");

Quote:
}

--
Sincerely,
Joe Sampson


Thu, 30 May 2002 03:00:00 GMT  
 Arrays and arrays of strings problem

Quote:

> I am doing an assignment that needs two sets of arrays, an array of strings
> for employee's names and an array of floats for the employee's wages.

> 1.  When I try to put values into the array of strings, name[10][40], I
> always get a general protection fault when I compile.

Really?  What compiler are you using?  Compilers shouldn't
segfault, especially on simple programs.

Quote:
> I have tried both
> scanf() and gets() to no avail.

scanf() is often a poor choice.  *Never* use gets().  fgets()
might be appropriate.

Quote:
> char util[81]
> char name[10][40];
> float wage[10];
> int x, y;

> printf("\nEnter employee name or press Enter to stop--> ");

> for (x=0; x<10; x++) {
>            scanf (name[x]);

That's not the way you use scanf().  Please refer to a C
reference manual for a description.  But I suggest you use
fgets() instead.

Quote:
>            if (!*name[x]) break;
>            printf (Enter employee wages--> ");

> }

--
"Large amounts of money tend to quench any scruples I might be having."
  -- Stephan Wilms


Thu, 30 May 2002 03:00:00 GMT  
 Arrays and arrays of strings problem
Hello,

Oops, I must have clicked send instead of save, I wasn't done and was
called away from the computer.  I'll edit the message...

I am doing an assignment that needs two sets of arrays, an array of strings
for employee's names and an array of floats for the employee's wages.

I have two questions:

1.  When I try to put values into the array of strings, name[10][40], I
always get a general protection fault when I compile.  I have tried both
scanf() and gets() to no avail.  I want the user to type in the name of the
employee at a prompt or enter a blank line if there are no more employees.  
I know I am doing something wrong with the string array because when I just
use the float array by itself it works fine.

I tried this:

char name[10][40];
float wage[10];
int x;

printf("\nEnter employee name or press Enter to stop.");

for (x=0; x<10; x++) {
        printf("\nEmployee name--> ");
        scanf ("%s", &name[x]);
        if (!*name[x]) break;
        printf ("\nEmployee wages--> ");
        scanf("%f", &wage[x]);

Quote:
}

This gives me a general protection fault using Borland Turbo C++ 4.5 (the

I'm running W98.  gets(), which is used in my textbook also gives me a
general protection fault.

Mr. Pfaff already replied, thank you BTW, and suggested I use fgets() which
I am unfamiliar with.  I will look it up and give it a shot.

2.  I also have to sort the arrays by last name and by wages using arrays
of pointers and sorting the arrays of pointers while keeping the original
arrays intact.  I cannot find a way to link the two different arrays so
when one gets sorted, the other gets sorted accordingly.  I am not to use
structures, only the two arrays and pointers.

--
Happy Holidays,
Joe Sampson



Fri, 31 May 2002 03:00:00 GMT  
 Arrays and arrays of strings problem

[I retained only the lines of interest]

Quote:
>char name[10][40];
>            scanf ("%s", &name[x]);
>            if (!*name[x]) break;
>This gives me a general protection fault using Borland Turbo C++ 4.5 ...

Feeding this sample code to gcc (by putting it inside a function,
and putting that inside a source file with "#include <stdio.h>")
produces:

        t.c: In function `f':
        t.c:12: warning: char format, different type arg (arg 2)

This is probably not the source of your protection fault, but it is
correct: the format "%s" needs an argument of type "char *", pointing
to the first of some set of "char"s.  The actual argument, &name[x],
has type "pointer to array 40 of char", rather than "pointer to char".
You could use either &name[x][0], or simply name[x].  But ...

The "%s" format for the scanf family also reads one "word" from
the input stream (here, stdin), after skipping any leading white
space, including blank lines.  That means that entering a blank
line is not going to satify scanf -- it will keep reading.  If
the line entered is, say, "   Joe Sampson", the %s will skip the
three leading blanks, stuff "Joe\0" into name[x][0] through name[x][3]
respectively, and return 1, leaving " Sampson" unconsumed in the
input stream.

Quote:
>2.  I also have to sort the arrays by last name and by wages using arrays
>of pointers and sorting the arrays of pointers while keeping the original
>arrays intact.  I cannot find a way to link the two different arrays so
>when one gets sorted, the other gets sorted accordingly. ...

(Sounds like homework.)  This description seems a bit odd: are you
supposed to sort a table of indices, or copies of the arrays?  If it
is the latter, you will wind up with something like:

        for (i = 0; i < n_items; i++)
                do_something(name[index[i]], wage[index[i]]);

which eliminates the problem.  (As you suspect -- in the "..." bit I
deleted -- an even nicer way to deal with this is to use structures.)
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Fri, 31 May 2002 03:00:00 GMT  
 Arrays and arrays of strings problem
Oops:

Quote:

>... This description seems a bit odd: are you
>supposed to sort a table of indices, or copies of the arrays?  If it
>is the latter,

That should read "former".

Quote:
>you will wind up with something like:

>    for (i = 0; i < n_items; i++)
>            do_something(name[index[i]], wage[index[i]]);

>which eliminates the problem.

Just to be more complete, one way to sort two separate tables --
you have to write your own sort of course, as qsort will not do
this -- is something like this (all untested!):

        /*
         * Perform an insertion sort on table item[], moving
         * things in value[] as well.  The type of the
         * elements in item[] is "key_type" and the type of
         * the elements in value[] is "val_type".
         */
        int compar(key_type, key_type);

        int findpos(int max, key_type key) {
                int i, mid, diff;

                /* binary search */
                for (low = 0, high = max - 1; low <= high;) {
                        mid = low + (high - low) / 2;
                        diff = compar(key, item[mid]);
                        if (diff < 0)
                                high = mid - 1;
                        else if (diff > 0)
                                low = mid + 1;
                        else {
                                fprintf(stderr, "warning: duplicate key\n");
                                return mid;
                        }
                }
                return low;
        }

        void do_insertion_sort(void) {
                int i, j, k;
                key_type newkey;
                val_type newval;

                /* insertion sort -- note that when n<2, table is sorted */
                for (i = 1; i < n_items; i++) {
                        /* assert: table item[0..i) is sorted */
                        newkey = item[i];
                        newval = value[i];
                        j = findpos(i, item[i]);
                        /* make room -- could use memmove() instead here */
                        for (k = i; k >= j; k--) {
                                item[k + 1] = item[k];
                                corresponding[k + 1] = correspnoding[k];
                        }
                        /* and insert */
                        item[j] = newkey;
                        value[j] = newval;
                }
        }
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Fri, 31 May 2002 03:00:00 GMT  
 Arrays and arrays of strings problem

Quote:
>Oops:


>>... This description seems a bit odd: are you
>>supposed to sort a table of indices, or copies of the arrays?  If it
>>is the latter,

>That should read "former".

I have to sort a group of names by last name and have the wages stay with
the name.

Like this:

original array as inputed by user...

NAME                 WAGES
Smith, John           1250
Clark, Steve          1235
Williams, Joe         1100

Sorted by last name...

Clark, Steve          1235
Smith, John           1250
Williams, Joe         1100

sorted by wage...

Williams, Joe         1100
Clark, Steve          1235
Smith, John           1250

then back to the original array,,,

Smith, John           1250
Clark, Steve          1235
Williams, Joe         1100

I had to do this using two different arrays, and sorting pointer arrays to
keep the original arrays intact.

I got it done by doing a sort and when one array of pointers got swapped, I
had the other array swapped as well, like this for the names:

for(out=0; out<count; out++)
        for(in=out+1; in<count; in++)
                if(strcmp(namePtr[out], namePtr[in]) > 0) {
                        nptr = namePtr[in];
                wptr = wagePtr[in];
                namePtr[in] = namePtr[out];
                wagePtr[in] = wagePtr[out];
                        namePtr[out] = nptr;
                        wagePtr[out] = wptr;
                }

and it worked.  I did the wage sort the same way.

As far as the actual input of the name strings, I gave up on scanf()
altogether and went a different route.

--
Sincerely,
Joe Sampson



Fri, 31 May 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Copy Array string to another Pointer array string in ANSI-C

2. convert list to array, arrays of pointers (to strings)

3. How To pass 2D String array to VB from VC++ Using Safe array

4. STL string to VARIANT Array (Safe Array)

5. Converting a string array to a int array

6. char array (array of array...) question

7. Help : Problems with Arrays of Arrays

8. problem realloc array inside array of struct

9. problem realloc array inside array of struct

10. Newbie Array Problem - how to strncpy from an array of char

11. Problem with Array of Strings

12. problems on array of strings char **

 

 
Powered by phpBB® Forum Software