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?
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?"
#include <cstdlib>
#include <iostream>
usingnamespace 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.
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. :)
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
#include <cstdlib>
#include <iostream>
usingnamespace 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;
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;
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;
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.