• C

# 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 SENTINEL 999

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

int main(void)

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

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

void instructions(void)

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

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

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

printf(" to the nearest integer.\nEnter %d when done:", SENTINEL);

{
{
Invalid ++;
printf("That is not a valid grade.\n\n\n\n");
}
else F++;
}

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");

}
###### 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.

Commented:

{

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
Author 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
Author 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 SENTINEL 999

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

int main(void)

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

instructions();
}

void instructions(void)

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

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

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

printf(" to the nearest integer.\nEnter %d when done:", SENTINEL);

graph(A, B, C, D, F, Invalid);

else
{
{
Invalid ++;
printf("That is not a valid grade.\n\n\n\n");
}
else F++;
}

}

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
Author Commented:
Ok, changed it again, but now the table won't print!

/* Sara Bissonnette */

#include <stdio.h>

#define SENTINEL 999

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

int main(void)

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

instructions();
}

void instructions(void)

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

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

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

printf(" to the nearest integer.\nEnter %d when done:", SENTINEL);

{
else F++;

}

{
Invalid ++;
printf("That is not a valid grade.\n\n\n\n");
}

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
Commented:
this is one error

i would replace the code of enter a number to :

{
{
else F++;
}
else
{
Invalid ++;
printf("That is not a valid grade. \n\n\n\n");
}
}

that should work
0
Commented:
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 SENTINEL 999

/* Orgainize our data into a structure */

unsigned int A;
unsigned int B;
unsigned int C;
unsigned int D;
unsigned int F;
unsigned int Invalid;
unsigned int unused1;
unsigned int unused2;

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

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

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

return 0;
}

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

{
/*  int A, B, C, D, F, Invalid, grade; */
char uinput[4];
/*
A=0;
B=0;
C=0;
D=0;
F=0;
Invalid=0;
*/
printf(" to the nearest integer.\nEnter %d when done:", SENTINEL);

do {
memset(uinput, 0x0, sizeof(uinput));
gets(uinput);
else
} else

/*
Invalid ++;
printf("That is not a valid grade.\n\n\n\n");
}

A++;
B++;
C++;
D++;
else
F++;
}

return(A, B, C, D, F, Invalid);*/
}

/* void graph(int A, int B, int C, int D, int F, int Invalid) */
{
int x;
printf("The following table corresponds to data containing ");

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

Experts Exchange Solution brought to you by

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

Commented:
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

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 ");
}

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
Commented:
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
Commented:
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
Author Commented:
Sorry for the delay!
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.