2

I would like the code to be able to accept a full name when I enter a name. How do I allow cin to accept spaces?

When I input a name with spaces, the result is it assumes that the next input of the subject name is the last name. How can I fix this?

#include<iostream>
using namespace std;

int main()
{
    float mark1, mark2, mark3, mark4;
    char subject1[25], subject2[25], subject3[25], subject4[25];
    char name;
    float average;

    cout << "Please enter your name"<< endl;
    cin >> name;

    cout << "Please enter the first subject name"<< endl;
    cin >> subject1;

    cout << "Please enter the mark for " <<subject1<< endl;
    cin >>mark1;

    cout << "Please enter the second subject name"<< endl;
    cin >> subject2;

    cout << "Please enter the mark for " <<subject2<< endl;
    cin >>mark2;

    cout << "Please enter the third subject name"<< endl;
    cin >> subject3;

    cout << "Please enter the mark for " <<subject3<< endl;
    cin >>mark3;

    cout << "Please enter the fourth subject name"<< endl;
    cin >> subject4;

    cout << "Please enter the mark for " <<subject4<< endl;
    cin >>mark4;

    average=(mark1+mark2+mark3+mark4) / 4;

    cout << name << "'s " << subject1<< " mark is " <<mark1 <<", " << subject2<< " mark is " << mark2 <<", " <<subject3 <<" mark is " <<mark3 <<", " <<subject4<< " mark is " <<mark4 <<". His/her average is " <<average<< endl;

    cout << endl;

    system("PAUSE");
    return 0;
}
Kara
  • 6,115
  • 16
  • 50
  • 57

1 Answers1

1

Try using getline(cin, <variable name>)

For example:

string name;
getline(cin, name);
cout << name;

Make sure to read about using getline though: getline reference

westonkd
  • 96
  • 9