Thread: sequential file program

  1. #31
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This program is quite elegant in it's use of pointers. Except for the memory leak, it's a gem, and clearly, not your original code. However, after the original wheel, there has never been an original outside rim shape for any wheel, either. They're all round.

    NHBad, take a look at the transaction code, and write out in just plain English, what would be the programs logic for each code. Something like:

    Code is 0:
    a) add a new blank record to the database with an ID number and name.
    b) whatever

    Code is 1:
    a) etc.

    etc.

    Then we'll take that and form it up into code, using that logic. So if your logic is haywire, the resulting transaction function code, will also be haywire.

    Time is of the essence here. If you take a week or ten days to get back with your input, you're taking a big chance nobody will still be interested in this program. In about a week, I will be starting another programming project, and be quite unavailable. If you want to finish this project with assistance from this forum, I'd put it in high gear. As you've seen, a simple bump is not much help.

  2. #32
    Registered User
    Join Date
    May 2008
    Posts
    61
    okay, so the types are:
    Code:
    4. The keys should be in sorted order, with duplicates.  The interpretation is as follows:
    Type = 0, insert a new record with name name and key key, and all other values 0.  
    key should have at least one character, that is, strlen (key) > 0.
    
    Type = 1, 2, or 3, add value points to test.  (value may be negative.)  
    Check for constraint on resulting test score.	
    
    Type = 4, add one assignment and value points for homework, checking the constraint. 
     value should be non-negative, and less than or equal 5.
    
    Type = 5, add value points for homework, without changing hwCount.  
    Total hwPoints should not exceed 5 * hwCount.
    Thus, how I see it goes is:

    Type 0: inserts a new record with a name and key in their respective spots with rest of the values zero.

    Type 1, 2, 3: inserts values for each test; test1, test2, test3.

    Type 4: add a value to hwCount and additional value points for homework, maxing at 5.

    Type 5: add value points to the homework parameter (hwPoints) without changing hwCount with the total not exceeding 5*hwCount
    Last edited by needhelpbad; 05-30-2008 at 02:41 AM.

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by needhelpbad View Post
    okay, so the types are:
    Code:
    4. The keys should be in sorted order, with duplicates.  The interpretation is as follows:
    Type = 0, insert a new record with name name and key key, and all other values 0.  
    key should have at least one character, that is, strlen (key) > 0.
    
    Type = 1, 2, or 3, add value points to test.  (value may be negative.)  
    Check for constraint on resulting test score.	
    
    Type = 4, add one assignment and value points for homework, checking the constraint. 
     value should be non-negative, and less than or equal 5.
    
    Type = 5, add value points for homework, without changing hwCount.  
    Total hwPoints should not exceed 5 * hwCount.
    Thus, how I see it goes is:
    So fleshing this out to code, partway:

    Code:
    switch (trans)   {
       case 0: inserts a new record with a name and key in their respective spots with rest of
                    the values zero; 
                    break;  
    
       case 1: insert value for test1; break;
       case 2: insert value for test2; break;
       case 3: insert value for test3; break;
    
       case 4: add a value to hwCount; 
                    add additional value points for homework, maxing at 5;
                    break;
    
       case 5: add value points to the homework parameter (hwPoints) without changing
                    hwCount with the total not exceeding 5*hwCount
                    break;
    
    }
    I'll add some more to it, later. Looks like a start, though.

  4. #34
    Registered User
    Join Date
    May 2008
    Posts
    61
    obviously im new to this, but ive never seen "switch"

    how does it act, compared to such as a struct or a void?

  5. #35
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by needhelpbad View Post
    obviously im new to this, but ive never seen "switch"

    how does it act, compared to such as a struct or a void?
    I don't even know what that question means. A struct is an object (a group of variables, held together by a common name), and doesn't act at all. Void is just a keyword used to denote that a function either doesn't return a result or doesn't take any parameters (depending on where it's used), or as a sort of generic type (a "void pointer" is a pointer to ... something that we don't know what it is).

    A switch is program control, like if, or while, or for. Get your textbook (or favorite C tutorial site), and look up "switch" in the index.

  6. #36
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A switch statement is just like a series of if statements, and is very useful and elegant to use, where you have a large number of choices to be made by your program.

    You could code it like this:
    Code:
    if(trans == 0)   {
        //list the various things to do if trans is 0
    }
    else if(trans == 1)   {
       //list the various things for the program to do if trans is 1
    }
    else if(trans == 2)   {
       //etc.
    etc.

  7. #37
    Registered User
    Join Date
    May 2008
    Posts
    61
    okay, that makes a lot more sense, ill see what i can do with it

  8. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'd start with something simpler, at first. The switch statement for the sequential file program will still need more code, in order to function.

    Code:
    int number;
    
    printf("\n Enter a number between 1 and 3");
    scanf("%d, &number);
    
    switch (number)   {
    
       case 1: printf("\n You entered a one"); break;
       case 2: printf("\n You entered a two"); break;
       case 3: printf("\n You entered a three"); break;
    
    //but watch this:
       case 0: printf("\n That's not between 1 and 3");  //no break
       case 4: printf("\n Please read more carefully, next time");  //no break
    
       default: printf("\n Because I won't always be here to remind you"); break;
    }
    Switch statements have a unique "fall through" feature, unlike anything else in the C syntax, that I know of. It can be very useful, but it will catch you badly if you forget the break statement you usually need, at the end of each case statement.

  9. #39
    Registered User
    Join Date
    May 2008
    Posts
    61
    looking how this transaction data is coming, does my actual seq_trans file look like it would apply?

    Code:
    3	100	5	Jeff	
    4	101	5	Mark	
    1	102	3	Jenn	
    2	103	5	Chris	
    4	104	4	Matt	
    3	105	3	Dan	
    0	106	4	Dave	
    5	107	14	Rich	
    3	108	2	Mike
    1	109	4       Tony	
    4	110	5	Rob	
    2	111	-4	Sonny	
    3	112	4	Luke	
    4	113	3	April	
    2	114	9	Sheryl	
    4	115	4	Tom	
    5	116	18	Lamont	
    1	117	3	Steve	
    2	118	1	Jake	
    3	119	3	Austin	
    0	0	0
    and is this how the coding would go for the switches? be gentle.

    Code:
    switch(trans) {
    
    if(trans == 0)   {
    
    printf("Enter a key and name, respectively.");
    scanf("%d%d", &theId, &theName);
    
    theTest1 = 0;
    theTest2 = 0;
    theTest3 = 0;
    theHwCount = 0;
    theHwPoints = 0;
    
       }
    else if(trans == 1)   {
    
    printf("Enter value for test 1");
    scanf("%d", &theTest1);
    
       }
    else if(trans == 2)   {
    
    printf("Enter value for test 2");
    scanf("%d", &theTest2);
    
       }
    else if(trans == 3)   {
    
    printf("Enter a value for test 3");
    scanf("%d", &theTest3);
    
       }
    else if(trans == 4)   {
    
    printf("Enter a value for homework count");
    scanf("%d", &theHwCount);
    printf("Any additional points for homework? (0 -> 5)");
    scanf("%d", &theHwPoints);
    
       }
    else if(trans == 5)   {
    
    printf("Enter a value for extra homework points, not exceeding 5 times actual homework count");
    scanf("%d", &theHwPoints);
    
       }
    }

  10. #40
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by needhelpbad View Post
    looking how this transaction data is coming, does my actual seq_trans file look like it would apply?

    Code:
    3	100	5	Jeff	
    4	101	5	Mark	
    1	102	3	Jenn	
    2	103	5	Chris	
    4	104	4	Matt	
    3	105	3	Dan	
    0	106	4	Dave	
    5	107	14	Rich	
    3	108	2	Mike
    1	109	4       Tony	
    4	110	5	Rob	
    2	111	-4	Sonny	
    3	112	4	Luke	
    4	113	3	April	
    2	114	9	Sheryl	
    4	115	4	Tom	
    5	116	18	Lamont	
    1	117	3	Steve	
    2	118	1	Jake	
    3	119	3	Austin	
    0	0	0
    I don't believe so. It should have repeats of ID and name with their test score additions, etc.

    and is this how the coding would go for the switches? be gentle.



    Code:
    switch(trans) {
    
    case 0: //no if(trans == 0)   { needed.
    
    printf("Enter a key and name, respectively.");
    scanf("%d%d", &theId, &theName);
    
    theTest1 = 0;
    theTest2 = 0;
    theTest3 = 0;
    theHwCount = 0;
    theHwPoints = 0;
    
       }
    case 1: //no else if(trans == 1)   { needed.
    
    printf("Enter value for test 1");
    scanf("%d", &theTest1);
    
       }
    
    case 2: //no else if(trans == 2)   { needed
    
    printf("Enter value for test 2");
    scanf("%d", &theTest2);
    
       }
    //etc.
    else if(trans == 3)   {
    
    printf("Enter a value for test 3");
    scanf("%d", &theTest3);
    
       }
    else if(trans == 4)   {
    
    printf("Enter a value for homework count");
    scanf("%d", &theHwCount);
    printf("Any additional points for homework? (0 -> 5)");
    scanf("%d", &theHwPoints);
    
       }
    else if(trans == 5)   {
    
    printf("Enter a value for extra homework points, not exceeding 5 times actual homework count");
    scanf("%d", &theHwPoints);
    
       }
    }
    Nope, sorry. The "job" of a switch statement is to remove a lot of if statements. Here, you start with a switch statement, and then proceed with the if statements, anyway.

    Play and experiment with the simple switch statement I gave you earlier, and you'll get the hang of it.

  11. #41
    Registered User
    Join Date
    May 2008
    Posts
    61
    so would this be the right way?

    Code:
    int trans;
    
    printf("\n Which transaction type is needed? (0-5)");
    scanf("%d", &trans);
    
    switch(trans) {
    
    case 0: 
    printf("Enter a key and name, respectively.");
    scanf("%d%d", &theId, &theName);
    
    theTest1 = 0;
    theTest2 = 0;
    theTest3 = 0;
    theHwCount = 0;
    theHwPoints = 0;
    break;
    
    case 1: 
    printf("Enter value for test 1");
    scanf("%d", &theTest1);
    break;
    
    case 2: 
    printf("Enter value for test 2");
    scanf("%d", &theTest2);
    break;
    
    case 3:
    printf("Enter a value for test 3");
    scanf("%d", &theTest3);
    break;
    
    case4:
    printf("Enter a value for homework count");
    scanf("%d", &theHwCount);
    printf("Any additional points for homework? (0 -> 5)");
    scanf("%d", &theHwPoints);
    break;
    
    case 5:
    printf("Enter a value for extra homework points, not exceeding 5 times actual homework count");
    scanf("%d", &theHwPoints);
    break;
    
    }
    I also have a question or two about the code you posted earlier:

    in this struct:
    Code:
    struct Student * getOneStudentFromData ( ) {
       struct Student * aStudentPtr;
       int scanned, theId, theTest1, theTest2, theTest3, theHwCount, theHwPoints;
       char theName [7];
       char cr;
       fscanf (infile, "%d%s%d%d%d%d%d%c",
    	&theId, theName, &theTest1, &theTest2, &theTest3,
    	&theHwCount, &theHwPoints, &cr);
    you have int scanned, but thats the only place I ever see 'scanned' in the whole code, what is it for?

    and what does the char cr refer to?
    Last edited by needhelpbad; 05-31-2008 at 11:13 AM.

  12. #42
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's a good start to using a switch statement. An important error control feature is this:

    Code:
    scanf("%d", &number);
    
    switch (number)   {
    
       case 0: //code for zero; break;
       case 1: //code for one; break;
       etc.
    
       default: printf("\n      Error #1 has Occurred in the Program!");
                    //your program should never reach this line, but has
    }
    You would be surprised how often a simple error-catcher like default in a switch statement, can catch a bug in your code. I don't always use it for a simple program, but I recommend it, and use it for non-trivial programs.

    It's a 10.

    For the sequential file program, the logic you show is not complete, but it's a start.

  13. #43
    Registered User
    Join Date
    May 2008
    Posts
    61
    adding in the default;

    Code:
    int trans;
    
    printf("\n Which transaction type is needed? (0-5)");
    scanf("%d", &trans);
    
    switch(trans) {
    
    case 0: 
    printf("Enter a key and name, respectively.");
    scanf("%d%d", &theId, &theName);
    
    theTest1 = 0;
    theTest2 = 0;
    theTest3 = 0;
    theHwCount = 0;
    theHwPoints = 0;
    break;
    
    case 1: 
    printf("Enter value for test 1");
    scanf("%d", &theTest1);
    break;
    
    case 2: 
    printf("Enter value for test 2");
    scanf("%d", &theTest2);
    break;
    
    case 3:
    printf("Enter a value for test 3");
    scanf("%d", &theTest3);
    break;
    
    case4:
    printf("Enter a value for homework count");
    scanf("%d", &theHwCount);
    printf("Any additional points for homework? (0 -> 5)");
    scanf("%d", &theHwPoints);
    break;
    
    case 5:
    printf("Enter a value for extra homework points, not exceeding 5 times actual homework count");
    scanf("%d", &theHwPoints);
    break;
    
    default: printf("You have encountered an error!");
    
    }
    I'm not noticing where my logic is imcomplete, I am sure I have a bunch wrong or that could be fixed, I am just not recognizing it

  14. #44
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't these values have to be written to the file?

    I think so.

  15. #45
    Registered User
    Join Date
    May 2008
    Posts
    61
    seems like the easiest things are the easiest to miss..

    so I know I would have to intertwine writeRecord function in there, but..im not sure how it would work

    for something like case 3, would it be..

    Code:
    case 3:
    printf("Enter a value for test 3");
    scanf("%d", &theTest3);
    writeRecord(&theTest3);
    
    //or maybe
    
    writeRecord(theStudentPtr->test3);
    break;
    or something along those lines?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. fputs and sequential file
    By sballew in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 04:48 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM