Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

need a basic code to finish

Ask ALL your questions here

need a basic code to finish

Postby *GR*Flair on Tue Oct 27, 2009 11:13 pm

if the person gets the answer before the 5 tries are up in this it let's you keep guessing, how would i break or continue it if they win w/o using all tries? In: int main()

Code: Select all
#include "stdafx.h"
#include <iostream>

using namespace std;

void loser()
{
   cout << "Aw, you didn't win this time, but the number is still the same so you can try\nagain.";
}

void winner()
{
   cout << "Congradulations, you have won my first game!";
}

int main()
{
   int theNumber = 67;
   int playerGuess;
   cout << "Let's play guess my number. You have 5 tries.\n";
   cout << "Type a number between 0 - 100: ";

   for(int Tries = 0; Tries < 5; Tries++)
   {
      cin >> playerGuess;
      if (playerGuess == theNumber)
         cout << "YOU WIN!\n\n";
      else
         if (playerGuess > 100)
                  cout << "Stay within the boundries.\n";
         else
            if (playerGuess > theNumber)
               cout << "Lower.\n";
            else
               if (playerGuess < 0)
                  cout << "No negative numbers!\n";
               else
                  if (playerGuess < theNumber)
                  cout << "Higher.\n";
   }
   if(playerGuess == theNumber)
      winner();
   else
      loser();
   

   char f;
   cin >> f;
   return(0);
}
*GR*Flair
 
Posts: 2
Joined: Tue Oct 27, 2009 10:53 pm

Re: need a basic code to finish

Postby Vevix on Wed Oct 28, 2009 12:45 am

When they guess the number correctly, all you're doing is outputting a message and continuing execution of the loop; You'll need to use break

Code: Select all
if (Guess == Number)
{
   // Message
   break; // Break from the loop
}


for example.
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg. -- Bjarne Stroustrup
User avatar
Vevix
 
Posts: 142
Joined: Sat Oct 10, 2009 9:24 pm
Location: kernel32.dll

Re: need a basic code to finish

Postby *GR*Flair on Wed Oct 28, 2009 5:37 pm

ok thanks it worked
*GR*Flair
 
Posts: 2
Joined: Tue Oct 27, 2009 10:53 pm


Return to QA

Who is online

Users browsing this forum: No registered users and 0 guests