I am making a text based game where the player is displayed with some text and a couple of choices. Here is the script:
#include <iostream>
using namespace std;
void choice1();
int homies;
int main()
{
int choice;
cout << "What's good my homie we is starting now\n\n";
cout << "How many homies you got ";
cin >> homies;
cout << "You was walking down the hood with your " << homies << " homies.\n";
cout << "You spot your rivals.\n What do you do\n 1) Run\n 2) Fight them\n";
cin >> choice;
if (choice == 1)
{
cout << "You run but your rivals shoot you and your " << homies << " homies.\n";
}
else if (choice == 2)
{
cout << "You ain't no wimp. You shoot at them with your " << homies << " homies.\n";
cout << "You win and you gain respect from all your other rivals and people.\n";
choice1();
}
else if (choice >= 3)
{
cout << "Don't play me like that.\n";
}
return 0;
}
void choice1()
{
int choice;
cout << "You just chillin at your crib with your " << homies << " homies.\n";
cout << "All of a sudden a car does a drive by on you and yo homies. What do you do?\n";
}
In the int main() I want to do something that if i choose 1 or 3+, the program goes back to the beginning of int main(). And also the same but if i choose the wrong answer in choice1() it goes back to int main(). I know that there is slang and improper grammar, it's supposed to be like that.
#include <iostream>
bool choice1();
int main() {
int choice;
std::cout << "... (get choice): ";
// set a variable to test if we are quitting yet
bool quit = false;
// while we aren't, loop
while (!quit) {
std::cin >> choice;
switch(choice) {
case 2:
std::cout << "You won\n";
quit = choice1();
default:
std::cout << "You lost, try again\n";
}
}
std::cout << "Finish!";
return 0;
}
bool choice1() {
int choice;
std::cout << "... (get choice)";
std::cin >> choice;
// while invalid choice options
while (choice < 1 || choice > 2) {
std::cout << "Invalid option. (get choice):"
std::cin >> choice;
}
if (choice == 1) {
std::cout << "You lost...\n";
returnfalse; // signal a loss
} else {
std::cout << "You Won!\n";
// either keep adding choices or do this to finish:
returntrue;
}
}
However, as you can see, this requires you to copy a bit of code every time you add some more to the program. A better way would be a data-driven approach: Have a function to do all of this by taking in a list of options, the messages for each option and the ID of the next option to go to, which can all be accessed through an array.
What do you mean your question isn't solved yet? Did you try my code? You could probably move the display for the text inside the while loop, but apart from that and changing the text to what you wanted that is exactly what you wanted (unless I misunderstood your question).
Sorry, I forgot to explain. I tried your code bit the thing is that I can keep typing a choice and the answer will keep popping up. Another Thing is that when I type a correct choice,, it displays the text the text for both the right and wrong choice
I figured this out myself now. If someone reading this is having the same problem, Create a function. IF you don't know how to do that then this is how.
#include <iostream>
using namespace std;
void function1(); //You need this for your function to work
int main()
{
cout << " Stuff happens.";
function1(); //tells the computer to do what is in that function
return 0;
}
void function1() // The actual function and what it does
{
cout << " This is function1() being displayed now";
}
This thread is terrible. If someone reading this is having the same problem, then you won't find any useful answers here.
@ OP: That is a terrible explanation of functions because it doesn't explain anything. For instance WHY do you need Line 5? Would it work if you put it higher in the file? You were using a function before that post, so what you have typed doesn't make any sense.
@ Little Bobby Tables: Yes, in this context he does so need Line 5. Otherwise the function would need to be defined before it is called.
@ NT3: Your program flow has redundancies and you forgot your "break" statement at the end of your first switch case.
i tried to help him more but i didnt understand what op was asking. and i was more making the point of how he didnt explain prototypes. i wasnt critiquing his code. and actually the function does work without line 5. the compiler just cant find it.