Unable to find the syntax error for my assignment

Havin2 2 errors while designing a do while loop to creat a guessing ga

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 6794 Views - Last Post: 21 September 2009 - 12:18 AM Rate Topic: -----

#1 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

Unable to find the syntax error for my assignment

Posted 19 September 2009 - 09:38 PM

I'm hoping someone can help me with my code for my assignment is that is due this week. I'm on my last exercise and I'm stuck and can't find the answer why. I don't know whether its a logic error or not because its telling me its syntax but I can locate the syntax error for the life of me. Here is my code so far it must state a do while loop. Any help is appreciated thanks;)

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

int main()
{
	srand((int)time(NULL));

	//use const for upper and lower bound
		const int upperLimit = 100;
		const int lowerLimit = 1;
		int guessNumber = 0;
		int theCount = 0;
		int myRandomNumber = 0;
   do {

int myRandomNumber = (rand() % (upperLimit - lowerLimit + 1)) + lowerLimit;  //Generate a random number
	  cout << myRandomNumber << endl;
													//Begin loop
		cout<< endl << "Enter a number: ";
	   cin >> guessNumber;
		cout <<endl << "You have entered " << theCount << "integers.";   //Enter user's guess

 }while  (guessNumber != myRandomNumber);

   { if (guessNumber > myRandomNumber);
						 
							  cout << "Too High, Try Again!";   //Display message
			   if (guessNumber < myRandomNumber);
						   
							   cout << "Too Low, Try Again!";	//Display message					  
							 
   }															// End loop

 guessNumber == myRandomNumber;
						 // Display message and exit loop
		cout << "You've guessed wisely! ";

	  cout << endl;

																//  Display number of guesses
		cout << "The number was " " it took you " " guesses.";
		cout << endl;
		cout << "The number was " " you gave up after " " guesses.";
		cout << endl;
																	//Stop
return 0;
}




oh and please ignore the whitespace and indents I have'nt made it look nice just yet LOL;)

** Edit ** :code:


here are the 2 errors

Error 1 error C2144: syntax error : '__w64 unsigned int' should be preceded by ';' c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h 19
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h 19

This post has been edited by Gmoney93079: 19 September 2009 - 09:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to find the syntax error for my assignment

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 09:44 PM

** Renamed tittle to be more descriptive **

View PostGmoney93079, on 19 Sep, 2009 - 10:38 PM, said:

I'm on my last exercise and I'm stuck and can't find the answer why. I don't know whether its a logic error or not because its telling me its syntax but I can locate the syntax error for the life of me.

Any chance you can give us some helpful information, such as what the error is :)

You do realize that by filling "myRandomNumber" with the output from rand() in each instance of your while loop, that the variable "myRandomNumber" is gong to have a new random number for each guess.

  do {
	int myRandomNumber = (rand() % (upperLimit - lowerLimit + 1)) + lowerLimit;  //Generate a random number
	   cout << myRandomNumber << endl;
													//Begin loop
		cout<< endl << "Enter a number: ";
		cin >> guessNumber;
		cout <<endl << "You have entered " << theCount << "integers.";   //Enter user's guess
 }while  (guessNumber != myRandomNumber);


I'm curious at what this line is supposed to do :

guessNumber == myRandomNumber;


The errors that you provided are from the file "sourceannotations.h" & not the above code. It compiled for me with out error or warning (under gcc).
Was This Post Helpful? 0
  • +
  • -

#3 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 09:57 PM

View Postno2pencil, on 19 Sep, 2009 - 08:44 PM, said:

** Renamed tittle to be more descriptive **

View PostGmoney93079, on 19 Sep, 2009 - 10:38 PM, said:

I'm on my last exercise and I'm stuck and can't find the answer why. I don't know whether its a logic error or not because its telling me its syntax but I can locate the syntax error for the life of me.

Any chance you can give us some helpful information, such as what the error is :)

You do realize that by filling "myRandomNumber" with the output from rand() in each instance of your while loop, that the variable "myRandomNumber" is gong to have a new random number for each guess.

  do {
	int myRandomNumber = (rand() % (upperLimit - lowerLimit + 1)) + lowerLimit;  //Generate a random number
	   cout << myRandomNumber << endl;
													//Begin loop
		cout<< endl << "Enter a number: ";
		cin >> guessNumber;
		cout <<endl << "You have entered " << theCount << "integers.";   //Enter user's guess
 }while  (guessNumber != myRandomNumber);





No I didn't thats definitely an error in my logic for sure. What I am trying to do is have a user input a number and if it is right output you guessed wisely!, if they are wrong then it outputs either "Too High try again' or Too low Try again'" and the user can enter a -1 to quit and then it will explain to the user the random number was x and you took this many guesses whether they guess correctly or if they give up.

What I believed was going to happen by changing the rand was that everytime a user would start the program that it would produce a different random number not the same.

I'm really stuck because of the pseudocode that was given to me and this do while loop I have to use

I figured the best thing for me to do was a :

{
do (while correct)
produce a random number, have the user guesss, and output you guessed wisely the number was x it took this many guesses

while not correct
if too high say too high try again
if too low say too low try again
else (which would be negative 1)
display you gave up
the number was x you gave up after x guesses
press any key to continue

But everytime I bring up the program I want the program to have a different random number

would you say my logic in english would be correct?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:09 PM

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

int main(void) {
	srand((int)time(NULL));
	//use const for upper and lower bound
	const int upperLimit = 100;
	const int lowerLimit = 1;
	int guessNumber = 0;
	int theCount = 0;
	int myRandomNumber = 0;
	myRandomNumber = (rand() % (upperLimit - lowerLimit + 1)) + lowerLimit;
	while(guessNumber != myRandomNumber) {
	  cout << myRandomNumber << endl;
	  cout<< endl << "Enter a number: ";
	  cin >> guessNumber;
	  theCount++;
	  cout <<endl << "You have entered " << theCount << " integers." << endl;
//Enter user's guess

	  if (guessNumber > myRandomNumber) {
		cout << "Too High, Try Again!" << endl;   //Display message
	  }
	  if (guessNumber < myRandomNumber) {
		cout << "Too Low, Try Again!" << endl;	//Display message

	  }
	}															// End loop
	// Display message and exit loop
	cout << "You've guessed wisely! " << endl;

	//  Display number of guesses
	cout << "The number was " << myRandomNumber << " it took you " << theCount <
<" guesses.";
	cout << endl;
																	//Stop
	return 0;
}


This gives the following output :

Quote

>$./guess
19

Enter a number: 11

You have entered 1 integers.
Too Low, Try Again!
19

Enter a number: 14

You have entered 2 integers.
Too Low, Try Again!
19

Enter a number: 99

You have entered 3 integers.
Too High, Try Again!
19

Enter a number: 23

You have entered 4 integers.
Too High, Try Again!
19

Enter a number: 19

You have entered 5 integers.
You've guessed wisely!
The number was 19 it took you 5 guesses.


You had a couple of problems.

1 being the random number generation that I pointed out in the previous post. The other being that you didn't increase the variable theCount. The most damaging error was your if conditional statements were ended with semi-colons. So their syntax (though not producing an error) was incorrect.
Was This Post Helpful? 0
  • +
  • -

#5 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:11 PM

View PostGmoney93079, on 19 Sep, 2009 - 08:57 PM, said:

View Postno2pencil, on 19 Sep, 2009 - 08:44 PM, said:

** Renamed tittle to be more descriptive **

View PostGmoney93079, on 19 Sep, 2009 - 10:38 PM, said:

I'm on my last exercise and I'm stuck and can't find the answer why. I don't know whether its a logic error or not because its telling me its syntax but I can locate the syntax error for the life of me.

Any chance you can give us some helpful information, such as what the error is :)

You do realize that by filling "myRandomNumber" with the output from rand() in each instance of your while loop, that the variable "myRandomNumber" is gong to have a new random number for each guess.

  do {
	int myRandomNumber = (rand() % (upperLimit - lowerLimit + 1)) + lowerLimit;  //Generate a random number
	   cout << myRandomNumber << endl;
													//Begin loop
		cout<< endl << "Enter a number: ";
		cin >> guessNumber;
		cout <<endl << "You have entered " << theCount << "integers.";   //Enter user's guess
 }while  (guessNumber != myRandomNumber);





No I didn't thats definitely an error in my logic for sure. What I am trying to do is have a user input a number and if it is right output you guessed wisely!, if they are wrong then it outputs either "Too High try again' or Too low Try again'" and the user can enter a -1 to quit and then it will explain to the user the random number was x and you took this many guesses whether they guess correctly or if they give up.

What I believed was going to happen by changing the rand was that everytime a user would start the program that it would produce a different random number not the same.

I'm really stuck because of the pseudocode that was given to me and this do while loop I have to use

I figured the best thing for me to do was a :

{
do (while correct)
produce a random number, have the user guesss, and output you guessed wisely the number was x it took this many guesses

while not correct
if too high say too high try again
if too low say too low try again
else (which would be negative 1)
display you gave up
the number was x you gave up after x guesses
press any key to continue

But everytime I bring up the program I want the program to have a different random number

would you say my logic in english would be correct?




here is the pseudocode I was provided:

Start
Generate a random number

Begin loop
Enter user's guess
If guess is right
Display message and exit loop
If guess is high
Display message
If guess is low display message
Display message
Increment counter
End loop

Display number of guesses
Stop



I believe that my own english logic is a better pseudocode and I should try to start over and follow it instead.

The question is though, in my own logical english, am I doing the loops in the right order?
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:15 PM

View PostGmoney93079, on 19 Sep, 2009 - 11:11 PM, said:

The question is though, in my own logical english, am I doing the loops in the right order?

No. The logic that you had (as per pseudo code :

Start
Begin loop
Generate a random number
Enter user's guess
Display message and exit loop
End Loop

If guess is high
stop
If guess is low
stop

Display number of guesses (though this code was incorrect)
Stop

You never incremented the counter & your loop structure was incorrect (as previously pointed out).
Was This Post Helpful? 0
  • +
  • -

#7 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:21 PM

so the above output is somewhat of what I'm trying to do but I do not want the user to see the "you have entered 5 integers" I definitely need to remove that statement and not show the count until the end of the program. Secondly, I though by putting in theCount++ is what increased the variable theCount? Lastly, I don't understand the first post you made regarding generating the random number correctly. See in our lecture she stated that this code: int myRandomNumber = 1 + rand() % (upperLimit - lowerLimit + 1); //generate the random number
is what generates the random number, and thats the code we need to use in the assignment. Is the above code correct?
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:28 PM

1st : Can you please quit including the entire reply? If you are replying to the previous post (which you are) there is no need to include my text.

View PostGmoney93079, on 19 Sep, 2009 - 11:21 PM, said:

Secondly, I though by putting in theCount++ is what increased the variable theCount?

Correct. But give me the line number in YOUR code where you provided theCount++, because I sure didn't see it.

Quote

Lastly, I don't understand the first post you made regarding generating the random number correctly.
All I did was move the random number generation out of the while loop.

The code you have does this

enter loop
generate random number
get number from user
while they don't match
generate random number
get number from user
while they don't match
generate random number
get number from user
while they don't match
generate random number
get number from user
while they don't match
generate random number
get number from user
while they don't match
generate random number
get number from user
while they don't match
...

and so on.
Was This Post Helpful? 0
  • +
  • -

#9 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:29 PM

oh and do I need to include#include <stdlib.h> in my code, or would #include <iostream> and #include <time.h>, have worked without the #include <stdlib.h> ?

ps sorry for asking so many questions I'm just really lost now, and need some help understanding when I input the code what each block is doing. I've been generating code correctly when I write my own pseudocode, but I'm trying to think like a programmer who has been given pseudocode and put in the correct c++ code, also I appreciate all the help;) Hope you understand too, I'm trying to do 2 things at once, this and write an argumentative essay after just getting off work about 2 hours ago LOL:)
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:33 PM

View PostGmoney93079, on 19 Sep, 2009 - 11:29 PM, said:

oh and do I need to include#include <stdlib.h> in my code, or would #include <iostream> and #include <time.h>, have worked without the #include <stdlib.h> ?

The headers in your code are C style headers, the headers that you want for C++ coding are :

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

Was This Post Helpful? 0
  • +
  • -

#11 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:41 PM

Sorry didn't know I was supposed to do fast reply I hope this doesn't show it all over and over again. I just joined and was debating to cause I wanted to figure this out on my own but as usual I'm pressured with time. So then .... how would I produce the output that you showed except the >$./ guess, and showing the integers?

I want it to produce

Welcome to the guess mm number game!

Enter a guess (-1 to exit):

Then after the user guesses it says you guessed wisely, or too high try again, or too low try again

from what it sounds like is I need to move that generate number code out of the do loop

but do I want it to do this if correct, and while not correct do this or this, or else do this.
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:42 PM

Did you run this code that I provided?
Was This Post Helpful? 0
  • +
  • -

#13 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:46 PM

Why did they tell us to use the headers that I had?

no I didn't yet, I will do that now. I believe the code you provided is the one that showed the output underneath it correct? kind of got myself lost now by showing each reply eveerytime, I apologize:)
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:47 PM

View PostGmoney93079, on 19 Sep, 2009 - 11:46 PM, said:

Why did they tell us to use the headers that I had?

I don't know. Maybe lack of concern regarding the languages & specifics. Either set of headers will work properly.
Was This Post Helpful? 0
  • +
  • -

#15 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 258
  • Joined: 19-September 09

Re: Unable to find the syntax error for my assignment

Posted 19 September 2009 - 10:49 PM

yes I just did and it gave me this error


Error 1 error C2059: syntax error : '<' c:\documents and settings\hp_administrator.wilson\my documents\visual studio 2008\projects\wilsoniii_lab3_exercise3\wilsoniii_lab3_exercise3\wilsoniii_lab3_exercise3.cpp 36
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3