Original Post
Hi guys, I’m really stumped on this one exercise in my book (C++ Primer 4th ed.). I’ve got everything working except one part. It’s so easy but for some reason I can’t figure it out to save my life. Here’s the exercise: P.S Yes, I know I should use string instead of char, but I'm trying to stick with the flow of the book. So, please don't flame me for using char instead of string. :P
Quote:As I said, I’ve got everything working except one part – the array. I can’t figure out how to increment the array. I’ve tried using the + operator in a billion different ways, pointers, typecasting – nothing seems to work. I've been able to increment arrays with integers but it seems I suck with char. :( Here’s the output I get now:
Write a C++ program that requests and displays information as shown in the following example of output. Note that the program should be able to accept first names of more than one word. Also note that the program adjusts the grade downward, that is, up one letter. Assume the user requests an A, B, or C so that you don't have to worry about the gap between a D and an F. What is your first name? Betty Sue What is your last name? Yew What letter grade do you deserve? B What is your age? 22 Name: Yew, Betty Sue Grade: C Age: 22
Quote:Here’s the source:
What is your first name? First Middle What is your last name? Last What letter grade do you deserve? A What is your age? 17 First name: First Middle Last name: Last Grade: - Age: 17
#include <iostream>
using namespace std;
int main()
{
char f_name[80];
cout << "What is your first name? ";
cin.getline(f_name, 80);
char l_name[80];
cout << "What is your last name? ";
cin.getline(l_name, 80);
char grade[3] = {'A', 'B', 'C'};
cout << "What letter grade do you deserve? ";
cin.getline(grade, 80);
char * p = new char[+ 1];
int age;
cout << "What is your age? ";
(cin >> age).get();
cout << "\nFirst name: " << f_name << "\n";
cout << "Last name: " << l_name << "\n";
cout << "Grade: " << *p << "\n";
cout << "Age: " << age;
cin.get();
return 0;
}