Thread: String troubles

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    36

    String troubles

    Hi there i am stuck on a programming question. The question below entails what i need to do. I have a very good idea of what i want to do to solve it, but i am stuck at the beginning. I have my data file that i have created with with the answer key and sample in it.

    My problem is that i want to be able to read the strings from the file and place them in variables. If this is the right idea to start this question. Now when i place them in variables using fscanf as told to use it will not place the whole string into the variable. As seen below in this setup.
    Code:
    char test[20];
    char studentId[8];
    /*Loop to read the file completely*/
    fscanf(grading,"%s %s", &studentId, &test);
    /*text within the data file */
    ABC54102 T FTFTFTTTFTTFTTF TF
    what will happen is the ABC54102 will be stored into studentID as planned. Then the test part will only store the 'T' in the variable test and nothing else. Then when it scans the file again it will place the "FTFTFTTTFTTFTTF" into studentID. The for test it will store the remaining "TF". It will go about this until the eof. How do i stop this from happening and what should i do if you could point me in the right direction.








    The instructor at your school needs help in grading a True/False test. The students’ IDs and test answers are stored in a file. The first entry in the file contains answers to the test in the form;

    TFFTFFTTTTFFTFTFTFTT

    Every other entry in the file is the student ID, followed by a blank, followed by the student’s responses. For example, the entry
    ABC54301 TFTFTFTT TFTFTFFTTFT
    Indicates that the student ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is false, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more then 150 students. Each correct answer is awarded 2 points, each wrong answer gets -1 points and no answer gets 0 points. Write a program that processes the test data. The output should be the student’s ID followed by the answers, followed by the test score, followed by the test grade. Assume the following grade scale: 90%-100%, A; 80%-89.99%,B; 70%-79.99%,C; 60%-69.99%,D; and 0%-59.99%, F.
    A sample test data file is:

    TTFTFTTTFTFTFFTTFTTF
    ABC54102 T FTFTFTTTFTTFTTF TF
    DEF56278 TTFTFTTTFTFTFFTTFTTF
    ABC42366 TTFTFTTTFTFTFFTTF
    ABC42586 TTTTFTTT TFTFFFTF

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    From what you're describing it's just a matter that your formatting string for fscanf() isn't right. You may need to read the whole line into a single string and then split it in two on the first space.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    Okay thats an interesting thought. would you be kind enough to show me a line of code that would demontrate how to do this?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Annihilate View Post
    Okay thats an interesting thought. would you be kind enough to show me a line of code that would demontrate how to do this?
    Assuming you have the input as a single string, you can split the string using strchr() to get a pointer to the first space. Then replace the space with a null and increment the pointer, now you have two strings...

    Code:
    char inbuf[256];  // raw input data
    char *str1, *str2;
    
    // acquire data here
    
    // split buffer
    str1 = &inbuf[0];
    str2 = strchr(inbuf,' ') ;
    *str2 = 0;
    str2++;
    
    // copy strings out of the buffer
    This may not compile perfectly, I haven't tested it... but I think you'll get the idea.

    Another alternative is strtok() which is generally used for bursting command line tokens, but can be used on strings easily as not.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    Okay so i got another idea after i was told that i would not be aloud to use strtok() or strchr().

    Heres my program, but i am still not certain if i am doing this right. I also have my results printing for the sake of my visual part of learning.

    nt main()
    {
    int i;
    int request;
    char id;
    char answer[21];
    char test[21];
    char studentId[9];
    int score;
    score= 0;



    Code:
    	FILE *grading;
          
       
          grading = fopen("results.dat", "w");
          if (grading == NULL) 
    	  {
             printf("I couldn't open results.dat for writing.\n");
             exit(0);
          }
    			/* prints the students test results to the data file*/
    		 
    		 fprintf(grading, "%s\n", "TTFTFTTTFTFTFFTTFTTF");
    		 fprintf(grading, "%s %s\n", "ABC54102", "T FTFTFTTTFTTFTTF TF");
    		 fprintf(grading, "%s %s\n", "DEF56278", "TTFTFTTTFTFTFFTTFTTF");
    		 fprintf(grading, "%s %s\n", "ABC42366", "TTFTFTTTFTFTFFTTF   ");
    		 fprintf(grading, "%s %s\n", "ABC42586", "TTTTFTTT TFTFFFTF   ");
    	
    		 
    		 fclose(grading);
    
    		 grading = fopen("results.dat", "r");
    		 if (grading == NULL) 
    	  {
             printf("I couldn't open results.dat for writing.\n");
             exit(0);
          }
    		fgets(answer, 21,grading);
    		printf("%s\n", answer);
    		{
    		fseek(grading, 2, SEEK_CUR);
    		while(!feof(grading))
    		{
    			fscanf(grading,"%s", &studentId);
    			fgets(test, 21, grading);
    			printf("%s ", studentId);
    			printf("%s", test);
    			
    		}
    		
    		}
    If you test it you will see that i am getting a somewhat normal print except towards the end for what ever reason. Any suggestions.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take a look at the way you are reading the file...

    Your student IDs are all the same length... you could use fgets(test,len,studentID); where len is the length of the id code. This would leave your file pointer at the space before the test results. From there you can use fscanf() to read the remainder of the line.

    Keep track of the file pointer... then it will make more sense to you.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    That just worked wonders thank you.
    Edit nevermind i was fooled. When i use fscanf on the string test it still wont read the spaces. Upon reaching a space in the scanf it stops at that point and declares everything before the space the string test.
    Last edited by Annihilate; 11-22-2010 at 02:02 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Annihilate View Post
    That just worked wonders thank you
    No worries... I was glad to help.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    Here are the changes i made.

    Code:
    while(!feof(grading))
    		{
    			fgets(studentId, 9 ,grading);
    			fscanf(grading, "%s", test);
    			printf("%s", studentId);
    			printf("%s", test);
    			
    			for(int i=0; i<21;i++)
    			{
    				if(test[i]=='T'&&answer[i]=='T')
    				{
    					score=score+1;
    				}
    				else if(test[i]==' '&&answer[i]==' ')
    				{
    					score=score+0;
    				}
    				else if(test[i]=='T'&&answer[i]=='F')
    				{
    					score=score-2;
    				}
    The fscanf looks like it works properly at first but is still behaving poorly like it did originally. Skipping all the spaces and including everything before the space as a string and thats it.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please don't use feof to control your file reading loop. Read up on why it's bad and how to fix your code here: Cprogramming.com FAQ > Why it's bad to use feof() to control a loop.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    haha yeah i am aware of hows it bad. This is twice that you have sent me this message. My teacher has taught me this way and insists that i program the way they have taught me. So for bad programming measure i will have to use this for assignments as much as that may hurt you and myself.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Duly noted Annihilate, I will try to avoid repeating myself from here on out. You might want to show that link to your teacher though, so they can stop propagating bad advice. Just phrase it a little more diplomatically.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    haha sounds good any chance you would be able to tell me where i am going wrong with my code

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <time.h>
    #include <string.h>
    
    int main()
    {
    	int score;
    	char answer[21];
    	char test[21];
    	char studentId[9];
    	
    	
    	
    	FILE *grading;
          
       
          grading = fopen("results.dat", "w");
          if (grading == NULL) 
    	  {
             printf("I couldn't open results.dat for writing.\n");
             exit(0);
          }
    			/* prints the students test results to the data file*/
    		 
    		 fprintf(grading, "%s\n", "TTFTFTTTFTFTFFTTFTTF");
    		 fprintf(grading, "%s %s\n", "ABC54102", "T FTFTFTTTFTTFTTF TF");
    		 fprintf(grading, "%s %s\n", "DEF56278", "TTFTFTTTFTFTFFTTFTTF");
    		 fprintf(grading, "%s %s\n", "ABC42366", "TTFTFTTTFTFTFFTTF   ");
    		 fprintf(grading, "%s %s\n", "ABC42586", "TTTTFTTT TFTFFFTF   ");
    	
    		 
    		 fclose(grading);
    
    		 grading = fopen("results.dat", "r");
    		 if (grading == NULL) 
    	  {
             printf("I couldn't open results.dat for writing.\n");
             exit(0);
          }
    		fscanf(grading,"%s", &answer);
    		printf("%s\n", answer);
    		{
    		fseek(grading, 2, SEEK_CUR);
    		while(!feof(grading))
    		{
    			
    			
    			fgets(studentId, 9,  grading);
    			fscanf(grading,"%[^\n]", &test);
    			printf("%s", studentId);
    			printf("%s", test);
    			
    			for(int i=0; i<21;i++)
    			{	
    				score= 0;
    				if(test[i]=='T'&&answer[i]=='T')
    				{
    					score=score+1;
    					
    				}
    				else if(test[i]==' '&&answer[i]==' ')
    				{
    					score=score+0;
    					
    				}
    				else if(test[i]=='T'&&answer[i]=='F')
    				{
    					score=score-2;
    					
    				}
    				
    				
    			}
    			printf("\tScore:%d", score);
    			
    			
    			
    			
    				
    
    				 
    		}
    		
    		}
    		fclose(grading);
    		
    		
    		
    		return 0;
    	}

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Want to give me a hint as to what's wrong?

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Looking pretty good...
    Now, take a look at how you're testing your scores... would it not be simpler to just test for blanks, then test for equality... If they match add a point, if they don't, subtract... C can't do == with strings, but it can do it with char.

    Think about if-else structures...

    Hint: once you get the idea how to do something, make your next step that of figuring out the simplest way to do it. It usually pays off.

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