Thread: help please

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    help please

    I have posted questions in the past but my code has evolved and i need some more assistance.. any help is appreciated..
    what i need to do is check to see if tid[i] is a character and if tname[i] is a digit. if they are i need to print an error and not store them in array. here is my code please help

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>



    #define MAXACCTS 16
    #define BUF 64
    #define NAME_SZ 10



    int main()
    {


    FILE *fp;
    FILE *out;
    char tname[MAXACCTS][NAME_SZ];
    int tid[MAXACCTS], i=0, size =0, k=0;
    float tamount[MAXACCTS];
    char buffer[BUF];


    //open pointers to files transact.txt and accounts.txt

    if(( fp = fopen("transact.txt", "r")) == NULL)
    {
    perror("UNABLE TO OPEN transact.txt\n");
    return -1;
    }

    if((out = fopen("accounts.txt", "w")) == NULL)
    {
    printf("UNABLE TO OPEN accounts.txt\n");
    system("pause");
    return -2;
    }

    for(size =0; size < MAXACCTS; ++size)
    {
    //read transact.txt and store in buffer
    while(fgets(buffer, BUF, fp) != NULL)
    {
    str

    // read string from file and store information in separate arrays
    if(sscanf(buffer,"%d %s %f", &tid[i], tname[i], &tamount[i]) ==3)
    {

    //verify id is valid
    if(tid[i] > 999 && tid[i] < 10000 )
    {
    //check name for less than ten characters
    if(strlen(tname[i]) <NAME_SZ)
    {
    //write valid accounts to Accounts.txt
    fprintf(out, "%5d\t%10s\t%10.2f\n", tid[i], tname[i], tamount[i]);
    }
    else
    printf("\nERROR: \t Account Name: \"%s\" is over %d characters\n\n", tname[i], NAME_SZ -1);
    }
    else
    printf("\nERROR: \t invalid ID %d\t%s\t\t%.2lf\n", tid[i], tname[i], tamount[i]);
    }
    else
    printf("Invalid transaction");


    }
    }
    printf("the value of size is %d\n", size);

    fclose(fp);
    fclose(out);
    return 0;


    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I didn't look through your code, but I assume you are having a difficult time determining if the value is an integer or character? You can use the functions

    isalpha()
    isdigit()

    They are defined in ctype.h
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    i attempted to use both the isalpha and isdigit functions in my code like below

    if(tid[i] > 999 && tid[i] < 10000 && isdigit(tid[i]))

    if(strlen(tname[i]) <NAME_SZ && isalpha(tname[i])

    when i do this my outputfile becomes blank..

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Have you tried stepping through it with the debugger yet? You could post the necessary files (either attach or copy and past) so that I can see what you are talking about.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    I have attached the input(transact.txt) and output(account.txt) files.

    i need to read in transactions from transact.txt, check the id for valid numbers between 999 and 10000, check the name for less than 10 characters, no numbers allowed in name.
    then i have to print to accounts.txt and sort them in order by id.

    I also have to figure out how to make an id unique so it cant be used by multiple names.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    It looks like you are reading and writing correctly so far. So are you confused how to sort them or what? Also, when you read in a new id you should just loop through the ones you have already read to make sure there isn't a duplicate. There are far better ways to do this but this is a very simple way. Good luck.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    yes i am very confused. I cant figure out how to verify that the array tid is in integer, and the char array tname is a letter.

    also ive tried sorting using a loop before i store them in the array but i am lost..


  8. #8
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    salut
    je vous conseille d'utiliser la bibliotheque <fstream.h>
    pour gere les fichier car c'est tre facile et tu peux manipuler sur tous les parie du fichier
    mais en ce qui concerne ta question
    tname[i] n'est pas un digit mais un caractere
    ....

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by enjoy
    salut
    je vous conseille d'utiliser la bibliotheque <fstream.h>
    pour gere les fichier car c'est tre facile et tu peux manipuler sur tous les parie du fichier
    mais en ce qui concerne ta question
    tname[i] n'est pas un digit mais un caractere
    ....
    le fstream est C++. Et parlez anglais svp.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    any more suggestions would be appreciated!!!!!

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    first use code tags here is your code indented. First one line comments are frownend upon(I hate them) here
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXACCTS 16
    #define BUF 64
    #define NAME_SZ 10
    
    int main(){
    	FILE *fp;
    	FILE *out;
    	char tname[MAXACCTS][NAME_SZ];
    	int tid[MAXACCTS], i=0, size =0, k=0;
    	float tamount[MAXACCTS];
    	char buffer[BUF];
    
    //open pointers to files transact.txt and accounts.txt
    
    	if(( fp = fopen("transact.txt", "r")) == NULL){
    		perror("UNABLE TO OPEN transact.txt\n");
    		return -1;
    	}
    	if((out = fopen("accounts.txt", "w")) == NULL){
    		printf("UNABLE TO OPEN accounts.txt\n");
    		system("pause");
    		return -2;
    	}
    
    	for(size =0; size < MAXACCTS; ++size){
    		//read transact.txt and store in buffer
    		while(fgets(buffer, BUF, fp) != NULL){
    			str /*where does this come from*/
    			// read string from file and store information in separate arrays
    			if(sscanf(buffer,"%d %s %f", &tid[i], tname[i], &tamount[i]) ==3){ 
    			//verify id is valid
    				if(tid[i] > 999 && tid[i] < 10000 ){
    				//check name for less than ten characters
    					if(strlen(tname[i]) <NAME_SZ){ 
    						//write valid accounts to Accounts.txt
    						fprintf(out, "%5d\t%10s\t%10.2f\n", tid[i], tname[i], tamount[i]);
    					}
    					else 
    						printf("\nERROR: \t Account Name: \"%s\" is over %d characters\n\n", tname[i], NAME_SZ -1);
    				}
    				else
    					printf("\nERROR: \t invalid ID %d\t%s\t\t%.2lf\n", tid[i], tname[i], tamount[i]);
    			}
    			else 
    				printf("Invalid transaction");
    		}
    	}
    	printf("the value of size is %d\n", size);
    	fclose(fp);
    	fclose(out);
    	return 0;
    }
    Did you expect anyone to follow all those nested if statments in a while loop in a for loop.
    Last edited by linuxdude; 04-19-2004 at 11:54 AM.

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    I couldnt figure out a better way to accomplish the things i needed to.. I am not supposed to use structures so thats what i came up with.. any suggestions to improve it would be appreciated

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by linuxdude
    First one line comments are frownend upon(I hate them)
    Por que? I find it refreshing to see comments of any kind on this board. Are you suggesting one should not comment code if the comment only needs one line? Or when one line is enough of a comment you should add filler to make it 2 or more lines?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    A well worded one line comment can be much more informative then some multi-line comments. Often times multi-line comments in the middle of the code make it harder to follow the code since you have a gap.

  15. #15
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I just replace // with /**/ oh and since we are in another language
    porque no me gusta.

Popular pages Recent additions subscribe to a feed