-3

So I have a coding project where I have to calculate the overall grade of a student in a class where they have 4 quizzes, 2 midterms, and 1 final exam. The quiz and exam are weighted 30% and the midterm 40%. I just help with 2 things:

  1. My code doesn't want to start w/o having 4 individual input numbers (79 80 0 0).
  2. When getting to the exam part of the code, it for some reason bypasses the needed user input and autos it to the 3rd number in the quiz coding. I'm coding in c programming.
#include <stdio.h>

int main()
{
    int testOne, testTwo;
    float totTest, percTest, testPoint;
    printf("Enter you test grades:\n");
    scanf("%d%d\n",&testOne,&testTwo);
    totTest=testOne+testTwo;
    printf("Your total grade points is: %.1f\n", totTest);
    testPoint=200;
    percTest=(totTest*.40)/testPoint *100;
    printf("Your percentage is %.1f%\n", percTest);
    
    int quizOne, quizTwo, quizThree, quizFour;
    float totQuiz, percQuiz, quizPoint;
    printf("Enter you quiz grades:\n");
    scanf("%d%d%d%d\n", &quizOne, &quizTwo, &quizThree, &quizFour);
    totQuiz=quizOne+quizTwo+quizThree+quizFour;
    printf("Your total quiz grade is %.1f\n", totQuiz);
    quizPoint=400;
    percQuiz=(totQuiz*.30)/quizPoint *100;
    printf("Your quiz percentage is %.1f%\n", percQuiz);
    
    int examOne, examPoint; <This is the code that automatically messes up>
    float percExam, finGrade;
    printf("Enter your exam grade\n");
    scanf(" %d\n", &examOne);
    examPoint=100;
    percExam=(examOne*.30)/examPoint *100;
    printf("Your final exam grade is %.1f\n", percExam);
    finGrade=(percExam+percQuiz+percTest);
    printf("Your overall grade is %.1f%\n", finGrade);
    
    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • Time to learn [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Filburt Sep 16 '21 at 18:37
  • Note that scanf() returns the # of input items that were successfully processed. Related: [https://stackoverflow.com/questions/10469643/what-does-the-scanf-function-return](https://stackoverflow.com/questions/10469643/what-does-the-scanf-function-return) – drescherjm Sep 16 '21 at 18:47
  • Search the internet for "C grade calculator quizzes". It's been done before, especially on StackOverflow. – Thomas Matthews Sep 16 '21 at 18:53
  • Your code has a few warnings but seemed to work: [https://ideone.com/cnXPuJ](https://ideone.com/cnXPuJ) when I typed the 80 for the 7 inputs. – drescherjm Sep 16 '21 at 19:38
  • I don't understand your point #1. Your code asks for 2 numbers then 4 numbers then 1 number. – drescherjm Sep 16 '21 at 19:41
  • The `\n` in `scanf("%d%d\n",` while cause `scanf` to continue reading and discarding whitespace input until the user enters non-whitespace input. That is probably the reason for at least one of your problems. You should remove the `\n`, or better, stop using `scanf` altogether. See this page for information on alternatives: [A beginners' guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) – Andreas Wenzel Sep 16 '21 at 20:40
  • 1
    Please choose a title which will help others with the same issue, as SO is a repository for questions and answers rather than a personal help desk. Thank you – user438383 Sep 17 '21 at 12:55

1 Answers1

2

Your problems have nothing to do with the logic of your program, nothing to do with computing grades, and everything to do with: scanf. scanf is simultaneously the most useful-looking, and the most actually useless, function in the C library when it comes to introductory C programming.

Here are three simple rules for using scanf in introductory C programming:

  1. Use only one of the following four input formats: "%d", "%s", "%f", "%lf". If you simply must read individual characters, you can add a fifth: " %c" (but make sure you always use that extra space). Use these in isolation, one at a time, without combining them or adding any other characters.

  2. Always check the return value of scanf. If it does not return 1, print an error message and exit: if(scanf(...) != 1) {printf("input error!\n"); exit(1); }

  3. If you want to do something fancier, that you can't accomplish with the limited number of formats allowed by rule 1, then it's time to learn how to use something better than scanf.

In particular, don't try to read multiple numbers at once using formats like %d%d. And don't use \n in scanf format statements, either — it doesn't do what you think, and it's probably causing you problems.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103