Program Repeat Help(Y or N)

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;
}
}
}
It is because you are mixing formatted and unformatted input.

Simplest fix:

 
#include <limits> 
1
2
3
// Get a string
std::string s;
getline( std::cin, s );
1
2
3
4
// Get anything else
int n;
std::cin >> n;
std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );


Consider writing yourself a function to get anything else:
1
2
3
4
5
6
7
8
template <typename T>
T get()
{
  T result;
  std::cin >> result;
  std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  return result;
}
1
2
3
// Get anything else
int n;
n = get <int> ();

Or, my fave, a manipulator to do it.

Hope this helps.
Heh, here’s that manipulator:

1
2
3
4
5
6
7
8
9
#include <iomanip>
#include <iostream>
#include <limits>

template <class CharT, class Traits>
std::basic_istream <CharT, Traits> & endl( std::basic_istream <CharT, Traits> & ins )
{
  return ins.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
}

And an example use:
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using namespace std;

int main()
{
  int n;
  cout << "n? ";
  cin >> n >> endl;

  string s;
  cout << "s? ";
  getline( cin, s );

  cout << "s = \"" << s << "\"\nn = " << n << "\n";
}

Enjoy!
Topic archived. No new replies allowed.