#include <iostream>
usingnamespace std;
struct form
{
char firstName[20];
char lastName[20];
char gradeLetter[1];
int age;
}person;
int main()
{
//Input Section
cout << "What is your first name? ";
cin.getline(person.firstName, 20);
cout << "What is your last name? ";
cin.getline(person.lastName, 20);
cout << "What letter grade do you deserve? ";
cin.getline(person.gradeLetter, 1);
cout << "What is your age? ";
(cin >> person.age).get();
// Output Section
cout << "Name: " << person.lastName << ", " << person.firstName << endl;
cout << "Grade: " << person.gradeLetter << endl;
cout << "Age: " << person.age << endl;
system("pause");
return 0;
}
The program is suppose to ask user for first name, last name, grade, age, and then output the information given by user.
The problem is that for some reason after the "What letter grade do you deserve?" part, when I press Enter key, the program skips the "What is your age?" question, displays the info, but it omits the grade letter and sets the age to 0.
I can't explain why it does this, you'll have to wait for someone more experienced to do that but a solution is to change: char gradeLetter[1]; // which is pointless, why make an array of 1 element when you could just create a char?
to char gradeLetter;
and change cin.getline(person.gradeLetter, 1); to cin >> person.gradeLetter;
You know you shouldn't be accessing members like that don't you?
An array of size 1 is not big enough to hold the letter grade plus the newline character, so it is left in the buffer. Then cin gets the newline character and send it to age, effectively skipping the input prompt. Either make your array size two, or insert a cin.get() statement after line 20. additionaly line 22 should look like this:
You know you shouldn't be accessing members like that don't you?
Please explain, I'm very new to C++.
Also, how would I make it so the program adjusts the grade downward—that is, up one letter.
Hence, if I say that I deserve a grade of "A", in the output it will show "B" instead.
You could look for a list of ASCII values and adjust from there (google: ASCII table).
For example, if A was 10 and B was 11, then you would just add 1 to the gradeLetter unless the user enters Z which you need to prevent the letter from downgrading anymore or it might turn into a random character like &.
mcleano:
You know you shouldn't be accessing members like that don't you?
Please explain, I'm very new to C++.
Well, I'll show using an example how you should do it, but i'll use class' instead of structs as that is more standard in c++. A class in c++ is basically the same as a structure except that members are private by default, whereas they are public by default in structres. Also, class' aren't available in C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class form {
// these variables are private by default up to the line "public: "
char firstName[20];
char lastName[20];
char gradeLetter;
int age;
public:
void set_firstName("a variable passed to this function") { // function code goes here; }
void set_lastName("a variable passed to this function") { // function code goes here; }
// and so fourth ...
void get_lastName() { // function code goes here; }
void get_firstName() { // function code goes here; }
// and so fourth...
} person;
You make the variables you need in your structure private so that nothing in your program can access them other than members of the same class.