• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Yet another C question...sigh!

Thank you soo much to everyone who helped me on my previous question! Now I am able to complile the program, but unfortunetly it doesn't quite work! Each time I enter a valid or invalid grade, nothing happens, and each time I try to enter the sentinel I get thousands of X's across my screen! Not quite what I had in mind! Here is my program again, with a few changes I made in attempt to rectify this problem.  Thanks!

/* Sara Bissonnette */



#include <stdio.h>

#define MAXGRADE 100

#define SENTINEL 999



void instructions(void);
int getgrades(void);
void graph(int, int, int, int, int, int);


int main(void)

        {
      int A, B, C, D, F, Invalid;
      
        instructions();
        getgrades();
        graph(A, B, C, D, F, Invalid);
        }

void instructions(void)

        {
        printf("\n\n\nThis program will form a bar chart from a ");
        printf("sequence of grades.\n\n");
        }

int getgrades(void)

        {
      int A, B, C, D, F, Invalid, grade;

      A=0; B=0; C=0; D=0; F=0; Invalid=0;

        printf("Please enter a grade between 0 and %d, rounding ", MAXGRADE);
        printf(" to the nearest integer.\nEnter %d when done:", SENTINEL);
      scanf("%d", &grade);


      while (grade!=SENTINEL)
            {
            while((scanf("%d", grade)!=1)||(grade<0)||(grade>100))
                  {
                  Invalid ++;
                  printf("That is not a valid grade.\n\n\n\n");
                  printf("Please reenter:");
                  scanf("%d", &grade);
                  }
            if (grade>=90) A++;
            else if (grade>=80) B++;
            else if (grade>=70) C++;
            else if (grade>=60) D++;
            else F++;
            printf("Please enter another grade or %d if done:", SENTINEL);
            scanf("%d", &grade);
            }

      if(grade=SENTINEL)
            return(A, B, C, D, F, Invalid);

      }



void graph(int A, int B, int C, int D, int F, int Invalid)
      
      {
      printf("The following table corresponds to data containing ");
      printf("%d A's, %d B's, %d C's, %d D's, ", A, B, C, D);
      printf("%d F's and %d invalid grades.\n\n\n\n", F, Invalid);

      printf("A's: ");
      while (A!=0)
            {
            printf("X");
            A--;
            }

      printf("\n\nB's: ");
      while (B!=0)
            {
            printf("X");
            B--;
            }

      printf("\n\nC's: ");
      while (C!=0)
            {
            printf("X");
            C--;
            }

      printf("\n\nD's: ");
      while (D!=0)
            {
            printf("X");
            D--;
            }

      printf("\n\nF's: ");
      while (F!=0)
            {
            printf("X");
            F--;
            }
      printf("\n\n\n\n");

      }
0
SaraBiz
Asked:
SaraBiz
1 Solution
 
imladrisCommented:
Note this sequence in getgrades:

scanf("%d", &grade);


while (grade!=SENTINEL)
{
while((scanf("%d", grade)!=1)||(grade<0)||(grade>100))


It requires the user to enter a grade, then (if the entry was not the sentinel) it waits for another entry (without having printed any prompt).

Have you tried entering two numbers?
What did it do?
(It seems to me from the code that it will print something after you entered the second grade).
0
 
SaraBizAuthor Commented:
Yes, and eventually everything was dumped. Why wouldn't it go down to the printf at the bottom asking for another grade? How can I make it do that?
0
 
SaraBizAuthor Commented:
Ok, I changed it a little, so now it runs, but it never asks for another grade! How can I fix this? Thanks!  (here's the changed program)


/* Sara Bissonnette */



#include <stdio.h>

#define MAXGRADE 100

#define SENTINEL 999



void instructions(void);
int getgrades(void);
void graph(int, int, int, int, int, int);


int main(void)

        {
      int A, B, C, D, F, Invalid;
      
        instructions();
        getgrades();
        }

void instructions(void)

        {
        printf("\n\n\nThis program will form a bar chart from a ");
        printf("sequence of grades.\n\n");
        }

int getgrades(void)

        {
      int A, B, C, D, F, Invalid, grade;

      A=0; B=0; C=0; D=0; F=0; Invalid=0;

        printf("Please enter a grade between 0 and %d, rounding ", MAXGRADE);
        printf(" to the nearest integer.\nEnter %d when done:", SENTINEL);
      scanf("%d", &grade);

      if(grade=SENTINEL)
            graph(A, B, C, D, F, Invalid);


      else
            {
            if((scanf("%d", grade)!=1)||(grade<0)||(grade>100))
                  {
                  Invalid ++;
                  printf("That is not a valid grade.\n\n\n\n");
                  printf("Please reenter:");
                  scanf("%d", &grade);
                  }
            else if (grade>=90) A++;
            else if (grade>=80) B++;
            else if (grade>=70) C++;
            else if (grade>=60) D++;
            else F++;
            printf("Please enter another grade or %d if done:", SENTINEL);
            scanf("%d", &grade);
            return(grade);
            }

      

      }



void graph(int A, int B, int C, int D, int F, int Invalid)
      
      {
      printf("The following table corresponds to data containing ");
      printf("%d A's, %d B's, %d C's, %d D's, ", A, B, C, D);
      printf("%d F's and %d invalid grades.\n\n\n\n", F, Invalid);

      printf("A's: ");
      while (A!=0)
            {
            printf("X");
            A--;
            }

      printf("\n\nB's: ");
      while (B!=0)
            {
            printf("X");
            B--;
            }

      printf("\n\nC's: ");
      while (C!=0)
            {
            printf("X");
            C--;
            }

      printf("\n\nD's: ");
      while (D!=0)
            {
            printf("X");
            D--;
            }

      printf("\n\nF's: ");
      while (F!=0)
            {
            printf("X");
            F--;
            }
      printf("\n\n\n\n");

      }
0
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 
SaraBizAuthor Commented:
Ok, changed it again, but now the table won't print!



/* Sara Bissonnette */



#include <stdio.h>

#define MAXGRADE 100

#define SENTINEL 999



void instructions(void);
int getgrades(void);
void graph(int, int, int, int, int, int);


int main(void)

        {
      int A, B, C, D, F, Invalid;
      
        instructions();
        getgrades();
        }

void instructions(void)

        {
        printf("\n\n\nThis program will form a bar chart from a ");
        printf("sequence of grades.\n\n");
        }

int getgrades(void)

        {
      int A, B, C, D, F, Invalid, grade;

      A=0; B=0; C=0; D=0; F=0; Invalid=0;

        printf("Please enter a grade between 0 and %d, rounding ", MAXGRADE);
        printf(" to the nearest integer.\nEnter %d when done:", SENTINEL);
      scanf("%d", &grade);

      
      
      while ((grade>=0)&&(grade<=MAXGRADE))
            {
            if (grade>=90) A++;
            else if (grade>=80) B++;
            else if (grade>=70) C++;
            else if (grade>=60) D++;
            else F++;
            
            printf("Please enter another grade or %d if done:", SENTINEL);
            scanf("%d", &grade);
            }

      while((scanf("%d", grade)!=1)||(grade<0)||(grade>MAXGRADE))
                  {
                  Invalid ++;
                  printf("That is not a valid grade.\n\n\n\n");
                  printf("Please reenter:");
                  scanf("%d", &grade);
                  }

      if(grade==SENTINEL)
            graph(A, B, C, D, F, Invalid);
      

      

      }



void graph(int A, int B, int C, int D, int F, int Invalid)
      
      {
      printf("The following table corresponds to data containing ");
      printf("%d A's, %d B's, %d C's, %d D's, ", A, B, C, D);
      printf("%d F's and %d invalid grades.\n\n\n\n", F, Invalid);

      printf("A's: ");
      while (A!=0)
            {
            printf("X");
            A--;
            }

      printf("\n\nB's: ");
      while (B!=0)
            {
            printf("X");
            B--;
            }

      printf("\n\nC's: ");
      while (C!=0)
            {
            printf("X");
            C--;
            }

      printf("\n\nD's: ");
      while (D!=0)
            {
            printf("X");
            D--;
            }

      printf("\n\nF's: ");
      while (F!=0)
            {
            printf("X");
            F--;
            }
      printf("\n\n\n\n");

      }
0
 
zultiCommented:
this is one error
should be &grade and not grade !!!


while((scanf("%d", &grade)!=1)||(grade<0)||(grade>MAXGRADE))

i would replace the code of enter a number to :

while((scanf("%d", &grade)==1) && (grade != SENTINEL))
{
   if ((grade >= 0) && (grade <= MAXGRADE))
   {
      if      (grade>=90) A++;
      else if (grade>=80) B++;
      else if (grade>=70) C++;
      else if (grade>=60) D++;
      else F++;
      printf("Please enter another grade or %d if done : ", SENTINEL);
   }
   else
   {
      Invalid ++;
      printf("That is not a valid grade. \n\n\n\n");
      printf("Please reenter : ");
   }
}

that should work
0
 
mmessuriCommented:
Sara:

Might I recommend the following solution (note that this is one of many possible ways to perform the task's that you are wishing).  I have left the original code that I commented out, in this example so that you may perform a side by side comparison.

Please let me know if you have any questions.

-- Michael Messuri --

/* Sara Bissonnette */
#include <stdio.h>
#include <stdlib.h>

/* If this is a DOS based program use this line, else remark out
   this line and replace with the proper header file (used for memset) */
#include <mem.h>

#define MAXGRADE 100
#define SENTINEL 999

/* Orgainize our data into a structure */

typedef struct STUDENTGRADES {
   unsigned int A;
   unsigned int B;
   unsigned int C;
   unsigned int D;
   unsigned int F;
   unsigned int Invalid;
   unsigned int unused1;
   unsigned int unused2;
}GRADES;

void instructions(void);
/* int getgrades(void); */
void getgrades(GRADES *);
void graph(GRADES *);
/* void graph(int, int, int, int, int, int); */

int main(void)
{
/*   int A, B, C, D, F, Invalid; */
   GRADES mGrades;

   memset(&mGrades, 0x0, sizeof(struct STUDENTGRADES) );

   instructions();
/*   getgrades(); */
   getgrades(&mGrades);
/*   graph(A, B, C, D, F, Invalid); */
   graph(&mGrades);

   return 0;
}

void instructions(void)
{
  printf("\n\n\nThis program will form a bar chart from a ");
  printf("sequence of grades.\n\n");
}

//int getgrades(void)
void getgrades(GRADES *mGrades)
{
/*  int A, B, C, D, F, Invalid, grade; */
  int grade;
  char uinput[4];
/*
  A=0;
  B=0;
  C=0;
  D=0;
  F=0;
  Invalid=0;
*/
  printf("Please enter a grade between 0 and %d, rounding ", MAXGRADE);
  printf(" to the nearest integer.\nEnter %d when done:", SENTINEL);

  do {
   memset(uinput, 0x0, sizeof(uinput));
   gets(uinput);
   grade = atoi(uinput);
   if(grade > 0 && grade <= 100){
      if(grade >= 90)
         mGrades->A++;
      else if(grade >= 80)
         mGrades->B++;
      else if(grade >= 70)
         mGrades->C++;
      else if(grade >= 60)
         mGrades->D++;
      else
         mGrades->F++;
   } else
       mGrades->Invalid++;

   if(grade != SENTINEL)
     printf("Please enter another grade or %d if done:", SENTINEL);

  } while(grade != SENTINEL);

/*    scanf("%d", &grade); */
/*
  while (grade!=SENTINEL) {
   while((scanf("%d", grade)!=1)||(grade<0)||(grade>100)){
      Invalid ++;
      printf("That is not a valid grade.\n\n\n\n");
      printf("Please reenter:");
      scanf("%d", &grade);
   }

   if (grade>=90)
      A++;
   else if (grade>=80)
      B++;
   else if (grade>=70)
      C++;
   else if (grade>=60)
      D++;
   else
      F++;
   printf("Please enter another grade or %d if done:", SENTINEL);
   scanf("%d", &grade);
 }

 if(grade=SENTINEL)
 return(A, B, C, D, F, Invalid);*/
}



/* void graph(int A, int B, int C, int D, int F, int Invalid) */
void graph(GRADES *mGrades)
{
  int x;
  printf("The following table corresponds to data containing ");
  printf("%d A's, %d B's, %d C's, %d D's, ", mGrades->A, mGrades->B,
    mGrades->C, mGrades->D);
  printf("%d F's and %d invalid grades.\n\n\n\n", mGrades->F, mGrades->Invalid);

  printf("A's: ");
  for(x = 0; x < mGrades->A; x++)
    printf("X");

  printf("\n\nB's: ");
  for(x = 0; x < mGrades->B; x++)
    printf("X");

  printf("\n\nC's: ");
  for(x = 0; x < mGrades->C; x++)
    printf("X");

  printf("\n\nD's: ");
  for(x = 0; x < mGrades->D; x++)
    printf("X");

  printf("\n\nF's: ");
  for(x = 0; x < mGrades->F; x++)
    printf("X");

/*
  while (A!=0){
   printf("X");
   A--;
  }

  printf("\n\nB's: ");
  while (B!=0){
   printf("X");
   B--;
  }

  printf("\n\nC's: ");
  while (C!=0){
   printf("X");
   C--;
  }

  printf("\n\nD's: ");
  while (D!=0){
   printf("X");
   D--;
  }

  printf("\n\nF's: ");
  while (F!=0){
   printf("X");
   F--;
  }*/

  printf("\n\n\n\n");
}
0
 
hongjunCommented:
I have suggested my answer to your above problem. More importantly, I have tried running my suggested solution and result is what you wanted. Hope it helps.

One bug for mmessuri's answer is that 0 is not considered as a F grade.

SaraBiz, if you find my answer acceptable, you could easily choose to accept my comment as an answer.

/* I uses pointer to solve your problem */

#include <stdio.h>
#include <stdlib.h>

#define SENTINEL 999
#define MAXGRADE 100

void instructions(void);
void getgrades(int*, int*, int*, int*, int*, int*);
void graph(int*, int*, int*, int*, int*, int*);

main()
{
    int A, B, C, D, F, Invalid;

    A=B=C=D=Invalid=0;
    instructions();
    getgrades(&A, &B, &C, &D, &F, &Invalid);
    graph(&A, &B, &C, &D, &F, &Invalid);

    printf("\n\nProgram listing is over......\n");

    return EXIT_SUCCESS;
}

void instructions(void)
{
    printf("\n\n\nThis program will form a bar chart from a ");
    printf("sequence of grades.\n\n");
}

void getgrades(int *A, int *B, int *C, int *D, int *F, int *Invalid)
{
    int value=0;

    while ( 1 ) {
      printf("Enter a grade (%d to quit) > ",SENTINEL);
      scanf("%d",&value);
      if ( value == SENTINEL )
          break;
      if ( value < 0 || value > 100 ) {
          puts("You have entered an invalid marks!");
          (*Invalid)++;
          continue;
      }
      if ( value >= 90 )
          (*A)++;
      else if ( value >= 80 )
          (*B)++;
      else if ( value >= 70 )
          (*C)++;
      else if ( value >= 60 )
          (*D)++;
      else
          (*F)++;
    }
}

void graph(int *A, int *B, int *C, int *D, int *F, int *Invalid)
{
    printf("\n\nThe following table corresponds to data containing ");
    printf("%d A's, %d B's, %d C's, %d D's, ", *A, *B, *C, *D);
    printf("%d F's and %d invalid grades.\n\n", *F, *Invalid);

    printf("A's: ");
    while ( (*A)-- != 0 )
      putchar('X');

    printf("\n\nB's: ");
    while ( (*B)-- != 0 )
      putchar('X');

    printf("\n\nC's: ");
    while ( (*C)-- != 0 )
      putchar('X');

    printf("\n\nD's: ");
    while ( (*D)-- != 0 )
      putchar('X');

    printf("\n\nF's: ");
    while ( (*F)-- != 0 )
      putchar('X');

    printf("\n\nInvalid's: ");
    while ( (*Invalid)-- != 0 )
      putchar('X');
}

hongjun
0
 
AxionCommented:
Always Remember:
When reading in an integer,  use a & before the variable.
eg, scanf("%d", &variable);
If u leave this out, alot of wierd stuff will happen.
0
 
hongjunCommented:
Sarabiz,
This question was answered long long ago but you still did not response to this question.
Could you please evaluate this question as soon as possible once you read this comment. I find it unfair to those who have made his or her very own's effort to help but their help were just not appreciated.

hongjun
0
 
SaraBizAuthor Commented:
Sorry for the delay!
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now