Initialization
This commit is contained in:
		
							parent
							
								
									00c0b044b6
								
							
						
					
					
						commit
						b48b1f925d
					
				| @ -1,7 +1,6 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include <stdbool.h> | ||||
| 
 | ||||
| #define SIZE 100 | ||||
| 
 | ||||
| @ -35,7 +34,7 @@ int main() { | ||||
|         memset(line, 0, SIZE); | ||||
|         char *r = fgets(line, SIZE, stdin); | ||||
| 
 | ||||
|         // check if the line is empty or just contains newline character
 | ||||
|         // check if the line is empty
 | ||||
|         if (r == NULL || (r[0] == '\n' && strlen(r) == 1)) { | ||||
|             // end of input
 | ||||
|             break; | ||||
| @ -44,22 +43,20 @@ int main() { | ||||
|         // parse the string and extract number of votes and student name
 | ||||
|         int votes; | ||||
|         char name[SIZE]; | ||||
| 
 | ||||
|         // check if sscanf successfully parsed the input
 | ||||
|         if (sscanf(line, "%d %[^\n]", &votes, name) != 2 || votes < 0) { | ||||
|             // error, input was not in expected format or votes are negative
 | ||||
|             // print error message and continue to next line
 | ||||
|             fprintf(stderr, "Nepodarilo nacitat nic\n"); | ||||
|             continue; | ||||
|         if (sscanf(line, "%d %[^\n]", &votes, name) != 2) { | ||||
|             // error, input was not in expected format
 | ||||
|             // print error message and exit
 | ||||
|             fprintf(stderr, "Nepodarilo␣nacitat␣nic\n"); | ||||
|             return 1; | ||||
|         } | ||||
| 
 | ||||
|         // check if this student already exists
 | ||||
|         bool found = false; | ||||
|         int found = 0; | ||||
|         for (int i = 0; i < size; i++) { | ||||
|             if (strcmp(students[i].name, name) == 0) { | ||||
|                 // add votes to existing student
 | ||||
|                 students[i].votes += votes; | ||||
|                 found = true; | ||||
|                 found = 1; | ||||
|                 break; | ||||
|             } | ||||
|         } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user