Program Repeat Help(Y or N)

Discussion in 'Programming/Scripts' started by tutu12, Mar 7, 2019.

  1. tutu12

    tutu12 New Member

    Hello, my code works fine the first run-through but when you repeat it, it skips over two user inputs. Can someone show me what I am missing?

    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;

    float averagescore = 0.0;
    string students_name;
    int exams=0, exam_score=0, totalscore=0;
    //prompts user for input of name and number of exams taken by student

    void getStudentName()
    {
    cout << "Please enter the student's name: ";
    getline(cin, students_name);
    }

    void getNumberExams()
    {
    cout<<"Please enter the number of exams taken by the student in the course:"<<endl;
    cin>>exams;
    cout<<"Enter the exam scores:"<<endl;
    while (exams < 1)
    {
    cout<<"Invalid: Please Try Again"<<endl;
    cin >> exams;
    }
    }

    void getScoreAndCalculateTotal()
    {
    for(int count = 1;count <= exams;count ++)
    {
    cout<< "Exam " << count << ": ? ";
    cin >> exam_score;
    totalscore = exam_score + totalscore;
    }
    }

    void calculateAverage()
    {
    averagescore = totalscore/exams;
    }

    void displayAverageGrade()
    {
    cout<<fixed << setprecision(1);
    cout << "Average: " << averagescore<<endl;
    }

    void determineLetterGrade()
    {
    if (averagescore >=90)
    {cout << "Letter Grade Earned: A"<<endl;}
    else if(averagescore >=80)
    {cout << "Letter Grade Earned: B"<<endl;}
    else if(averagescore >=70)
    {cout << "Letter Grade Earned: C"<<endl;}
    else if(averagescore >=60)
    {cout << "Letter Grade Earned: D"<<endl;}
    else
    {cout<<"Letter Grade Earned: F"<<endl;}
    }

    int main()
    {
    char type;
    while (true) {

    getStudentName();

    getNumberExams();

    getScoreAndCalculateTotal();

    calculateAverage();

    cout << "Student Name: "<<students_name<<endl;
    displayAverageGrade();

    determineLetterGrade();

    cout << "Would you like to calculate another student’s grade? < Y or N >" << endl;
    cin >> type;

    if ((type == 'N') || (type == 'n')) {
    break;
    }
    }
    }
     
  2. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    What programming language are you using? That looks like C++.
    Which inputs?
    Make a smaller test case, that includes only the parts of code that are relevant to this problem. Then paste that code in CODE tags to make it more readable. Now your code has lost indentation which makes it hard to read.
    To undestand how your code fails to work as expected, add extra output statements to show values of variables and which lines are executed, or run your program under debugger.
     

Share This Page