Need help with C++ homework!

Hello,

I'm not very familiar with programming at all (I'm a chem major and this class is a course requirement for me), and I'm looking for ANY help or suggestions with my homework assignment!

For this assignment, I was given a program (source code?) to compile and run using Bloodshed; any errors that are found by Bloodshed are to be corrected. Once the program is operational, I must send my teacher a copy of the program (source code?).

I'm pretty lost in regards to the program he has given us. (I've read the chapter!) I'm not sure what is wrong or how to fix it when Bloodshed shows errors in the code. Like I said, any help / suggestions would be greatly appreciated!!

The source code is posted below:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{ int secret_number, guess, count;
cout << "Let's play a guessing game!" << endl << endl;
cout << "I'll pick a number from 0 to 9, inclusive." << endl;
cout << "You have up to 9 tries to guess it." << endl;
cout << "Good luck!" << endl << endl;

//rand() returns an integer between 0 and RAND_MAX
//we use modulo, to reduce the scale to 0 .. 9

secret_number = rand() % 10;

count = 0;

do {
if ( count )
cout << "Sorry, that's not it!" << endl;

cout << "Enter your guess: ";
cin >> guess;

} while ( guess != secret_number ) && ( ++count < 9 ) );

//we need this b/c of the "short-circuit' boolean expression count++;

//we want some space between the guessing and the final result

cout << endl;
if ( guess == secret_number )
{
cout << "You got it in " << count << "guesses!" << endl;
cout << "Good work!" << endl;
}
else
{
cout << "Sorry, you used up all of your guesses!" << endl;
cout << "The secret number was: " << secret_number << endl;
cout << "Better luck next time!" << endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}
i didn't run your code through a compiler but can't seem to see any errors.

what errors is the compiler giving you?
this line:
} while ( guess != secret_number ) && ( ++count < 9 ) );
has an extra ) thrown in, its the only like that threw an error for me.

its a tricky one to catch ;)

also Id change "guesses!" to " guesses!" so when you do get the right answer it doesnt say "you got it in 1guesses!", puts a space between your tries and the word.. extra credit maybe? hah

also the random number ALWAYS seems to be 1..
may want to add a seed value using:

srand(time(NULL));
place that above the line secret_number = rand() % 10;

edit::

Id actually switch around the first line I mentioned with the extra ) to this:
} while (( ++count < 9 ) && guess != secret_number);

With guess != secret_number first, when this statement evaluates to true, count isnt incremented because it doesnt bother to continue checking the rest of the statement, meaning if you guess 5 times and get it right, it will say you got it in 4 guesses, because it doesnt get the chance to increment on the last guess ;)

thanks for the work ;o
Last edited on
I fixed the " guesses!", thanks!

I removed the extra ) from this line:
} while ( guess != secret_number ) && ( ++count < 9 ) );

I changed it to:
} while ( guess != secret_number ) && ( ++count < 9 );

But errors still pop up when I try to compile and run.

The errors popping up are in regards to the line I just fixed:

In function `int main(int, char**)':
expected `;' before '&&' token
expected identifier before'(' token
expected `;' before '(' token

Unfortunately, I have no idea what those error messages even mean!
Ok so I played around with the parentheses in the line:

} while ( ( guess != secret_number ) && ( ++count < 9 ) );

Turns out, it needed another parentheses to close the argument, between while and ( guess.

The program runs now, although the secret_number is 1 every time.

I assumed that the secret number would change every time I ran the program. Is there a way to change that, that isn't too involved?
you removed the wrong ) :P

} while ( guess != secret_number ) && ( ++count < 9 ) );
becomes
} while ( guess != secret_number && ( ++count < 9 ) );
but like I said in my edit, its better to rearrange it like so:
} while (( ++count < 9 ) && guess != secret_number);

As for your errors, because you left the ')' before the && the compiler sees only while(guess != secret_number) and expects a semi colon after the closing ')'
the 'identifier' before the next '(' would be a variable or a function etc. Its wondering what the heck you want to do with whats in the parentheses.





I understand now, thank you for your explanation! It really helped a lot, and again I appreciate your time!
closed account (3qX21hU5)
I believe the whole point of this exercise was for you to start learning what compiler errors look like. So I would suggest you also write some code you know compiles and then start removing random things and see what error message pops up. This way you will know them in the future. Also please please upgrade away from bloodshed. It has not been updated for years and is a very bad one to use. If you like dev c++ look into the Orwell version which recently has been updated. And tell your teacher to do the same.
Ok, I'll start working with some codes.

And I didn't realize Bloodshed was that outdated...

My professor seems pretty stuck on the class using Bloodshed, although I'll mention it. He said something to the effect of "If you use another compiler, I may not be able to help you with any problems you experience." Maybe he's just now fully understanding all of the idiosyncrasies of Bloodshed.

But I'm sure Orwell may be a good bet, thanks for the tip!
I use bloodshed 5.3.0.4 mainly because the errors to me are useful in debugging. I have tried some others and when I run into errors, I normally switch back to bloodshed to debug. You will not go wrong learning to use it.
Your professor probably wants you to use it because it has a simple interface, it's straight forward in troubleshooting and it's rock solid. If you have a problem with a piece of code, it's 99.999% the code, and not the software.

Btw, the last update to bloodshed was Jan 2012, so It's not as bad as Zereo makes out.

his program is missing
1
2
#include <time.h>
#include <cstdlib> 


You'll see this over and over, so put in
1
2
// #include <time.h>
#include <cstdlib> 


see what errors you get

Then try
1
2
#include <time.h>
// #include <cstdlib> 


see what errors you get

Anytime you get that type error, you know your missing something in the includes.

The last comment is for you.

If you use code tags, we can read your code a lot easier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <iostream>
#include <time.h>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{ 
	int secret_number, guess, count;
	cout << "Let's play a guessing game!" << endl << endl;
	cout << "I'll pick a number from 0 to 9, inclusive." << endl;
	cout << "You have up to 9 tries to guess it." << endl;
	cout << "Good luck!" << endl << endl;
	
	//rand() returns an integer between 0 and RAND_MAX
	//we use modulo, to reduce the scale to 0 .. 9

	srand(time(NULL));
	secret_number = rand() % 10;
	count = 0;
	
	do 
	{
		if ( count )
		cout << "Sorry, that's not it!" << endl;
		cout << "Enter your guess: ";
		cin >> guess;
	} while (( guess != secret_number ) && ( ++count < 9 ));
	
	//we need this b/c of the "short-circuit' boolean expression count++;
	//we want some space between the guessing and the final result
	
	cout << endl;
	if ( guess == secret_number )
	{
		cout << "You got it in " << count << " guesses!" << endl;
		cout << "Good work!" << endl;
	}
	else
	{
		cout << "Sorry, you used up all of your guesses!" << endl;
		cout << "The secret number was: " << secret_number << endl;
		cout << "Better luck next time!" << endl;
	}
	

//	system("PAUSE");
return EXIT_SUCCESS;
}
closed account (3qX21hU5)
Umm I believe you are confusing two different Dev C++'s Orwells version which has just been updated and bloodsheds that has not been updated since 2005. Please someone correct me if I'm wrong but I don't think I am.
Here is Bloodsheds website if you don't believe me http://www.bloodshed.net/

Btw, the last update to bloodshed was Jan 2012

Actually if was Feb 2005

Of if your talking about Orwell Dev C++ 5.3.0.4 it was December 2012


it's rock solid.


Bloodshed is no where near rocksolid... here are some reasons why.


Dev-C++ has not been updated since 2005, and is not currently maintained.

The software is very buggy. At the time of my writing there are 340 known bugs that will never be fixed.

It’s hard to get help, because the programming community have moved on to newer software.

Dev-C++ lacks features that are present in more modern solutions.
Code completion, intellisense, and proper debugging facilities (among others) are not provided. These tools can greatly improve the workflow and efficiency of an experienced programmer, and may aid the learning of beginners.

Error messages and the steps required to solve them are poorly documented compared to more modern solutions, and because most programmers have moved on from Dev-C++ it can be difficult (if not impossible) to find anyone who is able to help you. Some problems may not be able to be solved at all.

The compiler included with Dev-C++ is very out-dated, and buggy. An out-dated compiler can result in buggy and inefficient code, and may be damaging to the learning process for a beginner.

The provided “devpack” system is no longer supported by modern libraries. Using external libraries in Dev-C++ can be a confusing and difficult process for beginners who are expecting this simple system to handle it for them.

Again this all applies to Bloodshed version only, not Orwell's. I have not tryed Orwells yet but have heard good things about it.
Last edited on
The Orwell version your talking about is the new release of Dev C++, based on 4.9.9.2. It's a upgrade, even though it's by different team of programmers. I think 4.9.9.2 was by Johan Mes. you can call them different if it makes you happy, but that's like saying Microsoft office 2007 and Microsoft office 2010 is not the same product.

The download location is http://orwelldevcpp.blogspot.com/2012/12/dev-c-5304-released.html

The new version is called Dev C++, has links to the original bloodshed website, and paypal and the Build time was Jan 2012, but release date was Dec 31, 2012.

Instead of correcting you on the rest of your post, maybe you should check out the new version.

I admit, I was hesitant to upgrade, because as I said, the old version was working very well, it's user friendly and has a good screen layout for my personal taste. The interface is one reason I like this program, and it's not over bloated and slow due to all the "additional features".

The new version has a few features that are new/nice and i'm using a few of them. For the most part, it's layout and buttons, keyboard commands are identical, to the older version. I have not had any problems with it thus far.

Last edited on
closed account (3qX21hU5)
The Orwell version your talking about is the new release of Dev C++, based on 4.9.9.2. It's a upgrade, even though it's by different team of programmers. I think 4.9.9.2 was by Johan Mes. you can call them different if it makes you happy, but that's like saying Microsoft office 2007 and Microsoft office 2010 is not the same product.


That is not a good analogy for this. There is a major difference between the two. Like I have stated above one version (bloodshed version) has 300+ bugs that are never going to be fixed, has a WAY outdated compiler included with it that doesn't even support C++11 so it allows you to write inefficient code and buggy code that might not even run on other compilers.

You can not say they are the same thing becasue one has been updated recently and one has not. Maybe in office it doesn't really matter when it was updated but for programming and compilers it does matter and you must keep up with the technology or risk producing buggy and "crap" code.

So Dev C++ is fine to use if you use the new updated Orwell version, but the bloodshed version of Dev C++ is not recommend for use especially when so many other good IDE's and compilers are available.
Topic archived. No new replies allowed.