Text based adventure game(noob)

Hey guys, just started attempting to learn C++ so am an absolute noob when it comes to programming.
i figured id try to create a little text based game where its about the user speaking to the system, bur have been defeated when trying to create a menu line where i want a variety of options where you can press either G or C and the program will then do an action based upon what you have pressed. i heard of writing a switch function but ive had no luck! ive attached below what i have made so far.

Ive heard of a tutorial called hello world, but i cant seem to find any good videos or tutorials for it, does anyone know of a good tutorial for a beginner and could link me to it?

many thanks
sam!

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
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{

  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr);
  cout << "Hello " << mystr << ".\n";
  cout << "Its pretty sad that your talking to a computer isnt it "<< mystr <<" \n ";
  system("PAUSE");
  cout << "id just give up hope now\n";
  cout << "What would you like to do?\n"
          "  [G]ive up\n"
          "  [C]arry on\n"

  cin >> c;
  switch (toupper( c ))
    {
    case 'G':                     return cout "Go back to sleep.";
    case 'C':                     return cout "Brave one arent you?";
    }
  return ;
  }
  
  system("PAUSE");
  return 0;
    
closed account (o3hC5Di1)
Hi there,

Welcome to the forums. As for your question about a good tutorial, this site offers a pretty good one right here: http://www.cplusplus.com/doc/tutorial/ "Hello world" tutorials are usually the first lesson, teaching you how to print "hello world" to the screen.

As for your program:

1
2
3
4
5
cout << "What would you like to do?\n"
          "  [G]ive up\n"
          "  [C]arry on\n"

  cin >> c;


You speak of "c", but you haven't declared it anywhere. In other words, you haven't told the compiler "Yo! Compiler! I'll be using a variable called "c" of type "char", so reserve space for it, mkay?"

So the quickfix:

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
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{

  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr);
  cout << "Hello " << mystr << ".\n";
  cout << "Its pretty sad that your talking to a computer isnt it "<< mystr <<" \n ";
  system("PAUSE");
  cout << "id just give up hope now\n";
  cout << "What would you like to do?\n"
          "  [G]ive up\n"
          "  [C]arry on\n";

  char c; //declare c to be of type char 
  cin >> c;
  switch (toupper( c ))
    {
    case 'G':                    
         cout "Go back to sleep.";
         break;
    case 'C':                     
         cout "Brave one arent you?";
         break;
    }
  return ;
  } //what's this doing here?
  
  system("PAUSE");
  return 0;


Hope that helps. Please do let us know if you have any further questions.

All the best,
NwN
Last edited on
Yeah that helped! allthough you didnt use break? why is that? the code didnt run for me without using break.

im gonna just look for a few tutorials and see if i can adapt them to make anything :)
many thanks
sam
closed account (o3hC5Di1)
Hi there,

Glad that helped. I apologize - I just copied your code and made alterations, but I didn't spot the lack of break-statements. Well done for sorting that one out on your own though. :)

All the best,
NwN
Just my personal opinion here...

Don't make the mistake of spending time making a game in the console. It's perfectly fine making a text-based game, but my personal advice is to use an API designed games, or something that can print fonts etc. at least. I, just like you are doing, once tried making text games in the console and honestly I didn't learn much from it.
Have you got any tutorials on doing that, anything that you know of? ive found video tutorials much better as ive been mainly reading this http://www.cplusplus.com/files/tutorial.pdf and sometimes it gets so samey just reading a book :P


Had some problems again! i placed the error messages i received in the compiler on the lines where i received them any help?!



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
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{

  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr);
  cout << "Hello " << mystr << ".\n";
  cout << "Its pretty sad that your talking to a computer isnt it "<< mystr <<" \n ";
  system("PAUSE");
  cout << "id just give up hope now\n";
  cout << "What would you like to do?\n";
  cout << "  [G]ive up\n"; 
  cout << "  [C]arry on\n"; 

  char c; //declare c to be of type char
  cin >> c;
  switch (toupper( c ))
    {
    case 'G':;
x         return cout "Go back to sleep."; // Error invalid conversion from "void to "int" Error expected ; before string constant
         break;
    case 'C':;
         return cout "Brave one arent you?"; // Error invalid conversion from "void to "int" Error expected ; before string constant
         break;
    }
    system ("PAUSE");
  return 0;
Last edited on
closed account (o3hC5Di1)
Hi there,

There are a few more errors in your code:

1
2
3
4
5
cout << "What would you like to do?\n";  //<-- you end the statement by using a semi-colon
          "  [G]ive up\n"; // <-- so these statements have no effect
          "  [C]arry on\n"; /code]

[code]return cout "Go back to sleep.";


You can't "return std::cout, as you can see in its definition, main() returns an int. You're just wanting to print out the text, not return from the main() funciton, which would effectively end the program.

1
2
3
case 'G':;
x         cout "Go back to sleep."; // Error invalid conversion from "void to "int" Error expected ; before string constant
         break;


All the best,
NwN
i dont quite understand the last part of what you said about the void and int

but i changed the semi colons and added a few extra and have only got 2 errors

could you possibly enter the fix's and then explain them? as im getting quite confused

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cout << "What would you like to do?"
   "  [G]ive up\n"
   "  [C]arry on\n";

  char c = c;
  cin >> c;
  switch (toupper( 'c' ))
    {
    case 'G':;
    return cout "Go back to sleep."
    break;   //Error invalid conversion from "void to "int" // expected ; befire string constant expected ; before break statement has no effect

    case 'C':;
    return cout "Brave one arent you?"
    break; //Error invalid conversion from "void to "int" // expected ; befire string constant expected ; before break statement has no effect

    system ("PAUSE");
  return 0;
closed account (o3hC5Di1)
Hi there,

1
2
3
4
5
6
7
8
9
10
switch (toupper( 'c' ))
    {
    case 'G':;
        cout "Go back to sleep."; //no return
    break;   

    case 'C':;
        cout "Brave one arent you?"; //no return
    break; 
}



return; "returns" a function. A function states its return type in its declarator:

int main() - The int means that main() will be returning an int value. That's why you normally exit main() by stating return 0;.

Now, as for the error:

Error invalid conversion from "void to "int" // expected ; befire string constant expected ; before break statement has no effect


std::cout does not return any value, in which case we declare it as "void". Now, the compiler expects main() to return an int, so it is confused by your statement "return std::cout" and tells you it can't convert a void to an int.

Hope that makes sense.

All the best,
NwN
Topic archived. No new replies allowed.