-4
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
string fullName, honorsRank, className;
int year;
int testScore1, testScore2, testScore3, testScore4, testScore5;
int avgScore;
int main()
{
// Retrieve students name
cout << "Please enter your full name: " << endl;
cin >> fullName;

// Retrieve students year of high school
cout << "Please indicate which year of High School you are currently     in: " << endl;;
cin >> year;

// As for all five test scores
cout << "Please enter your score for your first exam: " << endl;
cin >> testScore1;

cout << "Please enter your score for your second exam: " << endl;
cin >> testScore2;

cout << "Please enter your score for your third exam: " << endl;
cin >> testScore3;

cout << "Please enter your score for your fourth exam: " << endl;
cin >> testScore4;

cout << "Please enter your score for your fifth exam: " << endl;
cin >> testScore5;


//Compute the average score of the five tests
avgScore = (testScore1 + testScore2 + testScore3 + testScore4 + testScore5)/5;

// Assign either freshman, sophomore, junior, or senior to the numbered year of high school
if (year == 1)
className = "Freshman";
else if (year == 2)
className = "Sophomore";
else if (year == 3)
className = "Junior";
else if (year == 4)
className = "Senior";

cout << "Name........ " << fullName << endl;
cout << "Year........ " << className << endl;
cout << "Scores...... " << testScore1 << " " << testScore2 << " " << testScore3 << " " <<testScore4 << " " << testScore5 << endl;
cout << "Average..... " << avgScore << endl;


// Determine students academic standing
if (avgScore >= 97.0 && year == 4) {
honorsRank = "**High Honors**", cout << honorsRank << " Note: This student IS ELIGIBLE for graduation" << endl;
}
else if (avgScore >= 95.0 && year == 4) {
honorsRank = "**Honors**", cout << honorsRank << " Note: This student IS ELIGIBLE for graduation" << endl;
}
else if (avgScore >= 90.0 && year == 4) {
honorsRank = "**Honorable Mention**", cout << honorsRank << " Note: This student IS ELIGIBLE for graduation" << endl;
}
else if (avgScore > 65 && year == 4) {
    cout << "Note: This student IS ELIGIBLE for graduation" << endl;
}
else if (avgScore < 65.0 && year == 4) {
    cout << "Note: This student is NOT ELIGIBLE for graduation" << endl;
}
else if (avgScore >= 97.0 && year < 4) {
    honorsRank = "**High Honors**", cout << honorsRank << " Great work!! Keep it up!!" << endl;
}
else if (avgScore >= 95.0 && year < 4) {
    honorsRank = "**Honors**", cout << honorsRank << " Great effort!!" << endl;
}
else if (avgScore >= 90.0 && year < 4) {
    honorsRank = "**Honorable Mention**", cout << honorsRank << " Nice job!" << endl;
}
else if (avgScore < 65.0 && year < 4) {
    cout << "Note: This student has been placed on academic probation" << endl;
}

return 0;
}

So I know that the program runs as I intended it to (https://i.stack.imgur.com/cqRAU.jpg) but when I type in a full name for someone with 2 words, it completely messes up the program (https://i.stack.imgur.com/UOr6x.jpg).

For some reason entering a first and last name messes up the program and causes all of the cout statements to display and it won't let you enter anything in the text fields.

Any advice/tips?

Also I'm more of a beginner C++ programmer if you couldn't tell, so please don't get too annoyed by any idiotic mistakes if you see any :))

eichel_15
  • 3
  • 2
  • It's the "correct" behaviour because `cin >> fullName`, `fullName` being full name, does not make sense. – LogicStuff May 13 '17 at 22:55
  • 1
    Related: [std::cin input with spaces?](http://stackoverflow.com/questions/5838711) – Drew Dormann May 13 '17 at 22:56
  • But use the highest upvoted answer, not the accepted one. – Baum mit Augen May 13 '17 at 23:02
  • This question has been answered e.g. here: http://stackoverflow.com/questions/9469264/c-cin-only-reads-the-first-word in short, cin >> only reads the first word; for the whole line, you'd need getline(cin,str). Hope that, helps, Chris – chros May 13 '17 at 22:57

1 Answers1

0

cin breaks on a space. Try: getline(cin, fullName);

parsley72
  • 8,449
  • 8
  • 65
  • 98