Sponsored Content
Full Discussion: integer arrays
Top Forums Programming integer arrays Post 16985 by Perderabo on Saturday 9th of March 2002 04:30:42 PM
Old 03-09-2002
You are not checking for overflowing your arrays. I put that in. It's the LISTMAX stuff and a slight mode to your scanf's. I didn't like the way you passed count into your function. So I did that a little differently. And I added a few statements to print out the arrays to be sure that it was working.

You will want to bump up LISTMAX, I made it low for easy testing...

Here is my modified code...
Code:
#include <stdio.h>

#define LISTMAX 3

void main(){
    char *name[LISTMAX], ch;
    int num, count, refNumber[LISTMAX];
    int i;
    int insert(char *name[],int refNumber[]);  
    count=insert(name,refNumber);
    printf("count = %d \n", count);
    for(i=0; i<count; i++) {
        printf("%30s  %d\n", name[i], refNumber[i]);
    }
    exit(0);
}

int insert(char *name[],int refNumber[]){ 
    int count;
    int i, temp3[4];
    char temp[30],choice[5];
    count=0;
    printf("\nDo you want to enter client data?\n");
    fflush(stdin);
    scanf("%5[^\n]", choice);
    for(i=0;choice[i]!='\0';i++){
        choice [i] = toupper(choice[i]);
    }
    while((!(strcmp(choice,"YES")==0) && !(strcmp(choice,"NO") == 0))){
        printf("\nInvalid option!\t \"yes\" or \"no\"\n");
        fflush(stdin);
        scanf("%5[^\n]", choice);
        for(i=0;choice[i]!='\0';i++){
            choice [i] = toupper(choice[i]);
        }
    }
    while(strcmp(choice, "YES") ==0){
        printf("\nEnter Client Name:\n");
        fflush(stdin);
        scanf("%30[^\n]", temp);
        name[count] = (char *) malloc (strlen(temp) + 1);
        strcpy (name[count], temp);

        printf("\nEnter 4 Digit Reference Number:\n");
        fflush(stdin);
        scanf("%4d",&temp3[count]);
        while(temp3[count]<1000||temp3[count]>9999){
            printf("\nInvalid Option!");
            printf("\nEnter 4 Digit Reference Number:\n");
            fflush(stdin);
            scanf("%d",&temp3[count]);
            }
        refNumber[count]=temp3[count];

        count++;
        if (count == LISTMAX) {
            printf("tables are full\n");
            return LISTMAX;
        }
        printf("\nDo you want to enter data for another client?\n");
        fflush(stdin);
        scanf("%[^\n]", choice);
        for(i=0;choice[i]!='\0';i++){
            choice [i] = toupper(choice[i]);
        }
        while((!(strcmp(choice,"YES")==0) && !(strcmp(choice,"NO") == 0))){
            printf("\nInvalid option!\t \"yes\" or \"no\"\n");
            fflush(stdin);
            scanf("%[^\n]", choice);
            for(i=0;choice[i]!='\0';i++){
                choice [i] = toupper(choice[i]);
            }
        }
    }
    return count;
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

increment an integer

hi I want to echo the variable $i while it auto-increments till 21 I set initially i to 1 any idea how to do that? thank you (4 Replies)
Discussion started by: melanie_pfefer
4 Replies

2. UNIX for Dummies Questions & Answers

Rouding off an integer

HI I want to round off an integer to the next multiple of 10 in shell script. (i.e.,) 91 should be rounded off to 100 and 90 should be rounded off to 90 It would be very helpful, if you can help me in this. Thanks in advance (4 Replies)
Discussion started by: dayamatrix
4 Replies

3. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

4. UNIX for Dummies Questions & Answers

Non-integer caluclations

Hey I am trying to calculate a number but I found out the expr I knew works only with integers. Any help. I want to calculate (120/220) *100. Thanks! (2 Replies)
Discussion started by: #moveon
2 Replies

5. UNIX for Dummies Questions & Answers

integer to string

Hi all, is there an easy way to convert integer to string in bash? I have numbers like 1, 2, ..., 112, ... and I would like to get 001 002 003 004 ... Thank you, Sarah (4 Replies)
Discussion started by: f_o_555
4 Replies

6. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

7. Solaris

How to Use a Variable as Integer?

hello, i am writing a script that takes the UID from the PASSWD and then i want to increse the Number by one. for the Next user. i cannot get this to work that a variable is as interger example: set i = 0 set $i = $+1 it's in tcsh if it's mather (10 Replies)
Discussion started by: shatztal
10 Replies

8. Shell Programming and Scripting

Convert to Integer

Hi fellows!! i'm doing something which is not working out for me properly which i don't understand why nowdate=`date +%s` echo $nowdate now the problem how to convert a date which is stored in a variable mydate="22/Oct/2011" mydate=`date -d '$mydate' +%s` it gives error... (11 Replies)
Discussion started by: me_newbie
11 Replies

9. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

10. Programming

Are These All The Integer Types In C

Hello and Good day, I am currently studying C and I just finished learning about variables mainly those of integer type. I am wondering if the list below are all there is to integer variables and there are still more that i have to learn. Here are the list: Char Short int long long long... (3 Replies)
Discussion started by: split_func0
3 Replies
STRING(3)						     Library Functions Manual							 STRING(3)

NAME
strcat, strncat, strcmp, strncmp, strcasecmp, strncasecmp, strcpy, strncpy, strlen, index, rindex - string operations SYNOPSIS
#include <strings.h> char *strcat(s, append) char *s, *append; char *strncat(s, append, count) char *s, *append; int count; strcmp(s1, s2) char *s1, *s2; strncmp(s1, s2, count) char *s1, *s2; int count; strcasecmp(s1, s2) char *s1, *s2; strncasecmp(s1, s2, count) char *s1, *s2; int count; char *strcpy(to, from) char *to, *from; char *strncpy(to, from, count) char *to, *from; int count; strlen(s) char *s; char *index(s, c) char *s, c; char *rindex(s, c) char *s, c; DESCRIPTION
These functions operate on null-terminated strings. They do not check for overflow of any receiving string. Strcat appends a copy of string append to the end of string s. Strncat copies at most count characters. Both return a pointer to the null- terminated result. Strcmp compares its arguments and returns an integer greater than, equal to, or less than 0, according as s1 is lexicographically greater than, equal to, or less than s2. Strncmp makes the same comparison but looks at at most count characters. Strcasecmp and strncasecmp are identical in function, but are case insensitive. The returned lexicographic difference reflects a conversion to lower-case. Strcpy copies string from to to, stopping after the null character has been moved. Strncpy copies exactly count characters, appending nulls if from is less than count characters in length; the target may not be null-terminated if the length of from is count or more. Both return to. Strlen returns the number of non-null characters in s. Index (rindex) returns a pointer to the first (last) occurrence of character c in string s or zero if c does not occur in the string. Set- ting c to NULL works. 4th Berkeley Distribution October 22, 1987 STRING(3)
All times are GMT -4. The time now is 03:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy