Thread: Anybody fix this simple code?

  1. #16
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Killinger
    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.
    Well, if someone could see what you have now, maybe someone could help.

    Regards,

    Dave

  2. #17
    Registered User
    Join Date
    Aug 2004
    Posts
    8
    Sorry Dave. I just updated the code.

  3. #18
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Killinger
    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.
    Oh, I think I see. You are editing and re-editing your original post. That's kind of confusing since I copied it to my local directory and have been looking at your reported problems.

    OK, Just a minute ago I downloaded from your original page, and when I compile it I get

    Code:
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    z.cpp:
    Warning W8060 z.cpp 28: Possibly incorrect assignment in function main()
    Warning W8060 z.cpp 32: Possibly incorrect assignment in function main()
    Warning W8060 z.cpp 55: Possibly incorrect assignment in function main()
    Error E2451 z.cpp 57: Undefined symbol 'ynbeer' in function main()
    Warning W8060 z.cpp 59: Possibly incorrect assignment in function main()
    Error E2451 z.cpp 61: Undefined symbol 'shop' in function main()
    Warning W8060 z.cpp 63: Possibly incorrect assignment in function main()
    Error E2451 z.cpp 65: Undefined symbol 'outside' in function main()
    Error E2238 z.cpp 73: Multiple declaration for 'ynbeer' in function main()
    Warning W8060 z.cpp 79: Possibly incorrect assignment in function main()
    Warning W8060 z.cpp 83: Possibly incorrect assignment in function main()
    Error E2238 z.cpp 92: Multiple declaration for 'shop' in function main()
    Error E2238 z.cpp 98: Multiple declaration for 'outside' in function main()
    Warning W8004 z.cpp 103: 'location' is assigned a value that is never used in function main()
    Warning W8004 z.cpp 103: 'alive' is assigned a value that is never used in function main()
    *** 6 errors in Compile ***
    Now, I usually work through the list including resolving (or at least understanding) warnings as well as errors. But, this time only, I will look at the errors:

    Error #1:

    Undefined symbol 'ynbeer' in function main()

    You have a statement
    Code:
    ynbeer;
    (Notice: this is now an undefined symbol, not an undefined label.)

    The C compiler generates code to find the value of some variable ynbeer, (then throws it away, since you don't assign it to anything or use it as an argument to a function).

    1. I am quite sure this is not what you meant to do.

    2. The compiler doesn't care what you meant to do, but it must know what "ynbear" is, and where it is. So it can't do anything unless you have a definition (like int ynbear; somewhere, for example).

    Putting int ynbear; somewhere before it is referenced will eliminate the compiler error, but the program won't do anything useful with that.

    Just because someone tells you how to eliminate a compiler error doesn't mean that the job is done. Lot's of very bad programs compile with no warnings, no errors.

    What do you really want to do with your C++ program? (That's a rhetorical question --- ask and answer it yourself. Then ask someone here for specific answers to language or compiler errors.)

    Regards,

    Dave
    Last edited by Dave Evans; 08-07-2004 at 10:36 PM.

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    @ Killinger
    You're writing way too much code between pressing compile, and as a result you're being swamped with lots of errors.

    Write the program in a series of small steps, then when you get some errors, you'll know its something to do with the 5 lines you just added rather than the 50 lines you just added.
    After every few of these compiles, try running the code as well to make sure its still working. If you break something, make sure you fix it before adding yet more stuff.

    1. Prove you got the compiler installed and you chose the correct project type
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        return 0;
    }
    2. Simple input output
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        cout<<"Welcome to ShortSword!"<<endl;
        cout<<"Where do you want to travel too?"<<endl;
        cout<<"The Bar - 1"<<endl;
        cout<<"The Shop - 2"<<endl;
        cout<<"Outside - 3"<<endl;
        cout<<"\n"<<endl;
        int traveln;
        cin>>traveln;
        cout<<"Going to "<<traveln<<endl;
        return 0;
    }
    3. Add a function
    Code:
    #include <iostream>
    using namespace std;
    int menu ( void );
    int main ( ) {
        cout<<"Welcome to ShortSword!"<<endl;
        int where = menu();
        return 0;
    }
    
    int menu ( void ) {
        cout<<"Where do you want to travel too?"<<endl;
        cout<<"The Bar - 1"<<endl;
        cout<<"The Shop - 2"<<endl;
        cout<<"Outside - 3"<<endl;
        cout<<"\n"<<endl;
        int traveln;
        cin>>traveln;
        cout<<"Going to "<<traveln<<endl;
        return traveln;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Declare the functions outside of main, as in above it. The compiler sees the function prototypes, and when it looks for the definition, it cant find them because its looking in 'int main()'

    And if they in proper place... I dunno, try getting rid of the semi colons, and try toying with it until it works.

  6. #21
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    I was feeling generous. This code COMPILES, but there are a lot of errors with the way things are handled that I took from your code. For example, you need to work on handling things in the bar, you need to work on code to handle moving around.. essentially, you have to finish the program.

    Hope this helps you on your way.



    Edit note: I changed "using namespace std;" to just putting std:: in front of every command, because, as I've learned from this board, it's better to be more specific than less when working with namespaces.



    Code:
    // Includes
    
    #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()
    {
    	if (strcmp(location, "T") == 0)
    	{
    		std::cout << "You are in The Town" << std::endl;
    	}
    
    	if (strcmp(location, "B") == 0)
    	{
    		std::cout << "You are in The Bar." << 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;
    
    	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();
    	}
    
    	if (choice == 2)
    	{
    		shop();
    	}
    
    	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::cin >> choice;
    	
    	if (choice = 1)
    	{
    		std::cout << "Aye, lad! Good choice! Here ya go!" << std::endl;
    	}
    	
    	if (choice = 2)
    	{
    		std::cout << "Fine then! See that I care!" << std::endl;
    	}
    
    	else
    	{
    		std::cout << "Ummm, please choose somethin' matey!" << std::endl;
    	}
    
    	return(0);
    }
    
    int shop()
    {
    	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;
    
    	// Write your code to handle the answer here.
    
    	return(0);
    }
    
    int outside()
    {
    	std::cout << "Under Construction" << std::endl;
    
    	return(0);
    }
    Last edited by Lithorien; 08-08-2004 at 02:34 PM.

  7. #22
    Registered User
    Join Date
    Aug 2004
    Posts
    8
    Thanks alot! The only reason I didn't compile it was because I had functions that didn't do anything and it would give me errors. Sorry, I will be more careful next time and compile more. Cheers!
    No more compiling errors, but now it always reads the first choice. Like if I inputed 2 or 3, it will always read 1 (except on traveln). Also the location isn't working very well, but thats not a big problem right now. Updated the code again!
    Last edited by Killinger; 08-08-2004 at 04:30 PM.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The only reason I didn't compile it was because I had functions that didn't do anything and it would give me errors.
    You can always create dummy functions just to get it to compile:
    Code:
    int foo( );
    int bar( );
    int baz( );
    
    int main( void )
    {
        ...doing stuff...
    }
    
    // create a dummy function just so we can compile
    int foo( ) { return 0; }
    int bar( ) { return 1; }
    int baz( ) { return 2; }
    Just something simple that meets the bare requirements of the function prototype, so you can compile and test portions at a time. Then later, you can go and write foo to do whatever it was supposed to, then compile and test, and move along to the next.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Killinger
    No more compiling errors, but now it always reads the first choice. Like if I inputed 2 or 3, it will always read 1 (except on traveln). Also the location isn't working very well, but thats not a big problem right now. Updated the code again!
    That would be my fault for not reading what I was writing better.

    Code edits in red.

    Code:
    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::cin >> choice;
    	
    	if (choice == 1)
    	{
    		std::cout << "Aye, lad! Good choice! Here ya go!" << std::endl;
    	}
    	
    	if (choice == 2)
    	{
    		std::cout << "Fine then! See that I care!" << std::endl;
    	}
    
    	else
    	{
    		std::cout << "Ummm, please choose somethin' matey!" << std::endl;
    	}
    
    	return(0);
    }

  10. #25
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I think there are some error in your choice statements:

    Code:
    if(choice = 1)  //should be change to : if(choice == 1) , "==" instead of "=".

  11. #26
    Registered User
    Join Date
    Aug 2004
    Posts
    8
    Thanks for your replys! I dont think I'll bug you guys any more with questions....for now

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