• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

c++ question

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

asusradeon

Member
Joined
Oct 25, 2004
Location
127.0.0.1
Hi, just started programming in c++ done some basics and now im confused. trying to make a lil text guesiing game all works except last bit asking if they wana play again can someone tell me where im going wrong.

Code:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

int main ( )
{
int guess;
char ans;

srand (time (0));
int random = rand( );
int num = ((random % 100) + 1);
for (int i = 0; i < 100; i++)
{




start:

cout << "Guess a number between 1 and 100: ";
cin >> guess;

if (guess > num)
{
cout << "The number is lower. Try Again!!\n\n";
continue;

}
if (guess < num)
{
cout << "The number is higher. Try Again!!\n\n";
continue;


}
else if (guess == num)
{
cout << "You've guessed correctly!\n\n";
break;
}

}

string again;
cout << "Do you want to play again?  y or n" << endl;
cin >> again;
if (again == "y")

goto start

end if;

    system("PAUSE");
    return EXIT_SUCCESS;
}
 
asusradeon said:
Hi, just started programming in c++ done some basics and now im confused. trying to make a lil text guesiing game all works except last bit asking if they wana play again can someone tell me where im going wrong.

Welcome to the club. C and C++ is confusing, at first.

There are a few cases where a goto is needed. This is NOT one of those special cases. You should NEVER use a goto unless it's ABSOLUTELY necessary.

The assembly program uses goto's, but you're not writing assembly programs, and goto's are religiously despised, because they turn good structured code into absolutely confusing noodle-code; completely uninteligible, when over-used.

How about a nice while loop?
Code:
again = 'y'
while again != 'n'
{
   print Do you want to play again? 
   scanf the answer (again)
    if again != 'n' then 
      call the play function, play()
}
Loop will exit when the user presses n. End of program.

The play() function will do the actual playing of the game, including the "you won" or "you lost", messages.

That's how I would do it.

I'll be back in a few hours. Why don't you make your changes, and post them?

Adak
 
The use of the unconditional jump (goto) is Considered Harmful and is deprecated. Heavily.

The 'continue'/'break' keywords may not appear outside of a loop construct (for, while, do{...}while).

(Never mind, there's a loop there, didn't see it at first glance. Doh! Stupid me. -CN)

An 'end if' is not permitted in C/C++, use a closing curly brace '}' at the end of the if-block.

The loop that Adak has is less ugly and easier to trace.
 
Hi, i worked out the goto statements but i was going to do the loop however im not sure where to put it etc.. could someone put it into the code for me please....

Code:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;


int main ( )
{

cout << "Guess the number game" << endl;
cout << "" << endl;
cout << "Written by Jason Underhill" << endl;
cout << "" << endl;
cout << "28 - 3 - 06" << endl;
cout << "" << endl;
cout << "Please enjoy the game..." << endl;
cout << "" << endl;
cout << "------------------------" << endl;
cout << "" << endl;



int guess;
char ans;

start:

srand (time (0));
int random = rand( );
int num = ((random % 100) + 1);
for (int i = 0; i < 100; i++)
{






cout << "Guess a number between 1 and 100: ";
cin >> guess;

if (guess > num)
{
cout << "The number is lower. Try Again!!\n\n";
continue;

}
if (guess < num)
{
cout << "The number is higher. Try Again!!\n\n";
continue;


}
else if (guess == num)
{
cout << "You've guessed correctly!\n\n";
break;
}

}

string again;
cout << "Do you want to play again?  y or n" << endl;
cin >> again;
if (again == "y")
{
goto start;
}

    system("PAUSE");
    return EXIT_SUCCESS;
}
 
Put everything from where you're seeding the random number generator through cin >> again; in a do-while loop - do{ //code here }while( condition ) and just set the while statement to be while( again == "y" )

Get rid of the start: and the goto start; lines. They're bad :D
 
Hey asusradeon,

This is my take on your program:

Code:
//I'm using a very old c++ compiler, so my include files are slightly 
//different. 

#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//#include <cstdlib>
//#include <cmath.h>
//#include <ctime.h>
//using namespace std;

int main( )
{
   int guess;
   char ans, again;

   srand (time (0));
   int random = rand( );
   again = 'y';
   while (again == 'y')   {
      int num = ((random % 100) + 1);
      for (int i = 0; i < 100; i++)   {
         cout << "Enter 0 to quit the game, or ";
         cout << "Guess a number between 1 and 100: ";
         
         cin >> guess;
         if (guess == 0)   {
            again = 'n';
            break;
         }
         if (guess > num) 
            cout << "The number is lower. Try Again!!\n\n";
         if (guess < num)  
            cout << "The number is higher. Try Again!!\n\n";
         else if (guess == num)  
            cout << "You've guessed correctly!\n\n";
      } // end of for int i...
      //on break, control jumps here
      if (again != 'n')  {
         cout << "Do you want to play again?  y or n" << endl;
         cin >> again;
      }
   } //end of while...   
      
   system("PAUSE");
   return EXIT_SUCCESS;
}

Hope that helps you.

Adak
 
In my C++ programming language third edition, Bjarne Stroustrup on goto

"The goto has few uses in general high-level programming, but it can be very useful when c++ code is generated by a program rather than written directly by a person; for example, gotos can be used in a parser generated from a grammar by a parser generator. The goto can also be important in the rare cases in which optimal efficiency is essential, for example, in the inner loop of some real-time application."

He basically says to avoid using goto.

As far as your program is concerned, it seemed to actually work with the gotos. In my opinion, if something works properly, thats what really matters. If there are no negative side effects to using gotos in a instance, why not? In a huge program, gotos would probably make things a bit complicated.
 
Last edited:
brakezone said:
In my C++ programming language third edition, Bjarne Stroustrup on goto

"The goto has few uses in general high-level programming, but it can be very useful when c++ code is generated by a program rather than written directly by a person; for example, gotos can be used in a parser generated from a grammar by a parser generator. The goto can also be important in the rare cases in which optimal efficiency is essential, for example, in the inner loop of some real-time application."

He basically says to avoid using goto.

As far as your program is concerned, it seemed to actually work with the gotos. In my opinion, if something works properly, thats what really matters. If there are no negative side effects to using gotos in a instance, why not? In a huge program, gotos would probably make things a bit complicated.

When you're learning a skill like programming, form is also very important. Important for your grade (my prof would not accept such a gratuitous goto, at all on a program assignment), and by those who would hire you perhaps, in the future. Also, you lose the respect of your peers, which is important in networking, and refining your own skills.

No matter how good you are, you will sooner or later want a peer's assistance with a program, and you won't get much beyond WTF? with gratuitous goto's in the code.

Starting from day 1, goto's should be avoided except in deeply nested loops, where breaking out of all of them is ludicrous, without the use of a goto.

Adak
 
As far as what I said,I'm not saying to use goto's

I'm saying if you can use them in order to accomplish something with no negative side effects, go for it. Of course people are generally going to think that this is bad style. Also, I doubt you can use alot of gotos without some kind of bad side effect.
 
There is break; and C++ exception handling for deeply nested stuff. Only place I've seen goto applied correctly so far is OS relevant exception handling (SEH) or C exception handling. I can't comment on what Bjarne says since he's a lot smarter than me, but computer generated code sucks anyways even if it's needed sometimes (parsers with lexx and yacc)
 
If you're going to use Adak's interpretation of the code, you need to somehow force the for() loop to exit when they guess correctly. Otherwise if they guess it on the 25th try, they'll be stuck for 75 more repetitions before they can play again. A simple way of doing this is alter the code run when (guess == num) to set i to some number greater than 100.

else if(guess==num)
{
i=101;
cout << "You've guessed correctly!\n\n";
}

Or, if 100 is the total number of guesses allowed, change the check condition in the for loop to some boolean variable that you set to false when either i==100, or guess==num.

bool run;
...
run = true; //immediately before the for() statement.
for(i=0; run; i++)
...
...
else if(guess==num)
{
...
run=false;
}
else if(i>=100)
{
//Generic "You Fail" message"
run=false;
}


However, looking at the timestamps I guess the program has already been turned in. Gotta love last minute runs. I won't comment on the badness of "goto" in C/C++, since other people have already argued that point.
 
Back