# Simple String Question

Hey everyone, I'm kinda new to this C++ thing, and I've just picked up a book and I'm working through the exercises.

Anyways, one exercise asks me to write a program that will ask the user:

So if the user says "I deserve a B", the computer will output "Grade: C". I am not supposed to use "if" or "switch" as I have not reached that chapter in the book yet

Right now, I'm thinking pointers might be the way to go, but I'm not sure.

Anyways, here's what I've got so far:

--------------------------------------------------------

cout << "What letter grade do you deserve? ";
char * grade = new char [5];
cin >> grade; // IS THIS CORRECT???

cout << "

----------------------------------------------------

Thanks!

• : Hey everyone, I'm kinda new to this C++ thing, and I've just picked up a book and I'm working through the exercises.
:
: Anyways, one exercise asks me to write a program that will ask the user:
:
: "What kind of grade do you deserve in your class", and then, output a grade that is one below the grade inputted.
:
: So if the user says "I deserve a B", the computer will output "Grade: C". I am not supposed to use "if" or "switch" as I have not reached that chapter in the book yet
:
: Right now, I'm thinking pointers might be the way to go, but I'm not sure.
:
: Anyways, here's what I've got so far:
:
: --------------------------------------------------------
:
: cout << "What letter grade do you deserve? ";
: char * grade = new char [5];
: cin >> grade; // IS THIS CORRECT???
:
: cout << "
:
: ----------------------------------------------------
:
: Thanks!
:

[red]First, don't bother to allocate small amounts of memory such as you did with grade -- it isn't work the effort. Next, you can initialize grade all in one statement, but that is completly unnecessary in your program[/red]
[code]
[/code]

The above is not correct. If the value of grade were 'A', then the above line will produce 'A' - 1, which is '@' (google for ascii chart). What you want is to increment the value so that 'A' becomes 'B'. Since grade of a character array, not a single character, you have to reference just the first element.
[code]
[/code]

Summary:
[code]
[/code]

• : Hey everyone, I'm kinda new to this C++ thing, and I've just picked up a book and I'm working through the exercises.
:
: Anyways, one exercise asks me to write a program that will ask the user:
:
: "What kind of grade do you deserve in your class", and then, output a grade that is one below the grade inputted.
:
: So if the user says "I deserve a B", the computer will output "Grade: C". I am not supposed to use "if" or "switch" as I have not reached that chapter in the book yet
:
: Right now, I'm thinking pointers might be the way to go, but I'm not sure.
:
: Anyways, here's what I've got so far:
:
: --------------------------------------------------------
:
: cout << "What letter grade do you deserve? ";
: char * grade = new char [5];
: cin >> grade; // IS THIS CORRECT???
:
: cout << "
:
: ----------------------------------------------------
:
: Thanks!
:

You do not need to allocate memory. Try this instead.
[code]
#include <iostream.h>

int main(void)
{
cout<<"What letter grade do you deserve? ";
";
return 0;
}
[/code]

Steph
• Thanks man! That seems to do the trick. However, there is one thing I am unclear about. Why is it unnecessary for me to declare the members of the char array?

How does the program know that member [0] is B, and [1] is C and so on??

: [red]First, don't bother to allocate small amounts of memory such as you did with grade -- it isn't work the effort. Next, you can initialize grade all in one statement, but that is completly unnecessary in your program[/red]
: [code]
: [/code]
:
: The above is not correct. If the value of grade were 'A', then the above line will produce 'A' - 1, which is '@' (google for ascii chart). What you want is to increment the value so that 'A' becomes 'B'. Since grade of a character array, not a single character, you have to reference just the first element.
: [code]
: [/code]
:
: Summary:
: [code]
: cout << grade << endl;
: [/code]
:
:
• : Thanks man! That seems to do the trick. However, there is one thing I am unclear about. Why is it unnecessary for me to declare the members of the char array?
:

There is no need for your program to know that information. cin will overwrite all the characters of the array with whatever you type, overwriting whatever may already be there. So if you type 'A' the buffer will contain
[code]
buf[0] == 'A'
buf[1] == ''; // string null terminator
[/code]

since characters are nothing more than small integers -- each character has an numeric value -- you can easily change from 'A' to 'B' by simply adding 1 to the 'A'.
• Gotcha, thanks!

: : Thanks man! That seems to do the trick. However, there is one thing I am unclear about. Why is it unnecessary for me to declare the members of the char array?
: :
:
: There is no need for your program to know that information. cin will overwrite all the characters of the array with whatever you type, overwriting whatever may already be there. So if you type 'A' the buffer will contain
: [code]
: buf[0] == 'A'
: buf[1] == ''; // string null terminator
: [/code]
:
: since characters are nothing more than small integers -- each character has an numeric value -- you can easily change from 'A' to 'B' by simply adding 1 to the 'A'.
: