Closed Thread
Results 1 to 4 of 4

Thread: can someone help me turn regular code into a switch statement??

  1. #1
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    can someone help me turn regular code into a switch statement??

    Hey guys, i've got all this coding here, it's almost complete, but i've run into a few snags to make it complete.

    of course, rep for help

    Here are my hiccups, then i'll post the code.

    What the code does:.. Its a game, you guess numbers and win money based off of how many guesses it took you to complete it, the more guesses, the less money!

    1. The game is supposed to not let them guess after their 8th guess.. that part of my code doesnt work for some reason? it seems like i can do a million guesses.

    2. I must have a function thats called "calcWinnings" that takes as an argument, the number of guesses it took to determine the number. I must use a switch statement to determine the winnings. This function returns the number of winnings.

    heres what someone said to help someone else doing this project, but i still dont understand it.
    Keep track of the number of guesses and pass that value to a function called calcWinnings(). Use the value passed in as the conditional in the switch statement. The switch statement will look a lot like the printPayout function except that it will use case statements indicating what the winnings are based on the value passed in. The return value of the function can be stored in the variable winnings and that value added to the bank.
    but i cant figure out for the life of me how to get it to work.
    3. i get 1 warning, which i cant figure out why.

    number 2 is killing me, i didnt set it up like that and now im having a darn bad time trying to figure that out and i just cant seem to do it. no code is allowed in main() and all the different functions and stuff that are there are because thats how i HAVE to do it.. weird i know.

    here is the code:
    i think everything is basic for coding wise, i just need to fix that problem of making it into a switch statement.
    the code runs great though, play it if you want. haha

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    double bank;
    double winnings;
    double total;
    int check;
    char number;
    int guess;
    bool done;
    int guessesnum = 0;
    int randm;
    int c;
    
    void printPayout()
    {
    	cout << "1 guess, win 2.00" << endl;
    	cout << "2 guesses, win 1.75" << endl;
    	cout << "3 guesses, win 1.5" << endl;
    	cout << "4 guesses, win 1.25" << endl;
    	cout << "5 guesses, win 1.00" << endl;
    	cout << "6 guesses, win .75" << endl;
    	cout << "7 guesses, win .5" << endl;
    	cout << "8 guesses, win .25" << endl << endl;
    }
    
    int genrandm()
    {
    	int r;
    
    	srand(time(NULL));
    
    	r = rand() % 100 + 1;
    
    	return r;
    }
    
    int checkGuess(int guess)
    {
    	if (guess > randm) {
    		check = 1;
    	}
    	else if (guess < randm) {
    		check = -1;
    	}
    	else {
    		check = 0;
    	}
    
    	return (check);
    }
    
    
    int game()
    {
    	cout << "Welcome to the game of guesses. It only costs a dollar to play. You can double your bet." << endl << endl;
    	cout << "Do you want to play? (y / n)" << endl;
    
    	bank = 100.00;
    	
    	cin >> number;
    	
                 if (number == 'y')
    	{
    		winnings = 2.00;
    
    		cout << "Great!, your payout will be as follows:" << endl << endl;
    
    		printPayout();
    
    		bank = bank - 1;
    
    		cout << "Your bank now has " << bank << " in it" << endl;
    
    
    		randm = genrandm();
    
    		while((bank != 0)&&(number == 'y'))
                             {
    
    			guessesnum = 0;
    
    			randm = genrandm();
    
    			winnings = 2.00;
    
    			while ((guessesnum != 8))
    			{
    				cout << "Now guess a number between 1 and 100" << endl;
    
    				cin >> guess;
    
    				guessesnum++;
    
    				c = checkGuess (guess);
    
    				if (c == 0)
    				{
    					bank = bank + winnings;
    
    					cout << "Correct, you win " << winnings << ", your bank is now " << bank << endl;
    					cout << "Would you like to play again?(Y/N)";
    
    					cin >> number;
    
    					break;
    				}
    				else if (c == -1)
    				{
    					cout << "Sorry that's too low try higher!" << endl;
    					winnings = winnings - .25;
    				}
    
    				else if (c == 1)
    				{
    					cout << "Sorry that's too high try lower!" << endl;
    					winnings = winnings - .25;
    				}
    			}
    		}
    	}
    
    
    	cout << "Thanks for playing, here is your bank " << bank << endl;
    
    	return 0;
    }
    
    
    int main()
    {
    
    	game();
    
    	return 0;
    }
    thank you so much if you can help me.
    Last edited by Hawk1; 10-18-2008 at 12:17 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    LukeyJ is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    92
    Rep Power
    13

    Re: can someone help me turn regular code into a switch statement??

    I'll have a look at it.

    First thing I noticed is there is no way to exit the game, when I press n and press enter it goes into a repeating loop. That may need to be edited. I'll edit this post when I've had a better look.

    Okay, your problem is that your main loop is inside the other loop. When it gets to 8 attempts, there is no way to exit the game. If you are right it exits the game. On 8 attempts it escapes the inside loop and goes to the loop on the outside which starts the game up again. I added:

    if (guessesnum ==8) { return 0; }

    To the main loop at the end, and it quits the program, you may want to escape it a bit prettier than that of course.

  4. #3
    LukeyJ is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    92
    Rep Power
    13

    Re: can someone help me turn regular code into a switch statement??

    The advice you got was correct...
    Code:
    current_winnings = calcWinnings (int guesses)
    
    double calcWinnings (int guesses)
    {
    switch (guesses) {
    
    case 1:
    return 2.0;
    break;
    
    etc.
    
    }
    C++ Syntax: switch

    There is a tutorial on switch's, I'm not great at them neither, but if needs be, they are staightforward. They are useful for any variable that could be 3 or more values and you want instructions based on this.

  5. #4
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: can someone help me turn regular code into a switch statement??

    I've PM'ed you it..
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Switch Statement
    By docmonkey in forum PHP Development
    Replies: 4
    Last Post: 06-17-2011, 03:46 PM
  2. Declaring an array and using within switch statement
    By thintheherd in forum C and C++
    Replies: 3
    Last Post: 06-02-2010, 03:12 AM
  3. Switch Statement
    By ahmed in forum C and C++
    Replies: 3
    Last Post: 11-07-2008, 02:57 PM
  4. what's wrong with this switch statement...
    By digerati in forum C and C++
    Replies: 13
    Last Post: 10-04-2008, 06:30 AM
  5. Switch statement in string returning method...
    By ozyabm in forum C and C++
    Replies: 3
    Last Post: 07-06-2008, 12:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts