Thread: Help Needed

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Question Help Needed

    Hi folks, this has got me stumped.
    input: (from a file called ch7d) Output:
    1 0 student ssessment attempt report
    1 0
    1 1 group 1
    1 1 none 2
    1 2 one 2
    5 2 both 1
    5 2 total 5
    5 1
    4 2 group 5
    4 2 both 2
    4 0 one 1
    4 1 total 3
    2 2
    99 group 4
    both 2
    none 1
    one 1
    total 4

    group 2
    both 1
    total 1

    basically the figure on the left of the two relates to a student group number, the figure
    on the right is the number of exams each student sat. The spec for the code is:

    open and read the files
    WHILE not at the end of the data(99)
    group total = 0
    store group as current group
    output group heading
    WHILE still processing current group and not at the end of the data
    store attempts as current attempts
    attempt toal = 0
    WHILE still processing current attempts and still processing current group and not
    at the end of the data
    increment attempt total
    read the next line.
    IF current attempt is 2
    output "both"
    ELSE IF current attempt is 1
    output "one"
    ELSE
    output "none"
    output attempt total
    accumulate group total
    output group total

    My code so far is:

    #include<stdio.h>
    #include<conio.h>

    main()
    {

    int group,
    attempt,
    group_total,
    current_attempt,
    current_group,
    attempt_total;

    FILE *ch7d = fopen("ch7d.dat", "r");
    fscanf(ch7d, "%d %d", &group, &attempt);
    printf("Student Assessment Attempt Report\n");

    while (group <= 99)
    {
    {
    group_total = 0;
    current_group = group;
    printf("Group %d\n", current_group);
    }
    while (group = current_group && group <= 99)
    {
    current_attempt = attempt;
    attempt_total = 0;
    }
    /*stuck from here*/
    while ((attempt = current_attempt) && (group = current_group) && (group <= 99))
    {
    attempt_total += 1;
    fscanf(ch7d, "\n%d %d", &group, &attempt);
    }
    if (current_attempt == 2)
    {
    printf("\nBoth ");
    }
    if else (current_attempt == 1)
    {
    printf("\nOne ");
    }
    else
    {
    printf("\nNone ");
    }
    printf("%d", attempt_total)
    group_total???????


    fclose(ch7d);
    getch();
    }

    The program seems to get stuck on the 3rd while condition. The 2nd while appears fine as
    I stuck a printf in there.

    Anyone any ideas?? PLEASE!?!?!


  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Remember the = sign means assignment and == means equality.

    //Change to == here like this
    while (group == current_group && group <= 99)
    {
    current_attempt = attempt;
    attempt_total = 0;
    }
    /*stuck from here*/
    //Same change needed here
    while ((attempt == current_attempt) && (group = current_group) && (group <= 99))
    {
    attempt_total += 1;
    fscanf(ch7d, "\n%d %d", &group, &attempt);
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I missed one on this line:

    while ((attempt == current_attempt) && (group = current_group) && (group <= 99))

    You also need to change all
    group <= 99
    to
    group < 99

    Here is an improved version.
    Code:
    #include<stdio.h>
    #include<conio.h> 
    
    int main() 
    { 
    
    int group, 
    attempt, 
    group_total, 
    current_attempt, 
    current_group, 
    attempt_total; 
    
    FILE *ch7d = fopen("ch7d.dat", "r");
    if (ch7d == NULL)
    {
       printf("Unable to open file.\n");
       return 0;
    } 
    fscanf(ch7d, "%d %d", &group, &attempt); 
    printf("Student Assessment Attempt Report\n"); 
    
    while (group < 99)
    { 
       group_total = 0;
       current_group = group; 
       printf("\nGroup %d\n", current_group);
       while (group == current_group && group < 99)
       { 
          current_attempt = attempt; 
          attempt_total = 0; 
          /*stuck from here*/
          while ((attempt == current_attempt) && (group == current_group) && (group < 99)) 
          { 
             attempt_total += 1; 
             fscanf(ch7d, "\n%d %d", &group, &attempt);
             //getch();
          }
          if (current_attempt == 2)
          { 
             printf("Both "); 
          } 
          else if(current_attempt == 1)
          { 
             printf("One "); 
          } 
          else 
          { 
             printf("None "); 
          }
          printf("%d\n", attempt_total);
          group_total += attempt_total;
       }
       printf("Group total:%d\n", group_total);
    }
    
    fclose(ch7d); 
    getch();
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. Releasing a program - what is needed? A few Q's
    By ulillillia in forum Tech Board
    Replies: 9
    Last Post: 04-28-2007, 12:18 AM
  5. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM