C++ Data types

Mar 14, 2015 at 11:39pm
Hi, I was going through a few practice problems and for some reason this one has me kind of "stuck".

Here's the code:

#include <iostream>
#include <string>
using namespace std;

int main() {
*this portion is left black to fix the problem*
int age = 9; // years
double mass = 5.6; // kilograms
char initial = 'B';
string hair = "Longhair";
bool male = false;
cout << age << " " << mass << " "
<< initial << " " << hair << " " << male;
// something's funny! we can fix it: see page 444 of the textbook
}

When I run it on my VM, it's showing the responses with the bool being 0. I've read page 444 but still haven't figured it out. Is the boolean the problem that needs to be fixed? As in, use boolalpha?
Last edited on Mar 14, 2015 at 11:40pm
Mar 14, 2015 at 11:43pm
Maybe it wants you to output yes or no on the male one? in that case do something like -

(male ? "Yes" : "No")

Basically its like a if statement, asking if male is true, output yes, else output no.

Whats the funny thing fix in page 444?
Mar 15, 2015 at 12:29am
I tried that but it still doesn't work :(

I also tried:

cout << boolalpha;

which gave me the word instead of the number but it's still not what they're looking for.

They're saying to refer to page 444 of the "C++ for Everyone" book to figure out the "funny thing" about this problem. But on page 444 it talks about the "Inheritance Hierarchy of Question Types", and mentions the boolalpha stream manipulator so I figured that's what they were referring to as the solution. But I guess I'm still missing something.
Mar 15, 2015 at 1:05am
You will need to explain further what you're supposed to be doing, this is a very simple program and you're not explaining what you're having issues with.. what is it they're looking for?
Mar 15, 2015 at 1:46am
Exactly, it is a simple program and I feel like I should know the solution but I'm not sure what they're looking for because I don't see anything "funny" about the problem.

It is under the topic: "Types", and the information given before the problem is:

C++ has a lot of different data types that serve different purposes. Here are a few of the most important ones. They're ultimately all stored in bits and bytes in memory (a byte is a group of 8 bits).

int: integer between -2,147,483,648 and 2,147,483,647

double: floating-point/decimal number

char: single character (letter, number, punctuation, etc)

string: sequence of characters. (Actually C++ has two kinds of strings, we'll explain more later.)

bool: true/false

Here's an example of using different types:

An example of variables, using my cat.

/*This is where the code is given which leaves a blank space after the line "main(){" and before "int age=9;" to fix the problem and refers to the page 444 in the book*/

The page mentions: At the root of this hierarchy is the "Question" type. A question can display it's text and it can check whether a given response is a correct answer. How the text is displayed depends on the question type. Later in this chapter, you will see some variations, but the base case simply sends the question text to cout. But in the base class, we will simply require that the response match the correct answer exactly. *And so on*
Mar 15, 2015 at 5:21am
You almost had it. Notice what happens when you have

cout << male << endl;

Output: 0

They want you to output False using the (Only god knows why they think this is relevant) boolalpha keyword. What you need is this:

cout << boolalpha << male << endl;
Output: False
Last edited on Mar 15, 2015 at 5:21am
Mar 16, 2015 at 8:38pm
Hey Shamieh,

I've also tried that before and double checked but it's still giving me an error because "bool male" is declared after the portion of space given to fill in the "funny problem". This has to be one of the most frustrating, annoying problems ever.

It's saying:

Did not compile. Error message:

types.cpp:6:25: error: use of undeclared identifier 'male'
cout << boolalpha << male << endl;
^
1 error generated.
Topic archived. No new replies allowed.