Thread: Anybody fix this simple code?

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    8

    Anybody fix this simple code?

    Hey all, I'm using Visual C++. This code is updated, no errors but it always reads choice 1 except on traveln.
    Code:
    #include <iostream>
    
    // Functions
    
    int stats();
    int traveltown();
    int traveln();
    int ynbeer();
    int shop();
    int outside();
    
    // Global Variables (This is bad code - work to fix it on your own)
    
    bool alive = true;
    char location[4] = "T";
    
    
    int bosses, kills, maxlife, life, damage, gold; // Everything is initilized to NULL initally. This basically equals 0. No need to redeclare in main().
    
    // Code
    
    int main()
    {
    	life = 100;
    	maxlife = 100;
    	damage = 3;
    	gold = 1000;
    	
    	std::cout << "Welcome to ShortSword!" << std::endl;
    
    	while (alive == true)
    	{
    		traveltown();
    	}
    	
    	return(0);
    }
    
    int stats()
    {
    	std::cout << "\n" << std::endl;
    	if (strcmp(location, "T") == 0)
    	{
    		std::cout << "You are in The Town" << std::endl;
    	}
    
    	else if (strcmp(location, "B") == 0)
    	{
    		std::cout << "You are in The Bar." << std::endl;
    	}
    	else if (strcmp(location, "S") == 0)
    	{
    		std::cout << "You are in The Store." << std::endl;
    	}
    
    	std::cout << "Your have killed " << bosses <<" bosses" << std::endl;
    	std::cout << "You have " << kills << " kills" << std::endl;
    	std::cout << "You have " << life << "/"<< maxlife << " life" << std::endl;
    	std::cout << "You do " << damage << " damage" << std::endl;
    	std::cout << "You have " << gold << " gold" << std::endl;
    	std::cout << "Stats appear every time you kill a monster or boss, go into town, go inside a store, or buy something" << std::endl;
    	std::cout << "\n" << std::endl;
    if (strcmp(location, "T") == 0)
    {
    	traveltown();
    }
    else if (strcmp(location, "B") == 0)
    {
    	ynbeer();
    }
    else if (strcmp(location, "S") == 0)
    {
    	shop();
    }
    	return(0);
    }
    
    int traveltown()
    {
    	std::cout << "Where do you want to travel too?" << std::endl;
    	std::cout << "The Bar - 1" << std::endl;
    	std::cout << "The Shop - 2" << std::endl;
    	std::cout << "Outside - 3" << std::endl;
    	std::cout << "\n" << std::endl;
    
    	traveln();
    
    	return(0);
    }
    
    int traveln()
    {
    	int choice;
    
    	std::cin >> choice;
    	
    	if (choice == 1)
    	{
    		ynbeer();
    		(strcmp(location, "B") == 0);
    	}
    
    	else if (choice == 2)
    	{
    		shop();
    		(strcmp(location, "S") == 0);
    	}
    
    	else if (choice == 3)
    	{
    		outside();
    	}
    	
    	else
    	{
    		std::cout << "Invalid input. Please put in a correct character" << std::endl;
    		traveln();
    	}
    
            stats();
    
    	return(0);
    }
    
    int ynbeer()
    {
    	int choice;
    
    	std::cout << "Bartender: Watcha want, kid? We got some mighty fine beer on sale. It'll fix ya right on up! It's only 100 gold! You want some?" << std::endl;
    	std::cout << "Yes - 1" << std::endl;
    	std::cout << "No, I just want to look around - 2" << std::endl;
    	std::cout << "No, I'm leaving! - 3" <<std::endl;
    	
    	std::cin >> choice;
    	
    	if (choice = 1)
    	{
    		std::cout << "Aye, lad! Good choice! Here ya go!" << std::endl;
    		life = maxlife;
    		gold = gold-250;
    		stats();
    	}
    	else if (choice = 2)
    	{
    		std::cout << "Fine then! See that I care!" << std::endl;
    		stats();
    	}
    	else if (choice = 3)
    	{
    		std::cout << "See ya later!" << std::endl;
    		traveltown();
    	}
    	else
    	{
    		std::cout << "Ummm, please choose somethin' matey!" << std::endl;
    		ynbeer();
    	}
    
    	return(0);
    }
    
    int shop()
    {
    	int chooseshop;
    
    	std::cout << "Clerk: What can I get for you today?" << std::endl;
    	std::cout << "Healing Potion Cost: 100 gold. Heals you 75 life. - 1" << std::endl;
    	std::cout << "Armor Cost: 500 gold. Less chance of being hurt. - 2" << std::endl;
    	std::cout << "I don't want anything. - 3" << std::endl;
    	std::cin>>chooseshop;
    		if (chooseshop = 1)
    		{
    			std::cout << "One healing potion coming right up!" << std::endl;
    			life = life+75;
    			shop();
    		}
    		else if (chooseshop = 2)
    		{
    			std::cout << "Armor, eh? Here ya go! Now you have a 25% less chance of being hit!" <<std::endl;
                                  // not finished yet
    			shop();
    		}
    		else
    		{
    			std::cout << "Come back any time!" <<std::endl;
    			shop();
    		}
    
    
    
    	return(0);
    }
    
    int outside()
    {
    	std::cout << "Under Construction" << std::endl;
    
    	return(0);
    }
    Last edited by Killinger; 08-08-2004 at 04:29 PM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I can't see a place where you declare the two variables mentioned in the error messages. Even if you're using cin or something like that to get values into the variables, you still need to declare them (i.e. int ynbeer) to allocate space in memory for the data. Hope this helps!

  3. #3
    1479
    Join Date
    Aug 2003
    Posts
    253
    I don't know Visual C++ to well. But from the errors it gave you it looks like you didn't declare a type for traveln and ynbeer. They look like they should be declared of type int. Just add :
    Code:
    int ynbeer;
    int traveln;
    somewhere in your code. Be carefull where you put it because it might mess with scoping. Hope this helps
    Knowledge is power and I want it all

    -0RealityFusion0-

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    As far as scoping - I would recommend just declaring them where you declare all your other variable right at the beginning of main(), especially if this code file is your entire project.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    8
    Thanks for your help! But another problem arises... ( i hate being a newb) I added those in my code in good spots (mind you) but it gave me more errors! It looks really simple (again) Here they be....
    --------------------Configuration: ShortSword - Win32 Debug--------------------
    Compiling...
    ShortSword.cpp
    C:\Borland\Bcc55\bin\ShortSword.cpp(102) : error C2094: label 'traveln' was undefined
    C:\Borland\Bcc55\bin\ShortSword.cpp(102) : error C2094: label 'outside' was undefined
    C:\Borland\Bcc55\bin\ShortSword.cpp(102) : error C2094: label 'shop' was undefined
    C:\Borland\Bcc55\bin\ShortSword.cpp(102) : error C2094: label 'bar' was undefined
    Error executing cl.exe.

    ShortSword.exe - 4 error(s), 0 warning(s)


    Now when I put in int bar; at the top again or int anything of those 4 above it gives me the same error message!
    Last edited by Killinger; 08-07-2004 at 09:04 PM.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    What's mean "label" in error message?
    I donn't know, because I learning vc++ ,not bc++.
    but I think you can change it from "label" to "function".
    this is my assume code:

    Code:
    #include <iostream>
    using spacename std
    // declaration these functions prototype before main()
    
    int traveltown();
    int gotravel();
    int Bar();
    int Shop();
    int Outside;
    
    int main()
    {
       ........
       ........
       // delete "goto", "goto bar" change to "bar" ,etc.
    }
     
    int traveltown()   //add "()"
    {};                     //maybe not add ";" after {} ?
    int gotravel()
    {};
    int Bar()
    {};
    int Shop()
    {};
    int Outside()
    {};

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    No. Label is the term the compiler using to refer to any name you've given something. It doesn't know if it's supposed to be a variable or what, but if it's not in it's little "dictionary", then undefined label is the term it'll use. Post your code. It sounds to me like you just forgot a couple of semicolons and that's making the problem spillo ver into other veriables. (You just need to post the part of your code where you're declaring all your variables I think).

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ sean_mackrory :

    Thanks for your explainning about "label" , I am a newbie , some words I cant see,

    but if it's not in it's little "dictionary"
    what 's label "dictionary" ? can you give me a simple example?

    another question:
    that's making the problem spillo ver into other veriables.
    what's mean "spillo ver" ?

    forgive my foolish question.
    Last edited by toysoldier; 08-07-2004 at 09:29 PM.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    8
    spillo ver = spill over. Hehehe.
    Well, that is the whole code so far, lol. Tell me what I should add or just add it in to the code and post it! I updated the code to what it is right now.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    In most cases, if the compiler comes across a word it doesn't recognize, it'll give you an error. It'll recognize words that are reserved (words like int, static, etc...) and the names of functions and variables that you have already declared and / or defined. So in in a sense it does keep a dictionary of those reserved words and all the variables / functions you've declared (i.e. all 'labels'). There are way to make the compiler ignore the fact that it doesn't recognize the word (like using the extern keyword), but in the code posted above, the compiler had no way of knowing how to handle the variable he was referring to, and that's why it threw the error.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ sean_mackrory :

    Oh, thanks , your answer speed is very quickly,
    you must be a c++ guru ,isn't it?
    I think I can learning more knowledge from you.
    Do you access this stupid student?

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    8
    So what would I add and to where?
    *nudge* *nudge* code please!
    Sean, you must know alot of C++. Your really patient with stupid newbs like me!

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Not exactly. I'm patient with polite newbies like you. Take a look at my signature and you'll see that I can get very cynical very quickly. And I wouldn't call myself a C++ guru. There's a lot of people at this board that are a lot better at it than me. I've tried to have a pretty general knowledge of most of the common languages, but there are people here the live and breathe C++ - and they know every detail about the specifications. Once you've done C++ for a little while, thought, you're able to spot problems in code very easily.

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Killinger
    Thanks for your help! But another problem arises... ( i hate being a newb) I added those in my code in good spots (mind you) but it gave me more errors! It looks really simple (again) Here they be....
    --------------------Configuration: ShortSword - Win32 Debug--------------------
    Compiling...
    ShortSword.cpp
    C:\Borland\Bcc55\bin\ShortSword.cpp(102) : error C2094: label 'traveln' was undefined
    C:\Borland\Bcc55\bin\ShortSword.cpp(102) : error C2094: label 'outside' was undefined
    C:\Borland\Bcc55\bin\ShortSword.cpp(102) : error C2094: label 'shop' was undefined
    C:\Borland\Bcc55\bin\ShortSword.cpp(102) : error C2094: label 'bar' was undefined
    Error executing cl.exe.

    ShortSword.exe - 4 error(s), 0 warning(s)


    Now when I put in int bar; at the top again or int anything of those 4 above it gives me the same error message!
    IIn your original post you have statements

    Code:
    		goto bar;
    .
    .
    .
    		goto shop;
    .
    .
    .
    		goto outside;
    .
    .
    .
    		goto gotravel;
    "goto" statements transfer control to a statement with a label. You have no statements with those labels. The compiler told you exactly what it didn't like.

    I am shocked that on one in this group has given you a hard time about using "goto" control statements. I will let someone else do that.

    Regards,

    Dave

  15. #15
    Registered User
    Join Date
    Aug 2004
    Posts
    8
    Alright alright, I know what he said I did take out the goto statements but did everything he said and it didn't work, so I just put the original code back in. Now I took out the goto statements
    and changed the gotravel to traveln (that took out the complaint about that) but it still gives me the other 3 and calls them undeclared identifiers instead of labels.
    Last edited by Killinger; 08-07-2004 at 10:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird error in this simple code! C2248!
    By gross100 in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2005, 01:31 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM