Else statement showing up upon making choice in choiceOne_Path

The else statement in the second choiceOne_Path (the walking bit) is showing up upon choosing 3. I am extremely new to coding and barely know what I'm doing.

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
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;

int main()
{
    system ("CLS");
    system ("color 02");
    int choiceOne_Path;
    char name [50];
    cout << "Input name."<<endl<<flush;
    cin.getline(name, 50);
    system ("CLS");
    cout << "Input received. Welcome, " << name << "." << endl<<flush;
    cout << "Are you a boy or a girl?" << endl;
    cout << ">> Enter '1' for boy." << endl;
    cout << ">> Enter '2' for girl." << endl;
    cout << ">> Enter '3' for unknown organism." <<endl;
    cin >> choiceOne_Path;
    system ("CLS");
    if(choiceOne_Path == 1)
    {
        cout << "I see... so you are a boy!" << endl;
    }
    else if(choiceOne_Path == 2)
    {
        cout << "I see... so you are a girl!" << endl;
    }
    else if (choiceOne_Path == 3)
    {
        cout << "I see... so you are an unknown organism!" << endl;
    }
    else
    {
        cout << "No! You are doing it wrong! Please try again." << endl;
    }
    cout << "You are walking." << endl<<flush;
    cout << ">> Press '1' to turn left." << endl;
    cout << ">> Press '2' to turn around." << endl;
    cout << ">> Press '3' to continue on forward." << endl;
    cin >> choiceOne_Path;
    if (choiceOne_Path == 1)
    {
        cout << "You successfully turn left!" << endl;
        cout << "..." << endl;
        cout << "Nothing happens." << endl;
        return 0;
    }
    if (choiceOne_Path == 2)
    {
        cout << "You successfully turn around!" << endl;
        cout << "..." << endl;
        cout << "Nothing happens." << endl;
        return 0;
    }
    if (choiceOne_Path == 3)
    {
        cout << "You successfully continue walking forward." << endl;
        cout << "I don't know what happens next." << endl;
        cout << "You win the game!" <<endl;
    }
    if (choiceOne_Path == 4)
    {
        cout << "You successfully turn right!" << endl;
        cout << "You find the key." << endl;
        cout << "You win the game!" << endl;
    }
    else
    {
        cout << "You successfully don't turn at all!" << endl;
        return 0;
    }
    _getch();
    return 0;
}
Hello bubby,

Welcome to the forum.

From line 43 on you have separate if statements, so when you get to the last if statement if "choiceOne_Path" is not equal to 4 the else is executed. What you want is make lines 50, 57 and 63 "else if" statements like you did in the first section.

As "else if" statements line 69 will only execute if nothing else matches "choiceOne_Path".

Hope that helps,

Andy
Hello bubby,

For being "extremely new to coding" I will point out some things that will help.

In you header includes "conio.h" is a file that is no longer available to everyone. This file is considered out dated and no longer included in newer versions of the compilers as a standard include file. Using this header file along with "_getch()" for your own use is OK, but makes the code non-portable.

"stdlib.h" is still used and is OK.

Try to avoid using using namespace std; in your programs it may seem easy now, but WILL get you in trouble some day.

It is better to learn to qualify what is in the standard name space with "std::" and then to learn what is in the standard name space now while it is easy. And not all at once to try to keep a job.

What you are most likely to use for now is "std::cout", "std::cin" and "std::endl". About a week or so of typing this and you will not even notice that you are doing it.

It is best not to use "system" anything in a program as this could leave the program vulnerable to attack by hackers who can use this. Also it tends to be specific to Windows and not everyone can use it. If it is your own program and only you will be using it that is OK, but do not let it out to everyone. An alternative you can use is:
1
2
3
4
5
//  This next line may not be needed. If you have to press enter twice put a comment on the line.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
//  Sometimes it is needed.
std::cout << "\n\n Press Enter to continue";
std::cin.get();



It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with and do not need to be initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of
int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.

Line 11. You should include the header <string> and make "name" a "std::string".

Line 12 I find it better to write this line as cout << "\nInput name: ";. The "\n" at the beginning helps to break up the output and make it easier to read. I end the prompt with a ": ";, so that the following "cin >>" is on the same line. I think it just looks better this way on the display.

Line 17, If you make "name" a "std::string" this would be written as: std::getline(std::cin, name);.

I will say this here, the use of "flush" does not work or is it necessary. The use of "endl" will write a new line to the output buffer and then calls "flush" to empty the buffer. Your use of endl << flush;is useless because the "endl" has already flushed the buffer, so the "flush" has nothing to flush.
See: http://www.cplusplus.com/reference/ostream/endl/

In the second set of "if else" statements you end several blocks with "return 0;". This may be what you want, but you are leaving the program early. For my setup as with many other people once the program ends the console window closes making it hard to read any last words sent to the screen. You have used "_getch()" to keep your window open by pausing the program, but you never reach this code unless you choose 4 that is not known about.

Do not be afraid to experiment with the use of '\n's in the "cout"s. This will add some blank lines to the output and help make the screen easier to read. This also helps with the "endl"s to where I try to keep the "endl" for the last "cout".

In your code the {}s line up in the same column which makes them easier to find and along with the proper indenting, as you have, it makes your code easier to follow. All you need now is some blank lines to break up the code and you are on your way to a good style.

I just happened to think about the "_getch()" just before the return. With out a prompt someone who does not know the program will have no idea what to do.

I offer this so you can see what I am talking about:
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
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <limits>

//using namespace std;  // <--- Best not to use:

int main()
{
	system("CLS");
	system("color 02");

	int choiceOne_Path{};
	char name[50]{};

	std::cout << "\nInput name: ";  // <--- std::endl and std::flush not needed. Since they do the same thing there is no need to use them together.
	std::cin.getline(name, 50);

	system("CLS");
	
	std::cout << "\nInput received. Welcome, " << name << "." << std::endl;
	std::cout << "\nAre you a boy or a girl?" << std::endl;
	std::cout << ">> Enter '1' for boy." << std::endl;
	std::cout << ">> Enter '2' for girl." << std::endl;
	std::cout << ">> Enter '3' for unknown organism. ";
	std::cin >> choiceOne_Path;

	system("CLS");

	if (choiceOne_Path == 1)
	{
		std::cout << "\nI see... so you are a boy!" << std::endl;
	}
	else if (choiceOne_Path == 2)
	{
		std::cout << "\nI see... so you are a girl!" << std::endl;
	}
	else if (choiceOne_Path == 3)
	{
		std::cout << "\nI see... so you are an unknown organism!" << std::endl;
	}
	else
	{
		std::cout << "\nNo! You are doing it wrong! Please try again." << std::endl;
	}

	std::cout << "\nYou are walking." << std::endl;
	std::cout << "\n>> Press '1' to turn left." << std::endl;
	std::cout << ">> Press '2' to turn around." << std::endl;
	std::cout << ">> Press '3' to continue on forward." << std::endl;
	//  Missing for choice 4.
	std::cin >> choiceOne_Path;

	if (choiceOne_Path == 1)
	{
		std::cout << "\nYou successfully turn left!" << std::endl;
		std::cout << "..." << std::endl;
		std::cout << "Nothing happens." << std::endl;
		//return 0;  // <--- Ends the program to early.
	}
	else if (choiceOne_Path == 2)
	{
		std::cout << "\nYou successfully turn around!" << std::endl;
		std::cout << "..." << std::endl;
		std::cout << "Nothing happens." << std::endl;
		//return 0;  // <--- Ends the program to early.
	}
	else if (choiceOne_Path == 3)
	{
		std::cout << "\nYou successfully continue walking forward." << std::endl;
		std::cout << "I don't know what happens next." << std::endl;
		std::cout << "You win the game!" << std::endl;
	}
	else if (choiceOne_Path == 4)
	{
		std::cout << "\nYou successfully turn right!" << std::endl;
		std::cout << "You find the key." << std::endl;
		std::cout << "You win the game!" << std::endl;
	}
	else
	{
		std::cout << "\nYou successfully don't turn at all!" << std::endl;
		//return 0;  // <--- Ends the program to early.
	}

	std::cout << "\n\n Press any key to continue: ";  // <--- Added.
	_getch();

	return 0;
}


If you are interested I have two functions to clear the screen and set the screen colors. This may only work on a Windows system.

Hope that helps,

Andy
Topic archived. No new replies allowed.