• C

Question in C

I have written a program that is suppose to take in grades from the user and make a vertical chart from the data.  Unfortunetly, I seem to have done something wrong, for although it compiles correctly, it doesn't run right! If anyone could help me, I'd really appreciate it.  Thanks!


/* Sara Bissonnette */



#include <stdio.h>

#define MAXGRADE 100

#define SENTINEL 999



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



int main(void)

        {
      int       A=0,
            B=0,
            C=0,
            D=0,
            F=0,
            Invalid=0;
      
        instructions();
        getgrades(&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(int *a, int *b, int *c, int *d, int *f, int *invalid)

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

      *a=A; *b=B; *c=C; *d=D; *f=F; *invalid=Invalid;

        printf("Please enter a grade between 0 and %d, rounding ", MAXGRADE);
        printf(" to the nearest integer.\nEnter %d when done:", SENTINEL);
      input=scanf("%d", &grade);
      
      
      
      while (grade!=SENTINEL)
            {
            if(input!=1)
                  {
                  printf("That input is invalid.\n\nPlease reenter:");
                  input=scanf("%d", &grade);
                  Invalid ++;
                  }


            else if((grade>100)||(grade<0))
                  {
                  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);
                  }
            }

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

      }



void graph(int A, int B, int C, int D, int F, int Invalid)
      
      {
      int max;

      printf("\nThe following table corresponds to data containing\n ");
      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);

      max=findmax(A, B, C, D, F);

      
      

      while(max>0)

            {
            if(A==max)
                  {
                  printf("X     ");
                  A--;
                  }
            else
                  printf("      ");

            if(B==max)
                  {
                  printf("X     ");
                  B--;
                  }
            else
                  printf("      ");

            if(C==max)
                  {
                  printf("X     ");
                  C--;
                  }
            else
                  printf("      ");

            if(D==max)
                  {
                  printf("X     ");
                  D--;
                  }
            else
                  printf("      ");

            if(F==max)
                  {
                  printf("X     \n");
                  F--;
                  }
            else
                  printf("      \n");


            
            max--;
            }

      printf("A's   B's   C's   D's   F's\n\n");

      }                        
      


int findmax(int A, int B, int C, int D, int F)
      {
      int max;
      if(A>B) A=max;
      else B=max;
      if(C>max) C=max;
      if(D>max) D=max;
      if(F>max) F=max;

      return(max);
      }

SaraBizAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dhaineaultCommented:
You're saying that it isn't running right...well what is it doing?

I did not analyse your entire code, but one thing that strikes me is these 2 lines:

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

    *a=A; *b=B; *c=C; *d=D; *f=F; *invalid=Invalid;

I *think* what you want is: a = &A; b = &B <etc>...
The way you have it now, you are probably getting some very wrong and big results for a, b, <etc>...

An easier suggestion would be to get rid of the A, B, C... local variables, and instead use something like:

   if(grade>=90) (*a)++;

instead of

   if (grade>=90) A++;

Right now, the way you have it, 'a' is not pointing to 'A', (I think that is what you wanted) but instead '*a' holds the value that 'A' was *randomly* assigned when the variable was declared.

0
SaraBizAuthor Commented:
Thank you for your help...now my program works, except for one flaw...if the user enters a non-integer, the program goes into an infinite loop displaying an error message.  Any ideas?

Here is what I have changed it to...



/* Sara Bissonnette */



#include <stdio.h>

#define MAXGRADE 100

#define SENTINEL 999



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



int main(void)

        {
      int       A=0,
            B=0,
            C=0,
            D=0,
            F=0,
            Invalid=0;
      
        instructions();
        getgrades(&A, &B, &C, &D, &F, &Invalid);
      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(int *a, int *b, int *c, int *d, int *f, int *invalid)

        {
      int grade, input;

      

        printf("Please enter a grade between 0 and %d, rounding ", MAXGRADE);
        printf(" to the nearest integer.\nEnter %d when done:", SENTINEL);
      input=scanf("%d", &grade);
      
      
      
      while (grade!=SENTINEL)
            {
            
            if( (grade>100) || (grade<0) || (input!=1) )
                  {
                  (*invalid)++;
                  printf("That is not a valid grade.\n\n\n\n");
                  printf("Please reenter:");
                  input=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);
                  }
            }
      

      }



void graph(int A, int B, int C, int D, int F, int Invalid)

      
      {
      int max;

      printf("\nThe following table corresponds to data containing\n ");
      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);

      max=findmax(A, B, C, D, F);

      
      

      while(max>0)

            {
            if(A==max)
                  {
                  printf("X     ");
                  A--;
                  }
            else
                  printf("      ");

            if(B==max)
                  {
                  printf("X     ");
                  B--;
                  }
            else
                  printf("      ");

            if(C==max)
                  {
                  printf("X     ");
                  C--;
                  }
            else
                  printf("      ");

            if(D==max)
                  {
                  printf("X     ");
                  D--;
                  }
            else
                  printf("      ");

            if(F==max)
                  {
                  printf("X     \n");
                  F--;
                  }
            else
                  printf("      \n");


            
            max--;
            }

      printf("A's   B's   C's   D's   F's\n\n");

      }                        
      


int findmax(int A, int B, int C, int D, int F)
      {
      int max;

      if(A>B) max=A;
      else max=B;
      if(C>max) max=C;
      if(D>max) max=D;
      if(F>max) max=F;

      return(max);
      }

0
dhaineaultCommented:
Well the best suggestion I can make is to avoid using scanf().
This is because scanf() offers no control over the data that the user will enter in order to verify its integrity.

Instead use gets() to read in a string and then convert that string to an int with atoi()

Example:

void function(int *a)
{
char buf[80];

printf("Enter number: ");
gets(buf);

if( isNumString(buf) == true )
    *a = atoi(buf);
else
   /* invalid number; try again */
}
Note: isNumString() is a function that you will have to write in order to verify if 'buf' is a numeric string. It does not exist (Tip: look at the isdigit() function.)
0
Challenges in Government Cyber Security

Has cyber security been a challenge in your government organization? Are you looking to improve your government's network security? Learn more about how to improve your government organization's security by viewing our on-demand webinar!

basantCommented:
I *think* there is certainly a problem in ur findmax too. you are returning a value without initialization.

The correct one may be :

int findmax(int A, int B, int C, int D, int F)
{
int max;
if(A>B) max = A; // Note the diff
else max = B; // Note the diff
if(C>max) max = C; // Note the diff
if(D>max) max = D; // Note the diff
if(F>max) max = F; // Note the diff

return(max);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SaraBizAuthor Commented:
Is there any way I can still find it without using a string and still using scanf? The professor may find it strange if I use something we haven't learned yet.  Thanks!
0
dhaineaultCommented:
Well although I must insist that scanf() is not the best solution, if you really want to use it, you could do something like:

   scanf("%s", buf); /* instead of gets() */
   grade = atoi(buf);

    if( isNumString(buf) == true )
       *a = atoi(buf);
    else
       /* invalid number; try again */

As you can, see this is pretty much identical to what I gave you before, except for using scanf().  No matter what you use to read in the value, you MUST implement an 'isNumString()' type function in order to validate the user's input.
0
ozoCommented:
scanf("%d", &grade) will return the number of successfully matched and assigned input items
0
tzxie2000Commented:
i think the best solution is using getch() to limit the user's input.
scanf can get the correct answer,but when your program's user enter "12a3" ,as he want to enter "1233" ,he may fill strag.
i think the answer will be below
bool GetNum(int * pi)
{
  char c;
  int num;
 
  num =0;
  for(;;)
  {
    c = getch();
    if (27 == c)//ESC
      break;
    if (13 == c)//ENTER
      break;
    if (8 == c)//BACKSPACE
      num/=10;
    if (('0'<=c)&&(c<='9'))
    {
      num*=10£»
      num+=c-'0';
    }
  }
  if(13 == c)
    *pi = num;
  return 13 == c;
}
the upper function can get a num
if you want get a float ,may be you should write some more code
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.