# Can someone tell me whats wrong?

This topic is 4329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i started a basic text rpg and im getting the error: 't' is undeclared first use this function Im using Bloodshed Dev-C++ as a compiler if that makes a difference. Here is the code:
#include <iostream>
using namespace std;

int main ()
{
bool key = 0;
int gold = 0;
int xp = 0;
int lvl = 0;
int move;

cout << "--------------------------- " << endl;
cout << "Welcome to the Redmage Rpg! " << endl;
cout << "--------------------------- " << endl;
cout << "A Terror Visual C++ Game" << endl;
cout << "By Dan Lodge " << endl;
cout << "--------------------------- " << endl;
cout << "" << endl;
cout << " t = Tavern, d = Dungeon" << endl;
cout << "" << endl;
cout << "move: ";
cin >> move;

if (move = t) {
cout << "You moved to the Tavern\n";
}
else
{
cout << "You do not have the key!/t";
}
;return 0;
}

##### Share on other sites
You declared the variable as the wrong type. Declare it as a string (make sure to #include <string>), and put quotes around the "t" in the if statement.

##### Share on other sites
ok,

now i get:

could not convert (&move)->std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)"t"))' to bool'

New code:
#include <iostream>
#include <string>
using namespace std;

int main ()
{
bool key = 0;
int gold = 0;
int xp = 0;
int lvl = 0;
string move;
string name;

cout << "--------------------------- " << endl;
cout << "Welcome to the Redmage Rpg! " << endl;
cout << "--------------------------- " << endl;
cout << "A Terror Visual C++ Game" << endl;
cout << "By Dan Lodge " << endl;
cout << "--------------------------- " << endl;
cout << "" << endl;
cout << " t = Tavern, d = Dungeon" << endl;
cout << "" << endl;
cout << "move: ";
cin >> move;

if (move = "t") {
cout << "You moved to the Tavern\n";
}
else
{
cout << "You do not have the key!/t";
}
;return 0;
}

##### Share on other sites
Most likely you want

if (move == "t")

The way you have it move is assigned the value "t"

##### Share on other sites
You guys own!
Thanks for such a quick response.

Lastly, when i run the compiled .exe file it closes so quickly after i have pressed "t" that i cant see the message. How do i stop it closing?

##### Share on other sites
Good 'ol command-line closes immediately after it finishes execution, which is a pain. You might want to throw in some basic input in there to force the window to hold still for a bit before it closes itself.

getch() comes to mind...

##### Share on other sites
Quote:
 Original post by bam104You guys own!Thanks for such a quick response.Lastly, when i run the compiled .exe file it closes so quickly after i have pressed "t" that i cant see the message. How do i stop it closing?

std::cin.get(); // right in front of the return
// and also, why is there an empty statement in the front of the return?
// ;return 0; // the first semi does nothing....

And the reason the previous didn't work was because of the string... The '=' operator returns a string (on a string object, that is... on an int for example it returns an int), which cannot be converted to a bool in the if, because the if looks for a bool... It looks something like this:

// Since '=' is assigning
stringA = "stringA test";
stringA = stringB;

bool IfTest = stringA; // Error, string cannot be converted to bool

if (IfTest)
...

While the '==' operator is a comparing, returning a bool:

stringA = "stringA test";

bool IfTest = stringA == stringB;

if (IfTest)
...

This would work and the IfTest would be true if the strings are equal because you are comparing and not assigning.

The bad thing is, that ints and others can be converted to bools... An non-zero int would result in true, and any zero int would be false....

So this would work:

int myInt = 0;
if (myInt) // myInt == 0 == false
... // Never
if (myInt = 1) // myInt = 1 == 1 == true
... // Always

That's why you want to becareful because:
1) myInt would be changed if not supposed to
2) the if would always work

Just some tips :).

Good luck!

##### Share on other sites
well, i tend to use getch() but thats a C style function.

Even so, it still works-

first include conio.h
then before you exit the main function, call getch() and it will wait for some kind of key input.

Wow, GDNet is on fire today [grin]

##### Share on other sites
cin.get(); should also work, though latley it hasn't been working for me..Sysytem ("pause") works too, but it writes a "Press any key to continue." That also requires the STD lib if you decide to use it.

##### Share on other sites
ok new problem,

Error:

}' at end of input

#include <iostream>
#include <string>
using namespace std;

int main ()
{
bool key = 0;
int gold = 0;
int xp = 0;
int lvl = 0;
string move;
string name;

cout << "--------------------------- " << endl;
cout << "Welcome to the Redmage Rpg! " << endl;
cout << "--------------------------- " << endl;
cout << "A Terror Visual C++ Game" << endl;
cout << "By Dan Lodge " << endl;
cout << "--------------------------- " << endl;
cout << "" << endl;
cout << "Where would you like to go?" << endl;
cout << " t = Tavern" << endl;
cout << " d = Dungeon" <<endl;
cout << "" << endl;
cout << "Move: ";
cin >> move;

if (move == "t") {
cout << "" << endl;
cout << "You moved to the Tavern" <<endl;
cout << "" <<endl;
cout << "--------------------------- " << endl;
cout << "The Blue Mermaid is a rough establishment, its wooden floor" <<endl;
cout << "creaks as you enter but no one looks up." <<endl;
cout << "--------------------------- " << endl;
cout << "" <<endl;
cout << "What would you like to do?" << endl;
cout << "s = Speak with the Barkeep" <<endl;
cout << "t = Go to a table" <<endl;
cout << "" << endl;
cout << "Move: ";
cin >> move;

if (move == "s") {
cout << "The Barkeep is short an stout,he looks you up" <<endl;
cout << "and down as you approach then smiles a black" <<endl;
cout << "tooth grin" <<endl;
cout << "d = Ask about the Dungeon" <<endl;
cout << "k = Ask about the Key" <<endl;
cin >> move;

}
else
{
cout << "You do not have the key!/t";
}

std::cin.get();
;return 0;
}

Thanks new comments :)

##### Share on other sites
Can you post the error? I'm not 100% sure, but I think you might be declaring your strongs wrong..lemme check.

##### Share on other sites
i did lolz,

}' at end of input

it claims its the last "}" thats the problem. Line 60.

##### Share on other sites
if (move == "t") {
cout << "" << endl;
cout << "You moved to the Tavern" <<endl;
cout << "" <<endl;
cout << "--------------------------- " << endl;
cout << "The Blue Mermaid is a rough establishment, its wooden floor" <<endl;
cout << "creaks as you enter but no one looks up." <<endl;
cout << "--------------------------- " << endl;
cout << "" <<endl;
cout << "What would you like to do?" << endl;
cout << "s = Speak with the Barkeep" <<endl;
cout << "t = Go to a table" <<endl;
cout << "" << endl;
cout << "Move: ";
cin >> move;

you forgot to close your if statement. Add a "}" to the end of it. I'm not sure if thats it tho..still a bit of a C++ newb myself.