Thread: Multidimensional String

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    93

    Multidimensional String

    How the hell would you, read in the first, middle, and last name of 150 people, keeping in mind that each name should be limited to 30 characters.

    I have tried using %ns in a scanf, but it just keeps going anyway.

    As of right now I am trying to figure out a way to do it in a multidimensional array, looking like
    Code:
    char names[150][3][30]
    I am also currently using c = getchar(), and trying to figure out a way.

    I will post my code, but it's of very little help since it just consists of about 3 practice programs that I am doing to try and understand what the hell to do. Had about 5 of them going, but just deleted a couple once I understood a bit more.

    Any help would be greatly appreciated. I would like to specifically use either getchar() or scanf, because it can be done that way. Instead of just jumping to a more useful function, I'd like to learn how to understand these two first.

    Thanks again.

    Here's the ridiculous code:

    Code:
    /*#include <stdio.h>
    
    int main ()
    
    {
    	
    	char names[5][5];
    	int i,j;
    
    	printf("Enter 3 names:\n");
    
    	for(i = 0; i < 3; i++)
    		for(j = 0; j < 5; j++)
    			names[i][j] = '#';
    
    	for(i=0;i<3;i++)
    		scanf("%4s",names[i]);
    
    	
    
    	for(i = 0; i < 3; i++)
    		for(j = 0; j < 5; j++){
    			printf("names[%d][%d]: %c ", i, j, names[i][j]);
    		if ( (j + 1) % 4 == 0)
    			printf("\n\n");
    		}
    		for(i=0;i<3;i++)
    			printf("%s\n",names[i]);
    
    
    	return 0;
    
    }
    
    
    */
    
    /*
    	char name[150][3][30];
    
    	printf("Enter your first name, middle name, and last name.\n");
    
    	for (i = 0; i < 1; i++)
    		for(j = 0; 
    
    
    
    	scanf("%5s", name, name, name);
    
    	printf("%s\n", name[0]);
    
    	return 0;
    
    }
    
      */
    /*
    #include <stdio.h>
    
    int main ()
    
    {
    	
    	char names[1][3][5];
    	int i,j,k;
    
    	printf("Enter your name:\n");
    
    	for(i = 0; i < 3; i++)
    		for(j = 0; j < 3; j++)
    			for (k = 0; k < 5; k++)
    			names[i][j][k] = '#';
    
    	for(i=0;i<1;i++)
    		scanf("%s",names);
    
    	
    
    	for(i = 0; i < 1; i++)
    		for(j = 0; j < 3; j++)
    		for(k = 0; k < 5; k++) 
    		{
    			printf("names[%d][%d][%d]: %c ", i, j, k, names[i][j][k]);
    		if ( (k + 1) % 4 == 0)
    			printf("\n\n");
    		}
    		for(i=0;i<1;i++)
    			printf("\n%s\n",names[i]);
    
    		
    	return 0;
    
    }
    
    */
    
    #include <stdio.h>
    
    int main ()
    
    {
    
    	char names[2][3][30];//, c;
    	int i, j, k,c;
    
    		printf("Enter 2 full names\n");
    
    		i = 0;
    		j = 0;
    		k = 0;
    		
    		do {
    			
    			c = getchar();
    
    			if ( k == 29)
    				names[i][j][k] = '\0';
    
    			names[i][j][k++] = c;
    			
    			if ((j != 0) && (j % 2 == 0)){
    				i++;
    				j = 0;
    			}
    				
    			
    			//j++;
    
    			printf("names [%d][%d][%d]: %c \n", i, j, k, names[i][j][k]);
    		}
    		
    		while(c != '\n' && c != ' ');
    			names[i][j][k++] = c;
    		return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Another thing, in my last program,

    Code:
    #include <stdio.h>
    
    int main ()
    
    {
    
    	char names[2][3][30];//, c;
    	int i, j, k,c;
    
    		printf("Enter 2 full names\n");
    
    		i = 0;
    		j = 0;
    		k = 0;
    		
    		do {
    			
    			c = getchar();
    
    			if ( k == 29)
    				names[i][j][k] = '\0';
    
    			names[i][j][k++] = c;
    			
    			if ((j != 0) && (j % 2 == 0)){
    				i++;
    				j = 0;
    			}
    				
    			
    			//j++;
    
    			printf("names [%d][%d][%d]: %c \n", i, j, k, names[i][j][k]);
    		}
    		
    		while(c != '\n' && c != ' ');
    			names[i][j][k++] = c;
    		return 0;
    
    }
    Although far from done, it gives me letters back when I use ++k in this line
    Code:
    names[i][j][k++] = c;
    in the do while loop.

    However when it has k++, as in that code, it just gives me a squiggly bracket thing.

    Any ideas?

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Hah, I apologize if my question is poorly worded...just typing out whatever comes to mind.

    I have decided to split this into 1-D arrays first, and at least get that to work.

    Any ideas, pointers (even pointers in C), or tips are greatly appreciated.

    Thanks.

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    #include <stdio.h>
    
    char First[30];
    char Middle[30];
    char Last[30];
    char FullName[120];
    
    int main()
    {
      int loop;
    
      for(loop = 0; loop < 150; loop++)
      {
        printf("Enter your name: ");
        scanf("%s %s %s", First, Middle, Last);
    
        sprintf(FullName, "%s %s %s", First, Middle, Last);
    
        printf("\n\nName %d: %s\n\n", loop + 1, FullName);
      }
    
      return 0;
    }
    Is that what your wanting to do? or store them in a file?

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Thanks for your response man.

    I do want to store them in an array, or else I really can't do anything with them.

    I have a potential max of 150 people, and a maximum of 30 characters for each name (first, middle, last).

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    This code still has a couple of bugs, and is not done, but you can see what I did for the strings.

    I wanted to do this though in a 3-D array, but have no idea how to do so.

    It's in bold.

    Code:
    
    #include <stdio.h>
    #include <ctype.h>
    
    #define MAX_SCORE  100
    #define MIN_SCORE  0
    #define TOTAL_ASSIGNMENT_MAX_SCORE 300
    #define MAX_STUDENTS 150
    #define MAX_LETTERS 30
    
    void menu (void);
    float take_score (char first_names[][MAX_LETTERS], 
    				  char middle_names[][MAX_LETTERS], 
    				  char last_names[][MAX_LETTERS], 
    				  int i);
    float final_score_calc (float student_grades[][3], int x);
    char letter_grade (float x, int y);
    float G_P_A (char grades[], int x, int y);
    void info_display (void);
    void print_scores (char first_names[][MAX_LETTERS], 
    				   char middle_names[][MAX_LETTERS], 
    				   char last_names[][MAX_LETTERS], 
    				   float x, char y, float z, int i);
    float high_score_calc (float x, float y);
    float low_score_calc (int x, float y, float z);
    
    int main () {
    
    	
    
    
    
    menu();
    return 0;
    
    }
    
    void menu (void) {
    
    	char c;
    	float final_score, total_final_score = 0, average_final_score = 0, high_score = 0, low_score = 0 ;
    	float A_Percentage = 0, B_Percentage = 0, C_Percentage = 0, D_Percentage = 0, F_Percentage= 0;
    	int student_counter = 0, ACounter = 0, BCounter = 0, CCounter = 0, DCounter = 0, FCounter = 0, 
    		total_Grade_Count = 0;
    	int i = -1; // Start at -1 so it does not equal 1 and the array later skips the 0 element
    	char grades[MAX_STUDENTS];
    	char first_names    [MAX_STUDENTS][MAX_LETTERS];
    	char middle_names   [MAX_STUDENTS][MAX_LETTERS];
    	char last_names     [MAX_STUDENTS][MAX_LETTERS];
    	float GPA;
    
    	
    
    	do{
    
    
    		printf("A/a  - Add a student's record\n");
    		printf("I/i - load students record from an input file\n");
    		printf("O/o - Save students record to an output file\n");
    		printf("L/l - display a list of students score\n");
    		printf("D/d - Display current statistical results\n");
    		printf("Q/q - Quit\n");
    		printf("\nPlease enter a key for one of the above options:");
    
    		c=getchar();
    		c=toupper(c);	
    		
    		switch (c) {		
    
    			
            
    		case 'A': 
    				  
    				  i++;  /*This is done so that when take_score(i) is called, the array in the function 
    						  knows to start on the next element, and it doesn't erase old elements.*/
    						         
    						 
    				  final_score = take_score(first_names, middle_names, last_names, i);
    
    				  grades[i] = letter_grade(final_score, i);
    				  GPA = G_P_A(grades, MAX_STUDENTS, i);
    
    				  //printf("%f %c %f\n\n", final_score, grades[i], GPA);
    				  print_scores(first_names, middle_names, last_names, final_score, (grades[i]), GPA, i);
    
    				  student_counter ++;
    
    				  switch (grades[i]) {
    
    				  case 'A': ACounter++;
    								break;
    
    				  case 'B': BCounter++;
    								break;
    
    				  case 'C': CCounter++;
    								break;
    
    				  case 'D': DCounter++;
    								break;
    
    				  case 'F': FCounter++;
    								break;
    
    				  }
    
    				  total_final_score += final_score;
    
    				  average_final_score = (total_final_score / student_counter);
    
    				  high_score = high_score_calc(final_score, high_score);
    
    				  low_score = low_score_calc(student_counter, low_score, final_score);
    				
    				  total_Grade_Count = ACounter + BCounter + CCounter + DCounter + FCounter;
    
    				  A_Percentage = (( float ) ACounter / total_Grade_Count) * 100;
    				  B_Percentage = (( float ) BCounter / total_Grade_Count) * 100;
    				  C_Percentage = (( float ) CCounter / total_Grade_Count) * 100;	
    				  D_Percentage = (( float ) DCounter / total_Grade_Count) * 100;
    				  F_Percentage = (( float ) FCounter / total_Grade_Count) * 100;
    
    				  break;
    
    		case 'D':  
    
    				   printf("\nStatistical result of your class:\n\n");
    				   printf("\n\tTotal Students Number:\t%d", student_counter);
    				   printf("\n\tAverage score:\t%.2f",average_final_score);
    				   printf("\n\tHighest score:\t%.2f",high_score);
    				   printf("\n\tLowest score:\t%.2f",low_score);
    				   printf("\n\tTotal A grade: %d, percentage   %.2f%%", ACounter, A_Percentage);
    				   printf("\n\tTotal B grade: %d, percentage   %.2f%%", BCounter, B_Percentage);
    				   printf("\n\tTotal C grade: %d, percentage   %.2f%%", CCounter, C_Percentage);
    			       printf("\n\tTotal D grade: %d, percentage   %.2f%%", DCounter, D_Percentage);
    				   printf("\n\tTotal F grade: %d, percentage   %.2f%%\n\n", FCounter, F_Percentage);
    				  
    				   break;
    
    		}
    
    		getchar ();
    
    	}while (c != 'Q' );
    	
    		printf("\nBye!\n\n");
    
    }	
    
    float final_score_calc (float student_grades[MAX_STUDENTS][3], int x)
    
    {
    
    	 float final_score;
    
    	 final_score = (student_grades[x][0]) / 3.0 * 0.3 + (student_grades[x][1]) * 0.3 + (student_grades[x][2]) * 0.4;
    
    	 return final_score;
    
     }
    
    float take_score (char first_names[][MAX_LETTERS], 
    				  char middle_names[][MAX_LETTERS], 
    				  char last_names[][MAX_LETTERS], 
    				  int i) {
    
    
    
    	float final_score;
    
    	float student_grades[MAX_STUDENTS][3];
    	char c;
    
    	int j = 0, k= 0;
    
    			printf("\nEnter a student's name (first middle last), total assignment, midterm and final exam scores:");
    						
    			scanf("%s %s %s", first_names[i], middle_names[i], last_names[i]);
    
    			scanf("%f %f %f", &student_grades[i][j], &student_grades[i][j+1], &student_grades[i][j+2]);
    
      while (student_grades[i][j] > TOTAL_ASSIGNMENT_MAX_SCORE || student_grades[i][j] < MIN_SCORE)
    
      {
       printf("\nPlease re-enter total assignment score (0~300) :");
       scanf("%f", &student_grades[i][j]);
      }
    
    
      while (student_grades[i][j+1] > MAX_SCORE || student_grades[i][j+1] < MIN_SCORE)
    
      {
       printf("\nPlease re-enter midterm score (0~100) :");
       scanf("%f", &student_grades[i][j + 1]);
      }
    
      while (student_grades[i][j+2] > MAX_SCORE || student_grades[i][j+2] <MIN_SCORE)
    
      {
       printf("\nPlease re-enter final exam score (0~100) :");
       scanf("%f", &student_grades[i][j+2]);
      }
     
      final_score = final_score_calc (student_grades, i);
    
      return final_score;
    
     }
    
    char letter_grade (float x, int i) {
    
    	float final_score = x;
    
    	float final_scores[MAX_STUDENTS];
    	
    	char grade;
    	
    	final_scores[i] = final_score;
    
    	
    
    	if (final_scores[i] >= 90)
    		grade='A';
    
    	else if (final_scores[i] < 90 && final_scores[i] >= 80)
    		grade='B';
    
    	else if (final_scores[i] < 80 && final_scores[i] >= 70)
    		grade='C';
    
    	else if (final_scores[i] < 70 && final_scores[i] >= 60)
    		grade='D';
    
    	else if (final_scores[i] < 60)
    		grade='F';
    
    	return grade;
    
    }
    
    float G_P_A (char grades[], int x, int y) {
    	
    	float GPAs[MAX_STUDENTS];
    
    	int i = y;
    
    	switch ( grades[i]) {
     case 'A': GPAs[i] = 4; 
          break;
    
     case 'B': GPAs[i] = 3; 
          break;
    
     case 'C': GPAs[i] = 2; 
          break;
    
     case 'D': GPAs[i] = 1; 
          break;
    
     case 'F': GPAs[i] = 0; 
          break;
    
     }
    
    	return GPAs[i];
    
    }
    
    void print_scores (char first_names[][MAX_LETTERS], 
    				   char middle_names[][MAX_LETTERS], 
    				   char last_names[][MAX_LETTERS], 
    				   float x, char y, float z, int i) {
    
    	printf("\n%s %s %s's final_score:   %.2f  letter grade:%c  GPA:%d\n\n", first_names[i], middle_names[i],
    																		  last_names[i], x, y, z);
    
    }
    
    float high_score_calc (float x, float y) {
    
    	if (x > y)
    		y = x;
    
    	return y;
    
    }
    
    
    float low_score_calc (int x, float y, float z) {
    
    /*	x = student_counter
    	y = low_score
    	z = final_score */
    
    	switch (x) {
    
    				  case 1: 
    					  
    					  y = z;
    						   break;
    
    				  default: 
    					   
    					  if (y > z)
    							   y = z;
    							   break;
    				  }
    
    	return y;
    
    }

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    may I suggest using a struct?

    Code:
    #include <stdio.h>
    
    struct Data
    {
      char First[30];
      char Middle[30];
      char Last[30];
    
      int scores;
    };
    
    struct Data Student[150];
    
    int main()
    {
      int loop;
    
      for(loop = 0; loop < 150; loop++)
      {
        printf("Enter your name: ");
        scanf("%s %s %s", Student[loop].First, Student[loop].Middle, Student[loop].Last);
    
    
        printf("\n\nName %d: %s %s %s\n\n", loop + 1, Student[loop].First, Student[loop].Middle, Student[loop].Last);
      }
    
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    You sure can my friend, but I haven't learned them yet lol.

    That's next chapter .

    Man, I took a CS programming course at my university once, I went to about 3 classes. I ended up with an A+ because I cheated like a madman, but I learned absolutely nothing.

    I remember walking in one day, they were doing structs, something about pops and snaps or whatnot, and I just walked out, haha.

    Now, I'm just trying to learn it out of my own accord. Amazing how things change eh?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Beast()
    Man, I took a CS programming course at my university once, I went to about 3 classes. I ended up with an A+ because I cheated like a madman, but I learned absolutely nothing.
    Thanks for that bit of info. I'll be sure and not answer your questions from now on.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I think I'll bookmark this post and quote it to all the "please do my homework" posters
    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.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Ahh..interesting.

    So when a man sees the error in his ways, sees that the path he is walking on is crooked and will not go anywhere, sees that he is wasting his time and the time of those around him, he's still at fault?

    When he is actually honest about the error in his past ways, and decides to take the bull by the horns and go forward by himself, he's still a cheat?

    I'm not trying to instill some bull.......... guilt in you guys, but you guys seem to have misunderstood me.

    In the past I cheated IN the class because I was a lazy bastard. In fact I cheated my way out of a lot of things, reading CliffNotes, saving answers in my calculator, little cheat sheets on tests, anything you could imagine I have probably done it.

    It's not that I was too dumb to do well on my own merit, but just too lazy...or thought it was useless.

    Now after some soul-searching, I realize knowledge is key to life...you can learn something everywhere you look. We may not always know where we're going but we can be sure that some extra knowledge along the way can only aid our journey.

    Instead of quitting my major, in which C programming is the building block of it all, I have decided to stay here and learn. Another class in which I had problems with was Calculus, ending up with a C-. Instead of saying ".......... this I will never need it again," I plan on taking all 5 levels.

    You can trust that I am not here to cheat my way out of anything. You can trust that I am here not only as an ardent learner, but hope to one day give back to those in my same situation. Hell, my scholarship won't even pay for summer classes. Class is still in session but I'm the teacher and this site the occasional tutor.

    Sorry for the book, but please don't take me for someone that I am not.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Beast()
    Ahh..interesting.

    So when a man sees the error in his ways, sees that the path he is walking on is crooked and will not go anywhere, sees that he is wasting his time and the time of those around him, he's still at fault?
    Stop trying to play the martyr. You've "see[n] the error in [your] ways"? Really? Here, let me quote you:
    Quote Originally Posted by Beast()
    I remember walking in one day, they were doing structs, something about pops and snaps or whatnot, and I just walked out, haha.
    Yeah, you seem real sincere.

    Quote Originally Posted by Beast()
    Sorry for the book, but please don't take me for someone that I am not.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    .
    Join Date
    Nov 2003
    Posts
    307
    Beastie - most of the people who CAN answer questions stayed in class - that's why they are able to answer the questions.

    Remember the Ant and the Grasshopper Fable, grasshopper?

  14. #14
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by quzah
    Stop trying to play the martyr. You've "see[n] the error in [your] ways"? Really? Here, let me quote you:

    Yeah, you seem real sincere.




    Quzah.
    I said I wasn't trying to make you feel guilty or play the role of a martyr, I was explaining to you my situation.

    So in order to refute my last post, you quote a post that I made before it? What type of sense does that make?

    I know I'm damn sincere, and quoting a post before that won't change anything.

    There's nothing wrong with making light of the situation. I find it funny that I had everything I wanted to learn right in front of me and threw it away...and now I want to learn it and I have to search for it.

    If you choose to no longer help me man, that's fine. I appreciate the help you gave me beforehand.

    But trust me when I tell you that I am here to learn.

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by jim mcnamara
    Beastie - most of the people who CAN answer questions stayed in class - that's why they are able to answer the questions.

    Remember the Ant and the Grasshopper Fable, grasshopper?
    Lol this is true.

    But I'm not going to sit here and cry/regret the past...I'm going to do something about it.

    I wrote the Grasshopper fable man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM