Sign in to follow this  

Incrementing a string array

This topic is 4598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
Quote:
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
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:
Quote:
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
Here’s the source:
 #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;
}
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

Share this post


Link to post
Share on other sites
Never mind. Try this instead.

char grade[3] = { 'A', 'B', 'C' };
char in[80];

cout << "What letter grade do you deserve? ";
cin.getline(in, 80);
char *p = new char(in[0]+1);

Share this post


Link to post
Share on other sites
char *p = new char[grade+1];

This initializes p to point to an array of (grade + 1) chars.

------

char *p = new char(grade+1);

This initializes p to point to one char with value (grade + 1).

Share this post


Link to post
Share on other sites
Couple things:
  • If the student can only enter a single letter grade as desired, why store it in an 80-character array as opposed to a single char?

  • If you're going to manipulate the student-specified grade, why have a local static array ({'A', 'B', 'C'}) at all?

  • In addition to the above, why dynamically allocate (and then forget to release) a single character when you're just going to modify the student-supplied value?
    char grade;
    cout << "What grade do you deserve? ";
    cin >> grade;
    cin.ignore(80);
    ...
    cout << "Grade: " << char (grade + 1) << endl;

Share this post


Link to post
Share on other sites
Looking at the problem that was assigned I don't think an array is required. Since they say not to worry about the gap between D and F, I think they are trying to signal the use of the ascii sequence. (Since 'A'=65,'B'=66,....)

So to do this, just input the character

char grade = cin.get();

then the grade they deserve is (grade+1). (make sure to use a static_cast and not to output as an integer but as a character)

-TOmcAT

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Couple things:
  • If the student can only enter a single letter grade as desired, why store it in an 80-character array as opposed to a single char?

  • If you're going to manipulate the student-specified grade, why have a local static array ({'A', 'B', 'C'}) at all?

  • In addition to the above, why dynamically allocate (and then forget to release) a single character when you're just going to modify the student-supplied value?
    char grade;
    cout << "What grade do you deserve? ";
    cin >> grade;
    cin.ignore(80);
    ...
    cout << "Grade: " << char (grade + 1) << endl;


Looks down at the carpet.
I can't believe I didn't think of it like that. Meh, I've got a looooooong way to go in my thinking programming skills (let alone programming skills). :(

Thanks so much guys you all helped out a lot today. Now please excuse me while I go bash my head a few times on the carpet...better yet let me go outside where there's concrete. :)

Thanks again.

Share this post


Link to post
Share on other sites
As general rules:

- Don't dynamically allocate things unless it becomes apparent that you need to.
- Pay attention to the types of things. If you want to read in one character, then it goes into a char.
- Don't just "try every combination of things"; when one of them doesn't work, figure out what it was doing, and think about how to fix it.
- Don't feel that you need to store the result of every calculation; become comfortable with writing "anonymous" expressions (i.e. in-line in the output statement). (For outputting the name in appropriate format, realize that you don't need to concatenate the strings even when you output; you can just output them in the appropriate order, inserting the ", " as needed.)

In your code, you allocate an array of one character ("+ 1" is just 1), and then output the character at the beginning of that array later (by dereferencing the pointer to get that character).

Share this post


Link to post
Share on other sites

This topic is 4598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this