So, Daniweb. My teacher has been teaching us some old C++ (a problem I've been trying with little avail to rectify). I need y'all wonderful people to look over a section of code that I'm having trouble with. It has to do with recognizing a letter in an if statement. The problem is that I can't get it to recognize Y as a valid entry, so it always just skips to the subsequent else. Halp pluzz?

It's part of a menu that determines answers for volume and area problems. This one is for the volume of a cylinder (area of base*height). And the braces are all in the right places, it's just nested like 2349087 times.

Choice3 is also declared as a char in case you saw that.

else if (choice2==4)										
		{
			cout<<"Enter the height: ";
			cin>>height;
			cout<<"Do you know the area of the base? ";
			cin>>choice3;
			if (choice3=='89')
			{
				cout<<"Enter the area of the base: ";
				cin>>basearea;
			}
			else
			{
				cout<<"Enter the radius of the base: ";
				cin>>radius;
				basearea=pi*pow(radius,2);
			}
			volume=basearea*height;
			cout<<"The volume is "<<volume<<" units cubed."<<endl<<endl;
		}

Word of the day: Sloppy
/slop.pee/ adj. Me.

Recommended Answers

All 10 Replies

>>if (choice3=='89')

'89' is not 'Y'. There is no such thing as 89 within single quotes. Your compiler should have given you an error or warning on that, which I have to assume you ignored. if(choice3 == 'Y') is what you want. But what if you type 'y' instead of 'Y'?

Couple things:
According to my book/teacher (and in the past my compiler), a single-letter string can be represented by the ASCII dec number in single quotes. It actually gives me an error if I put what you suggested, and removes it when I replace the letter with the ASCII code. I also had it originally as (choice3=='Y'||choice=='y'), so just putting an || in makes it no big issue, no matter how the rest of the code winds up.

The ASCII for a lowercase y is 121 I think. I'll look it up if I must, but not now.

>>According to my book/teacher (and in the past my compiler), a single-letter string can be represented by the ASCII dec number in single quotes

Either you mis-understood your teacher or your teacher needs to quit teaching and take up basket weaving. The decimal value of ascii codes are NOT put in quote

>>It actually gives me an error if I put what you suggested
Post what you tried because it seems to work for everyone else in this world (roughly 4 billion people) but not for you ;)

If at all u want to use the ASCII equivalent of a letter, it should be like this:

atoi('89');

If at all u want to use the ASCII equivalent of a letter, it should be like this:

atoi('89');

No, it shouldn't. '89' does not have a well-defined meaning in standard C++, so if your compiler accepts it, it's a local extension.

If you want the character that is equivalent to the decimal value 89 in your machine's character set, you can write char(89) or, more pedantically, static_cast<char>(89) .

First off, Ancient, it surprises me that you've managed to get to 19,000 posts and your standing in here while still remaining such an ass. Amicability shouldn't be countered with such bitchiness. Please feel free to just completely ignore ANY of my posts you ever see.

Minirant done, this is where the fact that she's teaching us old C++ comes in. The compiler can't accept any of those commands because it's so frackin' old. Most likely pre-standard. I guess until I finally take up the overhaul of the C++ course myself, because the school is doing nothing about it, I'll have to rely on my teacher for advice. Thanks anyway, guys :[

Edit: The copyright date for this compiler is '94-'98. So that gives you some idea.

>>First off, Ancient, it surprises me that you've managed to get to 19,000 posts and your standing in here while still remaining such an ass. Amicability shouldn't be countered with such bitchiness. Please feel free to just completely ignore ANY of my posts you ever see.

After a comment like that, I wouldn't be surprised if more users "feel free to just completely ignore ANY of my posts you ever see".

The simple fact of the matter is, this statement:

a single-letter string can be represented by the ASCII dec number in single quotes

is WRONG. Either you misinterpreted something or it's a typo. If the teacher is too dense to acknowledge, accept, and correct that fact, they shouldn't be teaching.

The proper forms of the statements are:

//declaration / initialization
char someVar1 = 89;
char someVar2 = 'Y';

//conditionals
if (someVar1 == 'Y')
  cout << "True.";

if (someVar2 == 89)
  cout << "True.";

You can only use 'single quotes' around ONE literal character. If more than one character is desired, you need a compatible data type (which a plain ol' char is not) and "double quotes" must be used. For numeric values, you do not use anything.

Now if Ancient had actually offered me that advice, instead of assuming my stupidity and throwing in some admittedly talented and biting sarcasm, I wouldn't have construed him as a dick.

But that's exactly what I need, and I did misinterpret it, so props to you for figuring it out.

Instead of just bitching about what I posted why don't you try actually reading and comprehending it. I told you several hours ago what the problem was with your code. It's not my fault you are apparently too dense to understand my first post. Learn to use your head for something other than a hat rack.

Post what you tried because it seems to work for everyone else in this world (roughly 4 billion people) but not for you ;)

Its actually more like 6.5 billion people.. The worlds population was at around 4 billion in the 1970s.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.