I'm New...=(

This is a discussion on I'm New...=( within the C++ Programming forums, part of the General Programming Boards category; int 'function name', then later just type 'function name()' the 'int' depends on what the function should return. The function ...

  1. #16
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    int 'function name', then later just type 'function name()'
    the 'int' depends on what the function should return. The function which divides its argument by 2 should return a 'double', so "double function( double arg)" , the function 'function1' does not return anything (or returns a void) and does not take arguments (or a void argument list), so " void function1( void)".

    Experiment a little....

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    18
    i would experiment if i knew what exactly i was doing. i see you've defined me some function types, but when i went and put in GameStart() instead of GameStart:, my switch said that GameStart was not defined in this section or something like that... also i would like to know why goto's are so looked down upon?

    and then i'd like to solve my string problem(more than fix my goto "problem".

  3. #18
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    i would experiment if i knew what exactly i was doing
    Isn't that a contradiction in itself? Try to write minimal functionality, start with just one function generating screen output, then bit by bit constructing your program. Start from the top: write in 'main' your program in a sort of pseudo-code, this will show the functions you will need. Then write a first function, compile and test. And so forth... step by step you will learn more on functions etc.
    Give it a try, and post your try-outs online.

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    18
    i'll attempt function AFTER my string/array problem is fixed...

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    Your mostly unindented goto-riddled using code is giving everyone headaches.
    I'll bet you they would be able to help you better if you got rid of the goto and used functions/loops instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #21
    Registered User
    Join Date
    May 2008
    Posts
    18
    1. i rly don't understand why goto's are so difficult, or confusing. WHY?
    2.I don't doubt ur not suppose to use goto's but i'm not able to do it any other way...

    so if u wanna explain exactly how i would make a new function, and then refernce/call it. i'd love to do that, but i dunno the syntax of any of it.

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    1) Because you can jump all over the place, forth and back, back and forth. It boggles the mind. Leave that code for some time and come back to it. I bet you'll be confused. Goto really has no restrictions on how it can jump or where. On other other hand, calling a function, you know where it's going to end up and what it's going to do. And loops are restricted so you know exactly where they will start and end.
    2) Functions are one of the first things you will learn. I do suggest you get a good book or tutorial and study it. But for reference, here is how to make a function:

    return_type function_name(argument_type argument_name, etc)
    {
    /* Code goes here */
    }

    Calling a function is as simple as typing the name of the function and (), so for example:
    function_name();
    Would call the function above.

    To properly use a function, you must also have a prototype. A prototype is simply the first part of the function (before the {) and put a ; afterwards. So the prototype for the above function would be:

    return_type function_name(argument_type argument_name, etc);

    These go into header files or at the top of your source file. The prototypes tell the compiler what kind of function it is, what arguments it takes and what it returns.

    Each function needs a return type. The type depends on what you need it to return. There are many types - integral ones such as int, long, short, etc. Floating ones - short, double and characters - char.

    There's a lot that could be explained, but it's just easier if you get a book or find a tutorial.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by thisisurlife08 View Post
    1. i rly don't understand why goto's are so difficult, or confusing. WHY?
    Because it's not structured. Any goto can go anywhere - in your case, you are jumping back and forth in several places. Multiple gotos in the same function that go to more than two labels is typical for "spaghetti programming" - where if you draw lines from the goto to the label it applies to, the drawing looks a bit like a plate of spaghetti [a bit mess].

    goto's are acceptable in special circumstances, typically when you have a function where "things can fail and cleanup is needed", such as:
    Code:
    int func()
    {
       FILE *f1, *f2;
       
       f1 = fopen("somefile", "r");
       if (f1 == NULL) 
       {
          printf("could not open file somefile\n");
          return -1;
        }
       f2 = fopen("otherfile", "r");
       if (f2 == NULL) 
       {
          printf("could not open file somefile\n");
          goto ErrorExit;
       }
       ... 
       if (/*some error happened, eg bad file content*/ )
       {
          goto ErrorExit;
       }
       ... 
    
       close(f1);
       close(f2);
       return 0; 
        
    ErrorExit:
       close(f2);
       return -1;
    }
    [/quote]

    2.I don't doubt ur not suppose to use goto's but i'm not able to do it any other way...

    so if u wanna explain exactly how i would make a new function, and then refernce/call it. i'd love to do that, but i dunno the syntax of any of it.[/QUOTE]


    If you are referring to this:
    Code:
    switch ( menuchoice ) //Allows For Menu Option Selection
      {
      case 1:
        goto GameStart;
        break;
      case 2:
        goto HelpAndGuide;
        break;
      case 3:
        exit(0);
        break;
      default:
        goto GameStart;
        break;
      }
    Then I would suggest that GameStart, HelpAndGuide should be made functions instead of "subroutines called by goto".

    Within the GameStart function, you should use a suitable loop to replace the PlayerInput label and related goto.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    2) Functions are one of the first things you will learn. I do suggest you get a good book or tutorial and study it. But for reference, here is how to make a function:
    Code:
    return_type function_name(argument_type argument_name, etc)
    {
    /* Code goes here */
    }
    As I wrote earlier.. just a small extra suggestion (compare with my "function2" which simply divides by 2):
    Code:
    return_type  function_name(  argument_type argument_name, etc )
    {
        /* Code goes here */
        return variable_of_return_type;
    }
    This is how functions with a void argument list (a function which doesn't take arguments) are called:

    Code:
    switch ( menuchoice ) //Allows For Menu Option Selection
      {
      case 1:
        GameStart();
        break;
      case 2:
        HelpAndGuide();
        break;
      case 3:
        exit(0);
        break;
      default:
        GameStart();
        break;
      }

  10. #25
    Registered User
    Join Date
    May 2008
    Posts
    18
    ok so the prototype of the function needs to be made, and then it has to be called? is that it or are there 3 parts to the function.

    cuz i thought i did it right once and it didn't work.

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    A prototype only tells the compiler the function exists. Therefore the prototype and the real function (the definition) needs to match.
    Then you call the real function. IF there's no prototype, the compiler doesn't know the function exists and barks instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #27
    Registered User
    Join Date
    May 2008
    Posts
    18
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
     int strikes=0; // to tell the plyers how many wrong guesses
     int wordlength; //to tell plyers how long the word is
     char guess; //what the players will be defining to play the game
     string gameword; //the designated word from the leader
     string decision; //string for type of guessing
     string answer; //reference to the answer for the player's guess
     string lost; //choice by player to play again
     int menuchoice; //what type of meanu they want
     string success; //to decide if guessed letter is present
     string name; //Players Name
     string players; //If theres more players
     int p; //number of players
     int lives=10; //number of lives player has
     char astring[30]="-";
    
     system("cls");
     cout<<"Choose A Number.\n"; //Displays Menu Text
     cout<<"1. Play Game\n";
     cout<<"2. Help and Guide\n";
     cout<<"3. Exit\n";
     cout<<"Choice:";
     cin>>menuchoice;
    switch ( menuchoice ) //Allows For Menu Option Selection
      {
      case 1:
        GameStart();
        break;
      case 2:
        HelpAndGuide();
        break;
      case 3:
        exit(0);
        break;
      default:
        GameStart();
        break;
      }
    
    int GameStart();
    { //Labels The Game's Start
    cin.get();
     system("cls");
    p=1;
    int PlayerNames();
    { //Labels Area For Player Identification
    cout<<"Enter Your Name Here:";
    cin>>name;
    cout<<"Welcome "<<name<<"!!\nYou Are Player Number"<<p<<"\n";
    cout<<"More Players?'y/n'"; //Allows For Multiple Player Notification
    cin>>players;
    if (players=="y")
    {
        p=p+1;
        PlayerNames();
    }
    else if(players=="n")
    {
        system("cls");
        cout<<"The Number Of Players Is "<<p<<"\n";
        cout<<"Designate A Leader Of This Game Now.\n"; //Tells Players To Choose A Leader
    }
    }
    cin.get();
       cout<<"For Game Leader:\nEnter Game Word Here:";
       getline(cin,gameword,'\n'); //leader of game enters the word of the game here
       system("cls");
       wordlength=gameword.length(); //stores the length of the word into "wordlength"
        if( wordlength>25)
        {
         cout<<"The Word You Entered Was Too Long For This Game\n";
         cout<<"Please Try Another Word."; //checks to see if word will fit in the string allowed
        }
        else
        {
         cout<<"CONGRATULATIONS! You Have Chosen A Valid Word\n";
         cout<<"Now You Are Ready To Play The Game\n\n\n"; //tells the leader he/she has chosen a valid word, and it's time to play
        }
       cout<<"\nFor Players:\nThe Game Word Has A Length Of "<<wordlength<<" Letters\n\n"; //reveals the length of the game word to the players
    
       for (char w=0;w<wordlength;w++)
       {
           cout<<astring;
       }
     int PlayerInput();
     { //Designates Time Of Game For Player Interaction
       cout<<"\n\nWhat Would You Like To Do?\n";
       cout<<"Type 'Answer' To Try And Guess The Word Or Type 'Turn' To Guess A Letter.";//Asks Player To Try Choose Between Guessing A Letter Or The Word
       cin>>decision;
        if (decision=="turn")//Goes Here If Player Wants To Guess Letters
        {
           system("cls");
           cout<<"Type Your Single Letter Guess Here:";
           cin>>guess;
           success="n";
           for(int i = 0; i < wordlength; i++)//Checks For Players Guess Validity
           {
             if(gameword[i] == guess)
             {
                 success="y";
               cout<<"Letter "<<guess<<" Is In Position "<<i+1<<" In The Game Word\n\n";//Displays Location Of Valid Guess
                   astring[i]=guess;
                   for(int i = 0; i < wordlength; i++)
                   {
                        if (astring[i]==guess)
                        {
                            cout<<astring;
                            if (astring==gameword)
                            {
                                goto Win;
                            }
    
                        }
                        /*if(astring[i]==guess)
                        {
                        cout<<astring;
                        }
                        if (astring[i]!=guess)
                        {
                            cout<<" ";
                        }*/
                   }
    
             }
           }
    
        if (success=="n")
        {
               cout<<"Sorry, The Letter You Have Guessed Was Incorrect.\n";//Tells Player Their Guess Was Not In The Word
               strikes=strikes+1;
               cout<<"Lives Remaining "<<lives-strikes<<"\n";//Takes Away Srikes From Total Initial Lives
               if(strikes==lives)//Check For Loss
               {
                  cout<<"YOU HAVE LOST!\nTry Again?'y/n'";//Reveals Loss
                  cin>>lost;
                  if (lost=="y")
                  {
                    main(); //Goes To Beginning of Game, If Chosen
                  }
                else
                {
                    exit(0);
                }
               }
        }
        }
        else if (decision=="answer")//Goes Here If Player Wants To Guess The Word
        {
           system("cls");
           cout<<"What Do You Think The Answer Is?";//Asks Player For The Guess Of What The Word Is
           cin>>answer;
           if( answer==gameword)
           {
               Win:
               system("cls");
               cout<<"CONGRATULATIONS! You Have Found The Word!\nYou Are Now The Game Leader!\n\n\n";//Reveals Victory
               cout<<"Hit Enter To Start New Game";
               cin.get();
               GameStart();
           }
           else
           {
               cout<<"Sorry, The Word You Have Guessed Was Incorrect.\n";
               strikes=strikes+2;
               cout<<"Lives Remaining "<<lives-strikes<<"\n";
               if(strikes==lives)
               {
                  cout<<"YOU HAVE LOST!\nTry Again?'y/n'";
                  cin>>lost;
                  if (lost=="y")
                  {
                    GameStart();
                  }
                  else
                  {
                    exit(0);
                  }
               }
           }
        }
        }
    }
    PlayerInput();
    int HelpAndGuide();//Goes Here From 2nd Menu Selection And Provides Game Information
    system("cls");
    cout<<"This Is The Consol Version Of HangMan!\n";
    cout<<"You Have A Space Of 25 Letters For The Game Word.\n";
    cout<<"When Playing This Game And Taking Guesses, A Wrong Guess Will Result\nIn A Strike.\n";
    cout<<"Also When Guessing Letters, The Letter You Type As Your Guess Will.\n";
    cout<<"Result In A Number Or Numbers Of Position(s) That The Letter Is.\n";
    cout<<"That Number Is A Reference To The Location Inside The Game Word.\n";
    cout<<"You Have 10 Chances, Including Both Letter Guesses And Full Word Guesses.\n";
    cout<<"Letter Guesses Are Worth 1 Life\n While Word Guesses Are Worth 2 Lives.\n";
    cout<<"After 10 Chances Are Gone, You Will Lose.\n";
    cout<<"When Typeing Anything In This Game, Type Exclusively In Lowercase.\n\n\n\n\n";
    cout<<"Hit Enter";
    cin.get();
    GameStart();
    
    cin.get();
    }
    that's what i've done... is it the right idea? cuz it doesn't work right.

  13. #28
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Main is just a function, like all others. It simply is the only one called for the execution. So do not define all your functions in main, but they are separate functions. The call happens in main (or in other functions when you want to nest them).
    Try again...

  14. #29
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	int strikes=0; // to tell the plyers how many wrong guesses
    	int wordlength; //to tell plyers how long the word is
    	char guess; //what the players will be defining to play the game
    	string gameword; //the designated word from the leader
    	string decision; //string for type of guessing
    	string answer; //reference to the answer for the player's guess
    	string lost; //choice by player to play again
    	int menuchoice; //what type of meanu they want
    	string success; //to decide if guessed letter is present
    	string name; //Players Name
    	string players; //If theres more players
    	int p; //number of players
    	int lives=10; //number of lives player has
    	char astring[30]="-";
    
    	system("cls");
    	cout<<"Choose A Number.\n"; //Displays Menu Text
    	cout<<"1. Play Game\n";
    	cout<<"2. Help and Guide\n";
    	cout<<"3. Exit\n";
    	cout<<"Choice:";
    	cin>>menuchoice;
    	switch ( menuchoice ) //Allows For Menu Option Selection
    	{
    	case 1:
    		GameStart();
    		break;
    	case 2:
    		HelpAndGuide();
    		break;
    	case 3:
    		exit(0);
    		break;
    	default:
    		GameStart();
    		break;
    	}
    
    	int GameStart();
    	{ //Labels The Game's Start
    		cin.get();
    		system("cls");
    		p=1;
    		int PlayerNames();
    		{ //Labels Area For Player Identification
    			cout<<"Enter Your Name Here:";
    			cin>>name;
    			cout<<"Welcome "<<name<<"!!\nYou Are Player Number"<<p<<"\n";
    			cout<<"More Players?'y/n'"; //Allows For Multiple Player Notification
    			cin>>players;
    			if (players=="y")
    			{
    				p=p+1;
    				PlayerNames();
    			}
    			else if(players=="n")
    			{
    				system("cls");
    				cout<<"The Number Of Players Is "<<p<<"\n";
    				cout<<"Designate A Leader Of This Game Now.\n"; //Tells Players To Choose A Leader
    			}
    		}
    		cin.get();
    		cout<<"For Game Leader:\nEnter Game Word Here:";
    		getline(cin,gameword,'\n'); //leader of game enters the word of the game here
    		system("cls");
    		wordlength=gameword.length(); //stores the length of the word into "wordlength"
    		if( wordlength>25)
    		{
    			cout<<"The Word You Entered Was Too Long For This Game\n";
    			cout<<"Please Try Another Word."; //checks to see if word will fit in the string allowed
    		}
    		else
    		{
    			cout<<"CONGRATULATIONS! You Have Chosen A Valid Word\n";
    			cout<<"Now You Are Ready To Play The Game\n\n\n"; //tells the leader he/she has chosen a valid word, and it's time to play
    		}
    		cout<<"\nFor Players:\nThe Game Word Has A Length Of "<<wordlength<<" Letters\n\n"; //reveals the length of the game word to the players
    
    		for (char w=0;w<wordlength;w++)
    		{
    			cout<<astring;
    		}
    		int PlayerInput();
    		{ //Designates Time Of Game For Player Interaction
    			cout<<"\n\nWhat Would You Like To Do?\n";
    			cout<<"Type 'Answer' To Try And Guess The Word Or Type 'Turn' To Guess A Letter.";//Asks Player To Try Choose Between Guessing A Letter Or The Word
    			cin>>decision;
    			if (decision=="turn")//Goes Here If Player Wants To Guess Letters
    			{
    				system("cls");
    				cout<<"Type Your Single Letter Guess Here:";
    				cin>>guess;
    				success="n";
    				for(int i = 0; i < wordlength; i++)//Checks For Players Guess Validity
    				{
    					if(gameword[i] == guess)
    					{
    						success="y";
    						cout<<"Letter "<<guess<<" Is In Position "<<i+1<<" In The Game Word\n\n";//Displays Location Of Valid Guess
    						astring[i]=guess;
    						for(int i = 0; i < wordlength; i++)
    						{
    							if (astring[i]==guess)
    							{
    								cout<<astring;
    								if (astring==gameword)
    								{
    									goto Win;
    								}
    
    							}
    							/*if(astring[i]==guess)
    							{
    							cout<<astring;
    							}
    							if (astring[i]!=guess)
    							{
    							cout<<" ";
    							}*/
    						}
    
    					}
    				}
    
    				if (success=="n")
    				{
    					cout<<"Sorry, The Letter You Have Guessed Was Incorrect.\n";//Tells Player Their Guess Was Not In The Word
    					strikes=strikes+1;
    					cout<<"Lives Remaining "<<lives-strikes<<"\n";//Takes Away Srikes From Total Initial Lives
    					if(strikes==lives)//Check For Loss
    					{
    						cout<<"YOU HAVE LOST!\nTry Again?'y/n'";//Reveals Loss
    						cin>>lost;
    						if (lost=="y")
    						{
    							main(); //Goes To Beginning of Game, If Chosen
    						}
    						else
    						{
    							exit(0);
    						}
    					}
    				}
    			}
    			else if (decision=="answer")//Goes Here If Player Wants To Guess The Word
    			{
    				system("cls");
    				cout<<"What Do You Think The Answer Is?";//Asks Player For The Guess Of What The Word Is
    				cin>>answer;
    				if( answer==gameword)
    				{
    Win:
    					system("cls");
    					cout<<"CONGRATULATIONS! You Have Found The Word!\nYou Are Now The Game Leader!\n\n\n";//Reveals Victory
    					cout<<"Hit Enter To Start New Game";
    					cin.get();
    					GameStart();
    				}
    				else
    				{
    					cout<<"Sorry, The Word You Have Guessed Was Incorrect.\n";
    					strikes=strikes+2;
    					cout<<"Lives Remaining "<<lives-strikes<<"\n";
    					if(strikes==lives)
    					{
    						cout<<"YOU HAVE LOST!\nTry Again?'y/n'";
    						cin>>lost;
    						if (lost=="y")
    						{
    							GameStart();
    						}
    						else
    						{
    							exit(0);
    						}
    					}
    				}
    			}
    		}
    	}
    	PlayerInput();
    	int HelpAndGuide();//Goes Here From 2nd Menu Selection And Provides Game Information
    	system("cls");
    	cout<<"This Is The Consol Version Of HangMan!\n";
    	cout<<"You Have A Space Of 25 Letters For The Game Word.\n";
    	cout<<"When Playing This Game And Taking Guesses, A Wrong Guess Will Result\nIn A Strike.\n";
    	cout<<"Also When Guessing Letters, The Letter You Type As Your Guess Will.\n";
    	cout<<"Result In A Number Or Numbers Of Position(s) That The Letter Is.\n";
    	cout<<"That Number Is A Reference To The Location Inside The Game Word.\n";
    	cout<<"You Have 10 Chances, Including Both Letter Guesses And Full Word Guesses.\n";
    	cout<<"Letter Guesses Are Worth 1 Life\n While Word Guesses Are Worth 2 Lives.\n";
    	cout<<"After 10 Chances Are Gone, You Will Lose.\n";
    	cout<<"When Typeing Anything In This Game, Type Exclusively In Lowercase.\n\n\n\n\n";
    	cout<<"Hit Enter";
    	cin.get();
    	GameStart();
    
    	cin.get();
    }
    I had to fix that horrible indentation. It's making it hard to read.
    Try following this example when writing code in the future.
    Also remember that each function is a separate code block - they are not related to each and cannot appear inside each other.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #30
    Registered User
    Join Date
    May 2008
    Posts
    18
    yea i understand from the errors generated that the function can't be in another function... but i just don't understand go i'm suppose to do what i wanna do with functions... cuz at the top i call gamestart, but game start isn't defined till after that.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21