Hi, i'm new to C++ and been trying to write a simple program to compare the randomly generator number with the number input. But once i got the num1 = num2; the program keep prompting for new input. Any help would be appreciated. Thanks in advance!

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main(){

	char n; 
	int amt, num1, num2;
	int count;
	int tries;
	
	srand(time(NULL));

	do
	{
	//code for number generated from 0-99
	for(int i=0; i < 1; ++i){
		num2 = rand() % 100 + 1;	}
	cout<<num2 <<endl;

	loop:
	//amount input
	cout << "Enter the amount $1, $2 or $5: ";
	cin >> amt; // get input
	//setting number of tries
	switch (amt){
	case 1: tries = 2;
	break;
	case 2: tries = 5;
	break;
	case 5: tries = 12;
	break;
	default: 
		cout << "Please enter $1, $2 or $5 only..." << endl; goto loop;
		
	}
	//comparing the number
	do
	{
	for (count =0; count < tries ; ++count){

		cout << "Please enter a number: ";
		cin >> num1; // get input
		
		if	(num1 > num2)
		
			cout << "Entered Number is higher than value, retry with a lower number" << endl;
		
		else if  (num1 < num2)
			
			cout << "Entered Number is lower than value, retry with a higher number" << endl;

		else if (num1 == num2)
		{cout << "Congradulations, you have guess the number correctly!!" << endl; break;}
			
			} 
	}while ( count < tries ); 
		
		cout << "Sorry, you have exhausted all your tries..." << endl;
	//To exit or continue
	cout << "Enter 'N' or 'n' to exit, any other key to continue..";
	cin >> n;
	n = toupper(n);
	}while (n != 'N');

	return 0;

	}

Edited 6 Years Ago by grahf23: n/a

Read up on operators and how = is a completely different beast from ==.

Edited 6 Years Ago by Narue: n/a

oh sorry about that..just realized i meant == .. edited but still can't get it to work.

Edited 6 Years Ago by grahf23: n/a

You have an extraneous close brace on Line 57.

EDIT:
Nvm, it now appears to not be the case. You should work on your code formatting a little. Not sure if it's just the way the page is wrapping your code or your formatting. On this site, a "space" or 2 is sufficient indent. A TAB is too big.

I'll keep looking though...

Also, a quick question:
Why do you have your RNG inside a for loop? Why not just make it a simple assignment statement?

EDIT 2:
The issue is that you have your comparison stored inside a for loop which is in turn stored inside a do-while loop. Your "break;" on line 55 ends the for loop but not the do loop. There are 2 ways to address this:

  1. Eliminate the do-while loop since it's doing nothing useful anyway **recommended option**
  2. Eliminate only the structure of the for-loop and modify the do-while loop

Edited 6 Years Ago by Fbody: n/a

On this site, a "space" or 2 is sufficient indent. A TAB is too big.

Actually, you shouldn't have actual tabs in your code. That is my opinion, and it is right. Code editors use monospace fonts ( at least they should, for the love of Turing ). Tabs are used by word processors to gurantee alignment of non-monospace fonts. As far as I know, all editors provide an option to replace tabs with some fixed number of spaces. You should use this option. My personal preference is 4 spaces for each indentation level.


Also, a quick question:
Why do you have your RNG inside a for loop? Why not just make it a simple assignment statement?

EDIT 2:
The issue is that you have your comparison stored inside a for loop which is in turn stored inside a do-while loop. Your "break;" on line 55 ends the for loop but not the do loop. There are 2 ways to address this:

  1. Eliminate the do-while loop since it's doing nothing useful anyway **recommended option**
  2. Eliminate only the structure of the for-loop and modify the do-while loop

Hi, sorry i'm new to this but do you mean just assign num2 = rand() % 100 + 1; ?

Regarding the do-while loop, i write it in to stop the for loop when count == tries. I'm not sure if i'm supposed to write it like that. Will try removing the do-while loop when i get home.

Thanks for the help

Actually, you shouldn't have actual tabs in your code. That is my opinion, and it is right. Code editors use monospace fonts ( at least they should, for the love of Turing ). Tabs are used by word processors to gurantee alignment of non-monospace fonts. As far as I know, all editors provide an option to replace tabs with some fixed number of spaces. You should use this option. My personal preference is 4 spaces for each indentation level.

Just because it's your opinion doesn't mean it's gospel. There is nothing wrong with a TAB in code, it just whitespace, which the language ignores under most circumstances anyway.

If you're working in an IDE, a TAB is just fine, the compiler doesn't care, as long as it's whitespace. I mentioned it because the TAB character that the site uses is too big and it messes up the block's formatting. I use TABs all the time, without replacement, but then I convert them to (2) spaces if I have to post code here. The horizontal size of the TAB character in a [code] block is too wide and makes the formatting look bad. I guess I don't see the point of replacing 1 character with 4 if you don't have to. I use a flash drive for alot of my stuff so that I can work on it in multiple locations, but I need it for other things as well, so my storage is limited. A TAB not only saves keystrokes, it makes for a smaller source file (because there are fewer chars to store).

>>Hi, sorry i'm new to this but do you mean just assign num2 = rand() % 100 + 1; ?
Yes, all you need is the assignment statement.

>>...i write it in to stop the for loop when count == tries. I'm not sure if i'm supposed to write it like that.
I suspected as much. No, you are not supposed to write it like that. That do-while loop has absolutely no effect on the for loop. All loop structures are completely self-contained blocks of code. All loops are responsible for stopping themselves, you can not use an outer loop to stop another loop nested within it. All that does is make the nested loop start over again. That's why nested loops are used for sorts and multi-dimensional arrays.
In theory, the way you have written this, you can get up to 144 iterations. That's 12x the number of iterations you want. Nested loops increase iteration counts, not decrease them. Get rid of the do-while loop.

Edited 6 Years Ago by Fbody: n/a

Hi,
i have do away with the do-while loop instead i have add in another if else statement. The program is working now but just wanted to ask whether this is the correct practice to write like this?

for (count =0; count < tries ; ++count){
		
		cout << "Please enter a number: ";
		cin >> num1; // get input		
		if	((num1 > num2)&&(count < tries-1))
			cout << "Entered Number is higher than value, retry with a lower number" << endl;		
		else if  ((num1 < num2)&&(count < tries-1))	
			cout << "Entered Number is lower than value, retry with a higher number" << endl;		
		else if (num1 == num2)
		{cout << "Congradulations, you have guess the number correctly!!" << endl; break;}
		
		else if ((num1 != num2)&&(count == tries-1))
		{cout << "Sorry, you have exhausted all your tries..." << endl;}		
			}

Hi,
i have do away with the do-while loop instead i have add in another if else statement. The program is working now but just wanted to ask whether this is the correct practice to write like this?

for (count =0; count < tries ; ++count){
		
		cout << "Please enter a number: ";
		cin >> num1; // get input		
		if	((num1 > num2)&&(count < tries-1))
			cout << "Entered Number is higher than value, retry with a lower number" << endl;		
		else if  ((num1 < num2)&&(count < tries-1))	
			cout << "Entered Number is lower than value, retry with a higher number" << endl;		
		else if (num1 == num2)
		{cout << "Congradulations, you have guess the number correctly!!" << endl; break;}
		
		else if ((num1 != num2)&&(count == tries-1))
		{cout << "Sorry, you have exhausted all your tries..." << endl;}		
			}

That should work okay, there is nothing wrong with that. I'm not too sure it's ideal, but it works.

Edited 6 Years Ago by Fbody: n/a

This question has already been answered. Start a new discussion instead.