Thread: Storing values?

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Storing values?


    Can more than one value be stored if you do not know the number of values that need to be stored?

    e.g. I am working on a program that will eventually write student information and GPA's to file. But, I am not to the IO part of the writing process yet. The code is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    		 struct student{
    		 		char lname[20];
    		 		char fname[10];
    		 		int tstgrade[3];
    		 		int gpa;
    				};
    		 		
    
    int main()
    {
      
      int i=0; 
      int count=0;
      
      	struct student student;
      
      FILE *pread;
      FILE *pwrite;
      FILE *Append;
      
      printf("***********************************************************\n");
      printf("***********************************************************\n");
      printf("***                                                     ***\n");
      printf("***               Student G.P.A.                        ***\n");
      printf("***                                                     ***\n");
      printf("***********************************************************\n");
      printf("***********************************************************\n\n\n");
      
      
      printf("How many grades would you like to enter? ");
      
      scanf("%i", &i);
      
      while (count < i)
      {
      // Enter student information
      	 printf("\nEnter student last name: ");
      	 scanf("%s", &student.lname[0]);
      	 printf("Enter student first name: ");
      	 scanf("%s", &student.fname[0]);
      
      	 printf("Enter first grade for %s, %s: ", student.lname, student.fname);
      	 scanf("%i", &student.tstgrade[0]);
      	 printf("Enter second grade for %s, %s: ", student.lname, student.fname);
     	 scanf("%i", &student.tstgrade[1]);
      	 printf("Enter third grade for %s, %s: ", student.lname, student.fname);
      	 scanf("%i", &student.tstgrade[2]);
      	 count++;
    }
      
    	printf("\n\n\nStudent Name\t\t\tTest1\tTest2\tTest3\t GPA\n");
      student.gpa = (student.tstgrade[0] + student.tstgrade[1] + student.tstgrade[2]) / 3;
      
      do
      {
      	printf("%s,%s\t\t %i\t %i\t %i\t %i\n", student.lname, student.fname, student.tstgrade[0],\
    	   student.tstgrade[1], student.tstgrade[2], student.gpa);
    
    } while (i = i- 1);
    
      system("PAUSE");	
      return 0;
    }
    The code actually works well although probably not professional, the issue though is currently it only stores the last information entered, which I understand, but without writing the files now is it possible to store multiple values?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    At the moment, you're only storing one student.
    > struct student student;

    Start with say
    Code:
     	struct student students[100];
    Then
    Code:
      printf("How many grades would you like to enter? (max 100)");
      scanf("%i", &count);
    Your input loop should be
    Code:
    for ( i = 0 ; i < count ; i++ ) {
      	 printf("\nEnter student last name: ");
      	 scanf("%s", &students[i].lname[0]);
      	 // this also works for %s and char arrays
             // scanf("%s", students[i].lname);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Helped


    That solved the storing values (guess I hove more reading on arrays to do).

    Two issues did come up:

    The math function stopped working,

    Code:
    student[i].gpa = (student[i].tstgrade[0] + student[i].tstgrade[1] + student[i].tstgrade[2]) / 3;

    And while the values store and display my test entries (tested up to 6 entries) the entry at [0] goes null. Any reason why that happens but, shows 2-6?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Hard to say without seeing your latest code.

    > char lname[20];
    > char fname[10];
    But if you type in long names, you will trash data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by salem
    Hard to say without seeing your latest code.

    > char lname[20];
    > char fname[10];
    But if you type in long names, you will trash data.
    The only thing I have noticed regarding name length is it throwing off alignment in the print out. For instance, name[0] would be "name, 1". In the print out it would read " , "and grades would print as 0,0,0 and g.p.a. 0 regardless of entry. Where as name[1] showing as, "name, 2" the 3 grades as entered and g.p.a. 0.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    		 struct student{
    		 		char *lname[20];
    		 		char *fname[10];
    		 		int tstgrade[3];
    		 		int gpa;
    				};
    		 		
    
    int main()
    {
      
      int i=0; 
      int count=0;
      
      	struct student student[100];
      
      FILE *pread;
      FILE *pwrite;
      FILE *Append;
      
      printf("***********************************************************\n");
      printf("***********************************************************\n");
      printf("***                                                     ***\n");
      printf("***               Student G.P.A.                        ***\n");
      printf("***                                                     ***\n");
      printf("***********************************************************\n");
      printf("***********************************************************\n\n\n");
      
      
      printf("How many grades would you like to enter? ");
      
      scanf("%i", &count);
      
      for (i = 0 ; i < count ; i++)
      {
      // Enter student information
      	 printf("\nEnter student last name: ");
      	 scanf("%s", &student[i].lname);
      	 printf("Enter student first name: ");
      	 scanf("%s", &student[i].fname);
      
      	 printf("Enter first grade for %s, %s: ", student[i].lname, student[i].fname);
      	 scanf("%i", &student[i].tstgrade[0]);
      	 printf("Enter second grade for %s, %s: ", student[i].lname, student[i].fname);
     	 scanf("%i", &student[i].tstgrade[1]);
      	 printf("Enter third grade for %s, %s: ", student[i].lname, student[i].fname);
      	 scanf("%i", &student[i].tstgrade[2]);
      	 //count++;
    }
      
    	printf("\n\n\nStudent Name\t\tTest1\tTest2\tTest3\t GPA\n");
      student[i].gpa = (student[i].tstgrade[0] + student[i].tstgrade[1] + student[i].tstgrade[2]) / 3;
      
      do
      {
      	printf("%s,\t%s\t\t %i\t %i\t %i\t %i\n", student[i].lname, student[i].fname, student[i].tstgrade[0],\
    	   student[i].tstgrade[1], student[i].tstgrade[2], student[i].gpa);
    
    } while (i = i - 1);
    
      system("PAUSE");	
      return 0;
    }
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Somewhat corrected


    I changed the " do while loop" to copy the "for loop" and moved the gpa calculation into the print report loop and it is working without flushing the first student entry and does allow the math to show a value.

    Now, is there a way to "auto-correct" the formatting based on string (or in C's case char) length? If there is a short name then a long name, the columns no longer are aligned?

    Originally Posted by astonecipher
    The only thing I have noticed regarding name length is it throwing off alignment in the print out. For instance, name[0] would be "name, 1". In the print out it would read " , "and grades would print as 0,0,0 and g.p.a. 0 regardless of entry. Where as name[1] showing as, "name, 2" the 3 grades as entered and g.p.a. 0.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    		 struct student{
    		 		char *lname[20];
    		 		char *fname[10];
    		 		int tstgrade[3];
    		 		int gpa;
    				};
    		 		
    
    int main()
    {
      
      int i=0; 
      int count=0;
      
      	struct student student[100];
      
      FILE *pread;
      FILE *pwrite;
      FILE *Append;
      
      printf("***********************************************************\n");
      printf("***********************************************************\n");
      printf("***                                                     ***\n");
      printf("***               Student G.P.A.                        ***\n");
      printf("***                                                     ***\n");
      printf("***********************************************************\n");
      printf("***********************************************************\n\n\n");
      
      
      printf("How many grades would you like to enter? ");
      
      scanf("%i", &count);
      
      for (i = 0 ; i < count ; i++)
      {
      // Enter student information
      	 printf("\nEnter student last name: ");
      	 scanf("%s", &student[i].lname);
      	 printf("Enter student first name: ");
      	 scanf("%s", &student[i].fname);
      
      	 printf("Enter first grade for %s, %s: ", student[i].lname, student[i].fname);
      	 scanf("%i", &student[i].tstgrade[0]);
      	 printf("Enter second grade for %s, %s: ", student[i].lname, student[i].fname);
     	 scanf("%i", &student[i].tstgrade[1]);
      	 printf("Enter third grade for %s, %s: ", student[i].lname, student[i].fname);
      	 scanf("%i", &student[i].tstgrade[2]);
      	 //count++;
    }
      
    	printf("\n\n\nStudent Name\t\tTest1\tTest2\tTest3\t GPA\n");
      student[i].gpa = (student[i].tstgrade[0] + student[i].tstgrade[1] + student[i].tstgrade[2]) / 3;
      
      do
      {
      	printf("%s,\t%s\t\t %i\t %i\t %i\t %i\n", student[i].lname, student[i].fname, student[i].tstgrade[0],\
    	   student[i].tstgrade[1], student[i].tstgrade[2], student[i].gpa);
    
    } while (i = i - 1);
    
      system("PAUSE");	
      return 0;
    }
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    char *lname[20];
    char *fname[10];
    ...
    scanf("%s", &student[i].lname);
    printf("Enter student first name: ");
    scanf("%s", &student[i].fname);

    This is all wrong.

    You might have "solved" your array overrun by allocating say 80 bytes for lname, rather than 20, but you're abusing the space. You were closer in your first post.

    Use this, but change the 20 to be whatever is appropriate for your data set.
    Code:
    char lname[20];
    scanf("%s", student[i].lname);
    Regarding the output formatting, you can do this
    Code:
    printf("%20s,%10s\n", student[i].lname, student[i].fname );
    printf("%-20s,%-10s\n", student[i].lname, student[i].fname );
    Regarding code formatting
    Code:
      	printf("%s,\t%s\t\t %i\t %i\t %i\t %i\n", student[i].lname, student[i].fname, student[i].tstgrade[0],\
    	   student[i].tstgrade[1], student[i].tstgrade[2], student[i].gpa);
    You don't gain anything from having a \ here.
    There is nothing wrong with say
    Code:
      printf("%s,\t%s\t\t %i\t %i\t %i\t %i\n", 
        student[i].lname, student[i].fname, 
        student[i].tstgrade[0], student[i].tstgrade[1], student[i].tstgrade[2],
        student[i].gpa);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0
    Okay, I am confused now. And I would like to actually get it right as opposed to "it works so leave it." I am confused with:

    Your input loop should be


    for ( i = 0 ; i < count ; i++ ) {
    printf("\nEnter student last name: ");
    scanf("%s", &students[i].lname[0]);
    // this also works for %s and char arrays
    // scanf("%s", students[i].lname);
    }
    Then:
    char *lname[20];
    char *fname[10];
    ...
    scanf("%s", &student[i].lname);
    printf("Enter student first name: ");
    scanf("%s", &student[i].fname);

    This is all wrong.
    And on this part:
    You might have "solved" your array overrun by allocating say 80 bytes for lname, rather than 20, but you're abusing the space. You were closer in your first post.

    Use this, but change the 20 to be whatever is appropriate for your data set.
    Code:
    char lname[20];
    scanf("%s", student[i].lname);
    Originally I allocated 10 characters for the first name and 20 characters for the last name, (Which is (10 char * 8 bits = 80 for the first name and 160 bytes for the last name, correct?) And I did that for the last name because when designing Access tables I use a char length of 25 for the last name.

    Regarding the output formatting, you can do this
    Code:
    printf("%20s,%10s\n", student[i].lname, student[i].fname );
    printf("%-20s,%-10s\n", student[i].lname, student[i].fname );
    So, the "20" between the conversion specifier says no matter what give this much space?


    The reason I used the / was when I have written VB an _ is needed to show line continuation. I thought that was needed for C to recognize as well.

    Regarding code formatting
    Code:
      	printf("%s,\t%s\t\t %i\t %i\t %i\t %i\n", student[i].lname, student[i].fname, student[i].tstgrade[0],\
    	   student[i].tstgrade[1], student[i].tstgrade[2], student[i].gpa);
    You don't gain anything from having a \ here.
    There is nothing wrong with say
    Code:
      printf("%s,\t%s\t\t %i\t %i\t %i\t %i\n", 
        student[i].lname, student[i].fname, 
        student[i].tstgrade[0], student[i].tstgrade[1], student[i].tstgrade[2],
        student[i].gpa);
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    The c preprocessor handles backslashes to continue lines. Otherwise, c itself treats new lines as it does white space, merely a token separator.

    If you have a multiline macro definition then you need to use backslash. Don't use it otherwise, it can only muck up your program.



    In your structure the expressions
    &student[i].lname[0]
    and
    student[i].lname
    produce the same address, which is the start of memory space to hold some input from scanf


    student[i].lname /* is an address */
    &student[i].lname /* is an address of an address. Not the same. */
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by b49P23TIvg
    In your structure the expressions
    &student[i].lname[0]
    and
    student[i].lname
    produce the same address, which is the start of memory space to hold some input from scanf


    student[i].lname /* is an address */
    &student[i].lname /* is an address of an address. Not the same. */

    So when I originally declared lname & fname respectively in the structure I incorrectly declared a pointer instead of declaring them as a char variable and then gave an address to the pointer location, not the actual variable, Correct?
  20. #11
  21. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Code:
    /***************** look for my comments with 16 asterisks */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct student {      /***************** this is the structure from your first post */
      char lname[20];     /* it's fine *****************/
      char fname[10];
      int tstgrade[3];
      int gpa;
    };
    
    
    int main() {
    
      int i=0;
      int count=0;
    
      struct student student[100];
    
      FILE *pread;
      FILE *pwrite;
      FILE *Append;
    
      puts("***********************************************************"); /* changed to puts**************** */
      puts("***********************************************************");
      puts("***                                                     ***");
      puts("***               Student G.P.A.                        ***");
      puts("***                                                     ***");
      puts("***********************************************************");
      puts("***********************************************************\n\n");
    
    
      puts("How many grades would you like to enter? ");
    
      scanf("%i", &count);
    
      /* **************** insert here code to verify that count is less than 100 and non-negative */
    
      for (i = 0 ; i < count ; i++) {
        // Enter student information
        char*last,*first;		/* **************** */
        /* struct student*s=student+i;  /////////nevermind. *****************/
        printf("\nEnter student last name: ");
        scanf("%s", student[i].lname);
        printf("Enter student first name: ");
        scanf("%s", student[i].fname);
    
        last = student[i].lname;	/***************** inserted merely as shorthand */
        first = student[i].fname;
    
        printf("Enter first grade for %s, %s: ", last, first);
        scanf("%i", student[i].tstgrade+0); /* effectively the same as, but simpler than  &student[i].tstgrade[0] *****************/
        printf("Enter second grade for %s, %s: ", last, first);
        scanf("%i", student[i].tstgrade+1);
        printf("Enter third grade for %s, %s: ", last, first);
        scanf("%i", student[i].tstgrade+2);
        //count++;/* right, this is wrong *****************/
    
        /* put this computation into the loop *****************/
        student[i].gpa = (student[i].tstgrade[0] + student[i].tstgrade[1] + student[i].tstgrade[2]) / 3;
      }
    
      puts("\n\n\nStudent Name\t\tTest1\tTest2\tTest3\t GPA"); /* changed to puts**************** */
    
    
      /***************** THINK HARD HERE.  WHAT IS i ?  (Hint, it is 1 too big.) */
      do /*****************printing backward here is a nice idea and all */
        /***************** display the names with the same for loop you used when reading the data and go to bed */
        printf("%s,\t%s\t\t %i\t %i\t %i\t %i\n", student[i].lname, student[i].fname, student[i].tstgrade[0], \
    	   student[i].tstgrade[1], student[i].tstgrade[2], student[i].gpa);
    
      while (i = i - 1);		/***************** what if count was zero????? */
      return 0;
    }

    Comments on this post

    • astonecipher agrees : Thanks
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo