Page 1 of 1

Fuction calls getting skipped in switch statements

Posted: Fri Aug 26, 2011 5:03 am
by blackball2186
The problem that i seem to be having is that none of the function calls inside of the switch statements are working, have spent quite a while tweaking the code trying to figure out whats wrong but i cant put my finger on it, if anyone knows what the solution to my problem is please let me know.

Code: Select all

// guessing_game.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <limits>

using namespace std;

int tries = 0;
int mynumber;
char response;
char second_response;
char third_response;
int guess();
int game();
int think_of_number();
int _continue();
int higherlower();



int _tmain(int argc, _TCHAR* argv[])
{
	int think_of_number(); // asks user to think of a number and waits for input
	{
		cout << "think of a number between 1 and 100 \n";
		cout << "press enter when ready \n";
		cin.clear();
		cin.get();
		int guess();
		
	}

	int guess(); // randomizes computers guess at what number user is thinking of
	{	
		srand(time(0));
		mynumber = rand() % 100 + 1;
		int game();
	}

	int game(); // main game loop
	{	
		tries++; // incriments the number of attempts the computer has made at guessing users number
		cout << " is your number " << mynumber << "\n";
		cout << " <y/n> \n";
		cin.clear(response);
		cin >> response; 

		switch (response) 
		{
			case 'y' : // if the computer guessed correctly prints victory message 
				cout << " the computer guessed right in " << tries << " tries \n";
					
					int _continue(); // asks user if they would like to play again
					{
						cout << " would you like to play again ? <y/n> \n ";
						cin.clear(second_response);
						cin >> second_response;

						switch (second_response) 
						{
							case 'y' : // runs the game from the beginning 
								int think_of_number();
								

							case 'n' : // exits the game
								 ;
								 

							default : // prints that you have pressed an invalid key
								cout << " invalid entry \n";
								int _continue(); // runs the would you like to play functionality over again
								
						}
					

					
					}
			case 'n' : // indicates that the computers guess was wrong 
				cout << " the computer guessed wrong \n";
				
					int higherlower(); // asks the user if the number is higher or lower and waits for response
					{
						cout << " is your number higher or lower ? <h/l> \n";
						cin.clear(third_response);
						cin >> third_response;

						switch (third_response)
						{
							case 'h' : // takes mynumber and incriments it by half its value and returns to the main game loop
								mynumber = mynumber + (( 100 - mynumber) / 2);
								int game();
								

							case 'l' : // takes mynumber and halves it and returns to the main game loop
								mynumber = mynumber - (mynumber / 2);
								int game();
								

							default : // indicates the user pressed a wrong key and restarts the higherlower function 
								cout << " invalid entry \n";
								int higherlower();
								
						}
				

					
					}
		}


	}
	int think_of_number(); // starts the game over again
	return 0;
	
}

Re: Fuction calls getting skipped in switch statements

Posted: Fri Aug 26, 2011 6:14 am
by blackball2186
there is also another thing about this code that is a bit puzzling. I get an error when i remove the semi-colon at the end of the function definition.

Code: Select all

int guess(); // this semicolon stops the error from occuring
	{	
		srand(time(0));
		mynumber = rand() % 100 + 1;
		int game();
        }

int guess() 
	{	// without the semicolon , i get an error on the brace saying it expected a semicolon
		srand(time(0));
		mynumber = rand() % 100 + 1;
		int game();
        }

Re: Fuction calls getting skipped in switch statements

Posted: Fri Aug 26, 2011 7:47 am
by techboy123
The syntax of your function definitions is wrong. In the current C++ standard it is illegal to define functions within functions (the new standard allows this in the form of lambdas). I am unsure why it compiles though I put it down to your function bodies actually being scopes within the main function body and the "int guess();" being a forward declaration. This is why it fails to compile when the semi-colon is absent.

The correct function definition syntax is as follows.

Code: Select all

int think_of_number(); //Declare the functions existence.

int main(int argc, char* argv[]) {
    think_of_number(); //Note we can call the function even though it has yet to be defined.
    return 0;
}

//If this isn't defined. You will get linker errors informing you of its lack of definition.
int think_of_number() {
     cout << "think of a number between 1 and 100 \n";
      cout << "press enter when ready \n";
      cin.clear();
      cin.get();
      
     guess();   
    return 0; //An int function MUST return an integer value. 
}

int guess() {
    //And so on...
}

Once you have corrected all function definitions. You will get several errors such as expected returns and missing braces ( { and } ). And in one case I found a } that shouldn't be there. Chances are you may need to rewrite parts of the code as variables inside the scopes will no longer be accessible to the functions in question. For more info you can go to http://www.cprogramming.com/tutorial/lesson4.html.

And another heads up. Your switches will not function as you intend them to. To give an example.

Code: Select all

switch(2) {
   case 1:
	   std::cout<<"One"<<std::endl;
   case 2:
	   std::cout<<"Two"<<std::endl;
   case 3:
          std::cout<<"Three"<<std::endl;   
   }

Outputs:
Two
Three
In C++ case statements fall through. In other word. It will goto the case that matches the condition and then continuing executing code downwards from there. To fix this. Simply append a break statement to each case like so.

Code: Select all

switch(2) {
   case 1:
	   std::cout<<"One"<<std::endl;
           break;
   case 2:
	   std::cout<<"Two"<<std::endl;
           break;
   case 3:
          std::cout<<"Three"<<std::endl;   
          break;
   }

Outputs:Two

I hope this post has helped you gain a better understanding of the syntax of C++. And I also hope someone with a better understanding of C++'s syntax can explain why a function declaration is valid within a function body.