Can someone tell me whats wrong?

Started by
11 comments, last by Oni Sephiroth 18 years, 1 month ago
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;
}
Advertisement
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.
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;}


Thanks for your answer btw.
Most likely you want

if (move == "t")

The way you have it move is assigned the value "t"
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?
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...
Quote:Original post by bam104
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?


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 assigningstringA = "stringA test";stringA = stringB;bool IfTest = stringA; // Error, string cannot be converted to boolif (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    ... // Neverif (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!
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]
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.
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 :)

This topic is closed to new replies.

Advertisement