Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- struct Elections
- { /* don't skimp on size for name */
- char name[1024];
- char * n1;
- //int votes;
- /* testing purposes */
- char value[1024];
- char *v1;
- };
- char * get_can_name_votes(char *s);
- int chop_string(char * s, struct Elections *e,
- int c);
- /* if pattern is, name, value
- sequential in sets of 2
- [ num x] name == argc 1
- [ num x] value == argc 2
- anything more is discaeded/not gotten
- quit command is a single char len 1
- */
- #define MAX_A 6
- int main(int argc, char *argv[]) {
- struct Elections egor[MAX_A];
- int count=0,c=0,r;
- char run='y';
- char *string;
- for(int i=0;i<argc;i++)
- printf("main top\n"
- "argc %d : %d argv %s\ncount %d",argc,i,argv[i],count);
- c=argc;
- while(run == 'y'){
- /* just the program name */
- switch(c){
- case 1:
- r = chop_string(
- get_can_name_votes(string),
- egor,count);
- switch(r){
- case 0: /*bad input */
- printf("ERROR\n"
- "bad input\n");
- c=1;
- continue;
- break;
- case 1: /* good */
- c=1;
- count++;
- continue;
- break;
- case 2:
- run='n';
- break;
- } // end inner switch
- break;
- case 2: /* program name & 1 of
- something else
- if it's not q || Q
- */
- if((strcmp(argv[1],"q")==160)||
- (strcmp(argv[1],"Q")==-128)){
- run='n';
- }else{
- printf("\nerror %s\n",argv[1]);
- c=1;
- continue;
- }
- break;
- case 3:/* program & 2 other things
- hopfully name & votes */
- printf("OFF THE LINE\n"
- "count= %d\n"
- "%s argv[1], %s argc[2]\n",
- count,argv[1],argv[2]);
- strcpy(egor[count].name,argv[1]);
- egor[count].n1=strdup(argv[1]);
- /*
- needs stoi and error checking
- if integer
- */
- strcpy(egor[count].value,argv[2]);
- egor[count].v1=strdup(argv[2]);
- count++;
- c=1;
- printf("\nOFF THE LINE POST\n"
- "%s \n%s\n%d\n",
- egor[count-1].name,egor[count-1].value,
- count);
- break;
- default:
- break;
- } // end switch
- if(count == MAX_A) run='n';
- } // end while
- printf("PRINT RESULTS\n"
- " end while\ncount = %d\n",count);
- for(int i=0;i<count;i++)
- printf("end of main\n"
- " egor[%d].name %s\n "
- " egor[%d].n1 %s\n "
- "egor[%d].value %s\n"
- "egor[%d].v1 %s\n",
- i,egor[i].name,
- i,egor[i].n1,
- i,egor[i].value,
- i,egor[i].v1);
- /*
- int k;
- for(k=count;k>=0;k--){
- printf("\n\nFREE ME\n"
- "%d %d\n",count,k);
- EVEN I QUESTION
- THIS.
- DOES THAT RELEASE
- EVERYTHING STRDUPPED?
- */
- free(egor[0].n1);
- free(egor[0].v1);
- //}
- return 0;
- }
- char * get_can_name_votes(char *s)
- {
- s = malloc(255);
- puts("Type name and votes \n"
- "or q to quit\n");
- fgets(s, 255, stdin);
- return s;
- }
- int chop_string(char * s, struct Elections *e,int c)
- {
- int i=0;
- char *temp;
- char *tok = strtok_r(s," ",&temp);
- printf("functon s=%s\n"
- "strlen s %lu\n"
- " tok = %s\n"
- "strcmp s q %d\n",s,
- strlen(s),tok,strcmp(s,"q"));
- printf("c count= %d\n",c);
- if((strlen(s)==2) && (strcmp(s,"q") == 160) ||
- (strcmp(s,"Q") == -128)){
- free(s);
- return 2;
- }
- /* we're only getting 2 at a time
- 2nd needs stoi and checking
- if number or not */
- printf("chop\nbefore for loop\n"
- "c=%d\n",c);
- /* neeed 2 if only 1 report error */
- for(i=0;i<1;i++){
- printf("INSIDE for i=%d tok\ns=%s\n",i,s);
- e[c].n1=strdup(tok);
- strcpy(e[c].name,tok);
- printf("t %d tok = %s\n",i,tok);
- tok=strtok_r(NULL," ",&temp);
- printf("b %d tok = %s\n\n",i,tok);
- if(tok == NULL)
- {
- free(s);
- return 0;
- }
- e[c].v1=strdup(tok);
- strcpy(e[c].value,tok);
- i++;
- } // end loop
- free(s);
- printf("\nchop\nreturn true\n");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement