Beginner C++ helppp

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Odifma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 23
Joined: Mon Feb 21, 2011 4:21 am

Beginner C++ helppp

Post by Odifma »

its been ages since ive been on this sight :/ ive been mia from my journey to teaching myself C++ so i decided to sign up for a class at my local Community College. i have an assignment for my beginner C++ class. I have to make a program to randomly generate a number 1-10. then ask the user to guess a number. and have it display something like your wrong or your right depending if they guessed it right or wrong. here is what i have so far

Code: Select all

#include <iostream>
#include <cstdlib>

using namespace std;

int main()

{
	int randomGeneratedNumber;
	randomGeneratedNumber = rand()%(10);

	int randomNumberGuess;

	while (randomGeneratedNumber != 10);
	{
	cout << "Guess a number between 1 and 10" << endl;

	cin >> randomNumberGuess;
	}
	if (randomNumberGuess == randomGeneratedNumber);
	{
		cout << "Correct!" << endl;
	}
	if (randomNumberGuess != randomGeneratedNumber);
	{
		cout << "Wrong! guess again!" << endl;
	}

	

	return 0;

}
i try to run it and just a black dos screen pops up.. can someone give me some help? btw my teacher never showed us how to generate a number, i had to research it myself... and im still iffy on the IF statements.. any help is appreciated :)
Image
User avatar
JarrodParkes
ES Beta Backer
ES Beta Backer
Posts: 325
Joined: Thu Feb 25, 2010 2:39 pm
Current Project: Yes, iPhone Application(s)
Favorite Gaming Platforms: N64, PC
Programming Language of Choice: C++
Location: Mountain View, CA
Contact:

Re: Beginner C++ helppp

Post by JarrodParkes »

I see a couple issues here...

To generate a number between 1-10, you will need to increment the number produced by your current call to the rand function.

Code: Select all

randomGeneratedNumber = rand()%10 + 1;
Then, initializing randomNumberGuess should probably be your next move, and in practice, it is a good convention to initialize all variables.

Code: Select all

int randomNumberGuess = 0;
After initializing, then you should poll for the first user input. The reason for doing this is because your while loop needs to check for values outside the valid range (1-10), and randomNumberGuess has not been populated with user input yet. Looking at the code might make more sense...

Code: Select all

cout << "Guess a number 1-10: ";
cin >> randomNumberGuess;

while(randomNumberGuess < 1 || randomNumberGuess > 10) {
cout << "Quit being a clown, and guess a number between 1-10!!!" 
cin >> randomNumberGuess;
}
Outside of that, I believe the rest of your code should work fine.
User avatar
Odifma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 23
Joined: Mon Feb 21, 2011 4:21 am

Re: Beginner C++ helppp

Post by Odifma »

awesome thank you :) ok that makes more sense. ya my instructor kinda just expects us to figure some of this stuff out. which is good sometimes, but not in the first few weeks of class heh... thanks for the help and i greatly appreciate it :)
Image
User avatar
Odifma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 23
Joined: Mon Feb 21, 2011 4:21 am

Re: Beginner C++ helppp

Post by Odifma »

ok i have an issue now. i run my program. asks me for a number between 1 and 10. i put 1 says lower, i put 1 again says higher, i put 1 again and it says correct. and im not sure how to make it so my program keeps running until i get the answer right. so if i gues 3, then 2, then 1, then 5 and their all wrong answers, have it let the user keep inputting nubmers until it gets the right number generated.
here is my code...

Code: Select all

#include <iostream>
#include <cstdlib>

using namespace std;

int main()

{
	int randomGeneratedNumber;
	randomGeneratedNumber = rand()%10 + 1;

	int randomNumberGuess = 0;

	cout << "Guess a number 1-10: ";
	cin >> randomNumberGuess;

	while(randomNumberGuess < 1 || randomNumberGuess > 10);

	if (randomNumberGuess > randomGeneratedNumber);
	{
		cout << "Lowerrrrrrr!" << endl;
		cin >> randomNumberGuess;
	}
	
	if (randomNumberGuess < randomGeneratedNumber);
	{
		cout << "HIGHER!!!" << endl;
		cin >> randomNumberGuess;
	}
	
	if (randomNumberGuess == randomGeneratedNumber);
	{
		cout << "CORRECT!!!11one!11!" << endl;
	}


	system ("pause");
	

	return 0;

}
I also took out the while statement and it does the same thing.
Image
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: Beginner C++ helppp

Post by superLED »

Put this in, before your while loop:
srand(static_cast<unsigned>(time(0)));

That will make sure you get random numbers every time you run the program.
I guess that now when you run the program, it generates the same numbers sequence.
So, with the srand function, it takes a random generated seed for you to start off with.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Beginner C++ helppp

Post by dandymcgee »

Please put your code in

Code: Select all

 blocks when you post it on the forums.  It preserves the formatting of the text, and prevents you from wasting vertical space.

Also, it's better practice to pause execution of your program using cin.get(); or getchar(); rather than system("pause").  This is a pet peeve of mine as system("pause") is both Windows dependent and infinitely less effeicient than the proper alternatives.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Odifma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 23
Joined: Mon Feb 21, 2011 4:21 am

Re: Beginner C++ helppp

Post by Odifma »

thanks superled, i put that in there :) and i apoogize for the long format, im still new to coding n what not and didnt know how to put it in blocks. but il do it from now on :) but im still having the same issue when i run the program. i can still put any number and itl tell me lower, hihger, then correct no matter waht numbers i put. i just want it to let me keep guessing until i get the right number. OH and also. my teacher was telling us about the system "pause" thing and actually said to use it over the things your mentioned. well not ecactly what you mentioned, it was just briefly in the 2nd day of class. but im noticng my teacher isnt taht great of a teacher heh..
Image
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: Beginner C++ helppp

Post by superLED »

You forgot the curly brackets for the While Loop.
And you are NOT supposed to put semicolon after If Statements.

And you told the program that it should run only 'if the number the user typed in was lower than 1, or higher than 10'.
That means, the program exits at the moment you type in a number between 1 and 10.
But if you checked if the user typed in a number that was higher than 1, and lower than 10, the program would stop if you typed '1' or '10'.

This is how I would manage to make this program:

Code: Select all

#include <iostream>
#include <cstdlib>
#include <ctime> // This will allow us to use a random seed, so the random number will always be random.

using namespace std;

int main() {
	// Making the random seed.
	srand(static_cast<unsigned>(time(0)));

	// To check it the program should keep running or not.
	bool run = true;

	// This is our random number.
	int randomNum = rand()%10 + 1;

	// This is our guess.
	int guess = 0;

	// Run this loop as long as the 'run' variable is true.
	while(run == true) {
		cout << "Guess a number 1-10: ";
		cin >> guess; // At the beginning of each loop, type in a new guess.

		// If the guess was lower than the random number.
		if(guess > randomNum) {
			cout << "Lower!" << endl << endl;
		}
		
		// If not, check if the guess was higher than the random number.
		else if(guess < randomNum) {
			cout << "Higher!" << endl << endl;
		}
		
		// If none of the abow choises were true, check if the guess is exactly the same as the random number.
		else if(guess == randomNum) {
			cout << "Correct answer! " << endl << endl;

			// This will exit the loop, and quit the program.
			run = false;
		}
	}

	system ("pause");
	return 0;
}
And please, read and understand the comments (after the '//'), and not just copy and paste. That way you won't learn anything.

(Keep in mind, the program will crash if you type in something other than numbers)
User avatar
Odifma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 23
Joined: Mon Feb 21, 2011 4:21 am

Re: Beginner C++ helppp

Post by Odifma »

whats that? copy and past so i can get an A+ in my class, why sureeeee hahaha jk :p yea usually i just try to read it and udnerstand why its there. cause just pasting isnt going to help me learn anything besides how to ctrl+c / ctrl+v faster.. the hardest part is that my teacher barely went over the "if" thing. and doesnt have notes. and for hw assignments, he just had us write it down ourselves... so ive had to do alot of research... i was goign to post another reply ealrier to waht i have so can you look at what i put and give me some advice? cause i tried using the ifelse and it woudlnt let me... anywho here is what I wrote

Code: Select all

 
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{
	srand(static_cast<unsigned>(time(0)));

	int randomGeneratedNumber = rand() % 10 + 1;

	int randomNumberGuess = 0;
	
	cout << "Guess a number 1-10: ";
	
	cin >> randomNumberGuess;

	bool stillGuessing = true;
	
	while (stillGuessing);
	{

		if (randomNumberGuess > randomGeneratedNumber);
		{
		cout << "Lowerrrrrrr! Try again!" << endl;
		cin >> randomNumberGuess;
		}
		
		
		else if (randomNumberGuess < randomGeneratedNumber);
		{
		cout << "HIGHER!!! Try again!" << endl;
		cin >> randomNumberGuess;
		}

		else (randomNumberGuess == randomGeneratedNumber) stillGuessing = false;
		{
		cout << "CORRECT!!!11one!11!" << endl;
		}

	}
	system ("pause");
	

	return 0;

}
 
from what i saw from your code, looks like i was close, but since im lakcing in some of the knowledge of the while and else statements, makes it a bit harder..
Image
User avatar
Odifma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 23
Joined: Mon Feb 21, 2011 4:21 am

Re: Beginner C++ helppp

Post by Odifma »

also, i probably should start to leave //comments in my code huh? gah... but really. i greatly apprecaite the help. im going to try to write it using what i have and just read over the comments you left for some help :)
Image
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: Beginner C++ helppp

Post by Avishaiozeri »

When using any control statement in c++ (if, else, while etc.) never put a semicolon (;) after the statement.

The control statement affects only what comes directly after it. If you want to affect more then 1 line, you need to put
the code in a code block ({..}).

if (randomNumberGuess > randomGeneratedNumber); <-- remove semicolon.

Basically, the 'if' statement will effect whats right after it, and guess what that is? a semicolon... you want to have your code block right after the 'if':

Code: Select all

 if (randomNumberGuess > randomGeneratedNumber) // <-- No semicolon, the if is applied on whats directly after it, the code block ({..})
 {
      cout << "Lowerrrrrrr! Try again!" << endl;
      cin >> randomNumberGuess;
 }
same with 'else':
else (randomNumberGuess == randomGeneratedNumber) stillGuessing = false; <-- the 'else' will affect only what's after it, which is: stillGuessing = false; it will not affect the code block ({..}) at all!

You need to get all the things the the 'else' should affect into the code block:

Code: Select all

else (randomNumberGuess == randomGeneratedNumber) // <-- No semicolon
{
      stillGuessing = false; // <-- In the code block!
      cout << "CORRECT!!!11one!11!" << endl;
}
And another thing, you forgot an 'if':
else (randomNumberGuess == randomGeneratedNumber)
should be:
else if(randomNumberGuess == randomGeneratedNumber) .
you can't put a condition after an 'else', if you want to put a condition after it, you have to put an 'if'...

Remember, all of this applies to all control statements (while, if, else etc.) So your code should look like this:

Code: Select all

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
   srand(static_cast<unsigned>(time(0)));

   int randomGeneratedNumber = rand() % 10 + 1;

   int randomNumberGuess = 0;
   
   cout << "Guess a number 1-10: ";
   
   cin >> randomNumberGuess;

   bool stillGuessing = true;
   
   while (stillGuessing) // <-- No semicolon
   {

      if (randomNumberGuess > randomGeneratedNumber)  // <-- No semicolon
      {
      cout << "Lowerrrrrrr! Try again!" << endl;
      cin >> randomNumberGuess;
      }
      
      
      else if (randomNumberGuess < randomGeneratedNumber) // <-- No semicolon
      {
      cout << "HIGHER!!! Try again!" << endl;
      cin >> randomNumberGuess;
      }

      else if (randomNumberGuess == randomGeneratedNumber)  //<-- added 'if'
      {
      stillGuessing = false; // <-- All code inside the code block ({..})
      cout << "CORRECT!!!11one!11!" << endl;
      }

   }
   system ("pause");
   

   return 0;

}
User avatar
Odifma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 23
Joined: Mon Feb 21, 2011 4:21 am

Re: Beginner C++ helppp

Post by Odifma »

Thank you Avishaiozeri! i was actually having a problem with another assignment and i came here to see if anyone else posted and you saved me! haha thanks for the great help! :)
Image
Post Reply