Void functions being wierd

Write your question here.
So for some reason,
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
  #include <iostream>

using namespace std;
string name;

void firstchoice (double firstbranch);
int secondchoice (double secondbranch);
int main()
{
    double choice1;
    double choice2;
    cout << "please put in your name" << endl;
    cin >> name;
    cout << "Sup, " << name << ", get ready for adventures" << endl;
    cout << "\nYOU WAKE UP ALONE AND CONFUSED IN AN ATTIC" << endl;
    cout << "You can see a door, and a bloody note on a paper" << endl;
    cout << "Do you wish to look at door or note?" << endl;
    cout << "1 for door or 2 for note" << endl;
    cin >> choice1;
    firstchoice (choice1);
    cout << "you are now in a beatuful plain, with the door no longer behind you" << endl;
    cout << "you can also see a city in the close distance" << endl;
    cout << "do you want to examine the city or the vehicle?" << endl;
    cout << "1 for vehicle, 2 for city" << endl;
    cin >> choice2;
    secondchoice (choice2);
    if (secondchoice(choice2) == 0)
    return 0;


    cout << "you are in the city" << endl;

    return 0;
}
void firstchoice(double firstbranch)
{
if (firstbranch==1)
        {cout << "door is open" << endl;
        cout << "you walk out into a beautiful plain." << endl;
        cout << "you see a car, and a city near by" << endl;
        }
else if (firstbranch==2)
{
    cout << "you read the note" << endl;
    cout << "the door is open bozo" << endl;
    cout << "you go to the door" << endl;
    cout << "to your surprise, the door is open" << endl;
}
}
int secondchoice (double secondchoice)
    {
        if (secondchoice==1)
        {
            cout << "you check out the vehicle" << endl;
            cout << "It seems to start through a button. /n you press it" << endl;
            cout << "the car springs to live, and it takes you to the city of its own accord" << endl;
        }
        else if (secondchoice==2)
           {

            cout << "you look at the city. you decide to start walking towards it" << endl;
            cout << "you walk and walk for miles" << endl;
            cout << "eventually you collapse" << endl;
            cout << "you lose " << endl;
            return 0;}

    }


Now, the this program is a simple thing illustrating my usage of functions. Using it will currently use two functions, void firstchoice and int secondchoice. Now the problem I am running into is that of getting secondchoice to work. Second choice has 2 possible outcomes, one the player progresses on (which I haven't finished) and second, the player loses. The problem is that no matter which choice is selected, it outputs whats under the two choices twice. I've looked into the problem, and I think it has something to do with the return 0 I've attempted to implement in order to simulate a loss of the game. However, it is a requirement for the program to return a number(in this case im making it zero). Does anyone have a solution that would still make the player lose by returning a value, whilst still solving the problem of doubled outputs?
Last edited on
Though I don't necessarily understand your question, I tried to the best of my abilities to solve it and give you a couple of interesting tools for your game.

Firstly, this line didn't make sense
1
2
    if (secondchoice(choice2) == 0)
    return 0;


Secondly, in order to slow your code down, you can #include <window.h> in your header so you can use system("PAUSE")

I hope that helps!

If you wanted the if statement to be dependent on choice 2, just make if(secondchoice -== 2)
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 "stdafx.h"
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
string name;

void firstchoice(double firstbranch);
int secondchoice(double secondbranch);
int main()
{
	double choice1;
	double choice2;
	cout << "please put in your name" << endl;
	cin >> name;
	cout << "Sup, " << name << ", get ready for adventures" << endl;
	cout << "\nYOU WAKE UP ALONE AND CONFUSED IN AN ATTIC" << endl;
	cout << "You can see a door, and a bloody note on a paper" << endl;
	cout << "Do you wish to look at door or note?" << endl;
	cout << "1 for door or 2 for note" << endl;
	cin >> choice1;
	firstchoice(choice1);
	cout << "you are now in a beatuful plain, with the door no longer behind you" << endl;
	cout << "you can also see a city in the close distance" << endl;
	cout << "do you want to examine the city or the vehicle?" << endl;
	cout << "1 for vehicle, 2 for city" << endl;
	cin >> choice2;
	secondchoice(choice2);
	if (choice2 == 1){
		cout << "You're in the car going 60 mph hour, aren't you glad you didn't walk?" << endl;
			//this is what ever text you want after the player gets in the car
		system("PAUSE");
	}


	cout << "you are in the city" << endl;

	return 0;
}
void firstchoice(double firstbranch)
{
	if (firstbranch == 1)
	{
		cout << "door is open" << endl;
		cout << "you walk out into a beautiful plain." << endl;
		cout << "you see a car, and a city near by" << endl;
		system("PAUSE");
	}
	else if (firstbranch == 2)
	{
		cout << "you read the note" << endl;
		cout << "the door is open bozo" << endl;
		cout << "you go to the door" << endl;
		cout << "to your surprise, the door is open" << endl;
		system("PAUSE");
	}
}
int secondchoice(double secondchoice)
{
	if (secondchoice == 1)
	{
		cout << "you check out the vehicle" << endl;
		cout << "It seems to start through a button. /n you press it" << endl;
		cout << "the car springs to live, and it takes you to the city of its own accord" << endl;
		system("PAUSE");
	}
	else if (secondchoice == 2)
	{

		cout << "you look at the city. you decide to start walking towards it" << endl;
		cout << "you walk and walk for miles" << endl;
		cout << "eventually you collapse" << endl;
		cout << "you lose " << endl;
		system("PAUSE");
		return 0;
	}
}
KillaCosby, about what you said about the line not making sense. The function on option 2 will return a 0. The if statement is so that if that happens, it makes it so that the game will end there, with the selection of #2 for "secondchoice". The problem I was getting was that for whatever choice the player makes for "secondchoice", it displays the cout stuff twice.
Oh! Then you actually have to have it return 2 and not 0 silly :P
Firstly, this line didn't make sense

No offence, but just because something doesn't make sense to you doesn't mean it's wrong. This:
1
2
3
if (secondchoice(choice2) == 0)
return 0;

is perfectly fine (albeit not formatted brilliantly).


Oh! Then you actually have to have it return 2 and not 0 silly :P

wrong. The OP is 100% completely correct when he/she said:
it makes it so that the game will end there
Last edited on
Topic archived. No new replies allowed.