Beginner C++ code will not compile in terminal

Hello, everyone;

Apologies in advance--I am a very new C++ user, and I'm attempting to make a simple, fun code. Theoretically, the inter-actor can input integers and depending on the number input, the code will output the same # of puppies with descriptions about them. When I attempt to build, I get no outputs, however, and the code does not compile (it looks like the compiler just went out to lunch because nothing happens). Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Create class/member functions/data members to showcase puppies
#include "pup_class.hpp" //for Class Declaration


//Create a puppy, set itsAge, have it speak or bark, tell us its age

int main()
{
	int number;

	std::cin >> number;

	switch (number)

		{
			case 1:
			{std::cout << "Just one?\n";
			Puppy Scooby;
			Scooby.SetAge(4);
			Scooby.Bark();
			std::cout << "Scooby is a mystery-solving Great Dane who is " << Scooby.GetAge() << " years old.\n";
			std::cout << "He likes long walks on the beach and foot long sandwiches.\n";


			break;}

			case 2:
			{std::cout << "Yes, there are those fluffy two here!\n";
			Puppy Waddlesworth;
			Waddlesworth.SetAge(3);
			Waddlesworth.Bark();
			std::cout << "Waddlesworth is a corgi who is " << Waddlesworth.GetAge() << " years old.\n";
			std::cout << "Speak, Wadi!\n";
			Waddlesworth.Bark();
			std::cout << "\n";

			Puppy Trix;
			Trix.SetAge(7);
			Trix.Yip();
			std::cout << "Trix is a sheltie who is " << Trix.GetAge() << " years young.\n";
			std::cout << "Say hi, Trix.\n";
			Trix.Speak();
			
			break;}

			case 3:
			{
				std::cout << "The 3 musketeers!\n";
			Puppy Porthos;
			Porthos.SetAge(1);
			Porthos.Bark();
			std::cout << "Porthos here is a bulldog who is " << Porthos.GetAge() << " years old.\n";
			std::cout << "Speak, Porthos!\n";
			Porthos.Bark();
			std::cout << "\n";

			Puppy Athos;
			Athos.SetAge(2);
			Athos.Yip();
			std::cout << "Athos is a collie who is " << Athos.GetAge() << " years young.\n";
			std::cout << "Say hi, Athos.\n";
			Athos.Speak();

			Puppy Aramis;
			Aramis.SetAge(3);
			Aramis.Yip();
			std::cout << "Aramis is a german shepherd who is " << Aramis.GetAge() << " years young.\n";
			std::cout << "Say hi, Aramis.\n";
			Athos.Bark();

			break;
		}

			default:
			std::cout << "I don't think I see more than 3 at the moment. . . \n";
			break;

return 0;
		}}


When I attempt to open it in the terminal with g++ (filename.cpp) and execute it with ./(filename.cpp) the terminal also leaves the cursor simply blinking at me. I'm very confused as to what is happening. Any advice would be much appreciated.
What system? Linux? Or Windows? The first user-facing instruction in your program is cin >> number, so assuming you're actually running your program, you need to enter a number.

PS: The executable file itself normally wouldn't have an extension like .cpp. That's the plaintext source file.
Last edited on
Hello phoenyxfeatherz,

Thank you for using code tags.

Your programs starts with #include "pup_class.hpp" , but with including this header file no one can compile or test your code and no one has any idea what is in that header file that could be causing a problem.

Please include the header file.

In
1
2
3
4
5
6
7
8
9
10
11
12
case 1:
{
    std::cout << "Just one?\n";
    Puppy Scooby;
    Scooby.SetAge(4);
    Scooby.Bark();
    std::cout << "Scooby is a mystery-solving Great Dane who is " << Scooby.GetAge() << " years old.\n";
    std::cout << "He likes long walks on the beach and foot long sandwiches.\n";


    break;
}

I do not think it makes any difference, but "Scooby" is a local variable to the {}s of "case 1" and is destroyed when you reach the "break" and closing }.

Your style of the {}s should be consistent in your use. "main" and the "switch" have 1 style while "case 3" it is hard to tell if the closing } on line 72 belongs with "case 3" or the "switch".

The goal is to make your code easy to read and follow.

Andy
Hi, @Ganado--Unix on Mac; I want the user to be able to input the integer, though, right? So, I don't want that part to be encoded...

Hi, @Andy! Thank you--I'm sorry for not placing that in; here's the header file pup_class.hpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//Puppy Class Declaration
//Create class/member functions/data members to showcase puppies!
#include <iostream>

class Puppy //declare the class
{
public:
	int GetAge(); //accessor
	void SetAge(int age); //accessor
	void Bark(); //general methods want public
	void Speak(); //general
	void Yip(); //general

private:
	int itsAge; //data want private
};

//GetAge, public accessor function returns value of itsAge member

int Puppy::GetAge() //2 colons, name of f(x), parameters
{
	return itsAge;
}

//SetAge definition is a public accessor f(x), and sets itsAge data member (variable)

void Puppy::SetAge(int age)
{
	//set member variable itsAge to a value passed in by parameter age
	itsAge = age;
}

//definition of Bark and Speak method
//returns: void
//parameters: None
//action: Prints "bark" or "Hi, hooman" to screen

void Puppy::Bark()
{
	std::cout << "Ruff!\n";
}

void Puppy::Yip()
{
	std::cout << "Yip!\n";
}

void Puppy::Speak()
{
	std::cout << "'Hi, hooman!'\n";
}


I see what you're saying--I'll try to format my {} to be in-line with the appropriate descriptors; thank you for that.
I want the user to be able to input the integer, though, right? So, I don't want that part to be encoded...
I don't know what you mean by 'encoded'. Your first instruction to the user is a "cin >> number". This will pause the execution of the program until it receives input (through typing a number and striking Enter).
Oh my goodness, yes I see what you've been saying, Ganado--I programmed myself into confusion. Thank you--problem solved. The prompt was indeed waiting for my input (obviously, right?), and I interpreted that as a frozen terminal. I added a line before the std::cin to include a prompt for the user that holds: std::cout << "Tell me how many puppies you see: " << std::endl;

Thank you for your help! Now it runs perfectly.
Hello phoenyxfeatherz,

You wrote:
1
2
3
4
5
6
int main()
{
    int number;

    std::cin >> "Enter a number: ";// <--- The "cin", from the keyboard, ALWAYS needs a prompt.
    std::cin >> number;

This way you are not staring at a blinking cursor wondering what to do.

Leaving off any new line (\n) or "endl" puts the input on the same line as the prompt.

Sorry for the delay. I was out longer than I thought.

Andy
Hello phoenyxfeatherz,

Playing around with your code I reworked it. See what you think.

pup_class.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//Puppy Class Declaration
//Create class/member functions/data members to showcase puppies!

#ifndef PUPPYHPP
#define PUPPYHPP

class Puppy //declare the class
{
    public:
        Puppy(){}  // <--- Default ctor. Must define if you have an overloaded ctor.
        Puppy(int age) : itsAge(age) {}  // <--- Overloaded ctor.

        int GetAge(); //accessor
        //int GetAge() { return itsAge; }

        void SetAge(int age); //accessor
        //void SetAge(int age) { itsAge = age; }

        void Bark(); //general methods want public
        //void Bark() { std::cout << "Ruff!\n"; }  // <--- You could simply do this. And the same for the other 2.
        void Speak(); //general
        void Yip(); //general

    private:
        int itsAge; //data want private
};

//GetAge, public accessor function returns value of itsAge member

int Puppy::GetAge() //2 colons, name of f(x), parameters
{
    return itsAge;
}

//SetAge definition is a public accessor f(x), and sets itsAge data member (variable)

void Puppy::SetAge(int age)
{
    //set member variable itsAge to a value passed in by parameter age
    itsAge = age;
}

//definition of Bark and Speak method
//returns: void
//parameters: None
//action: Prints "bark" or "Hi, hooman" to screen

void Puppy::Bark()
{
    std::cout << "Ruff!\n";
}

void Puppy::Yip()
{
    std::cout << "Yip!\n";
}

void Puppy::Speak()
{
    std::cout << "'Hi, hooman!'\n";
}
#endif // !PUPPYHPP 


main.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//Create class/member functions/data members to showcase puppies

#include <iostream>

#include "pup_class.hpp" //for Class Declaration


//Create a puppy, set itsAge, have it speak or bark, tell us its age

int main()
{
    bool done{};
    int number{};  // <--- ALWAYS initialize all your variables.

    do
    {
        std::cout << "\nEnter a number 1, 2 or 3 (4 to quit): ";
        std::cin >> number;

        switch (number)
        {
            case 1:
            {
                std::cout << "\nJust one?\n\n";

                Puppy Scooby(4);  // <--- Makes use of the overloaded ctor. As an example.

                //Scooby.SetAge(4);

                Scooby.Bark();

                std::cout <<
                    "Scooby is a mystery-solving Great Dane who is " << Scooby.GetAge() << " years old.\n\n"

                    "He likes long walks on the beach and foot long sandwiches.\n";

                break;
            }

            case 2:
            {
                std::cout << "\nYes, there are those fluffy two here!\n\n";

                Puppy Waddlesworth;

                Waddlesworth.SetAge(3);
                Waddlesworth.Bark();

                std::cout <<
                    "\nWaddlesworth is a corgi who is " << Waddlesworth.GetAge() << " years old.\n"
                    "Speak, Wadi!\n\n";

                Waddlesworth.Bark();
                std::cout << "\n";

                Puppy Trix;

                Trix.SetAge(7);
                Trix.Yip();

                std::cout <<
                    "\nTrix is a sheltie who is " << Trix.GetAge() << " years young.\n"
                    "Say hi, Trix.\n\n";

                Trix.Speak();

                break;
            }

            case 3:
            {
                std::cout << "\nThe 3 musketeers!\n\n";

                Puppy Porthos;

                Porthos.SetAge(1);

                Porthos.Bark();

                std::cout <<
                    "Porthos here is a bulldog who is " << Porthos.GetAge() << " years old.\n"
                    "Speak, Porthos!\n";

                Porthos.Bark();

                std::cout << "\n";

                Puppy Athos;

                Athos.SetAge(2);

                Athos.Yip();

                std::cout <<
                    "Athos is a collie who is " << Athos.GetAge() << " years young.\n"
                    "Say hi, Athos.\n";

                Athos.Speak();

                std::cout << '\n';

                Puppy Aramis;

                Aramis.SetAge(3);

                Aramis.Yip();

                std::cout <<
                    "Aramis is a german shepherd who is " << Aramis.GetAge() << " years young.\n"
                    "Say hi, Aramis.\n";

                Athos.Bark();

                break;
            }

            case 4:
                done = true;

                break;

            default:
                std::cerr << "\n     I don't think I see more than 3 at the moment. . . \n";
                break;

                //return 0;  // <--- Never reached because of the "break".
        }

    } while (!done);

    return 0;  // <--- Not required, but makes a good break point for testing.
}


"case 1" is just an example of using the overloaded ctor of the class. You do not have to use it. What you have works fine.

Andy
Hi @Andy, no worries; and thank you for the rework. I think the initialization of the variables is definitely something I need to learn, and the do section is helpful as well. Thank you so much for putting in the work to help me! This has been really beneficial :)
Topic archived. No new replies allowed.