Jump to content
  • Advertisement
Sign in to follow this  
bam104

Can someone tell me whats wrong?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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;
}


Thanks for your answer btw.

Share this post


Link to post
Share on other sites
Most likely you want

if (move == "t")

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!