LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2005, 12:04 PM   #1
lmvent
LQ Newbie
 
Registered: Sep 2005
Posts: 24

Rep: Reputation: 15
rand() and syntax error


This is my code:



#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main(void)
{ int atHome = 0;
int stepsToTry = 5000;
double x = 0;
double y = 0;
double dx = 0;
double dy = 0;
double angle;
const double PI = 4*atan(1.0);
cout << "Will Waldo make it home?\n\n";
cout << "Input an int to start the random number generator: ";
int myStart;
cin >> myStart;
srand(myStart);
for (int i = 0; i < stepsToTry; i = i + 1)
{ rand()
do
{ double angle = 2*PI*rand()/RAND_MAX;
dx = cos(angle);
dy = sin(angle);
}while (atHome = 0);
do
{ y = y + dy;
}while (y >= -10 && y <= 10);
do
{ x = x + dx;
}while (x >= -10 && x <= 10);
if (x>=8 && x<=10)
{ if(y >= 8 && y <= 10)
{ atHome = 1;
}
}
}
if (atHome)
{ cout << "Waldo made it home after taking " << i << "steps!\n";
}
else
{ cout << "Waldo is still lost after taking" << stepsToTry << "steps\n\n";
cout << "Please play again and try to get Waldo home!\n";
}
return 0;
}

And this is my error message:

waldo.cpp: In function 'int main()':
waldo.cpp:24: synatx error before 'do':
waldo.cpp: At global scope:
waldo.cpp:41: syntax error before 'if':
waldo.cpp:46: syantax error before '<<' token:

I'm not exactly sure how to use rand() and I bet that's my problem. Could someone help please?
 
Old 10-06-2005, 12:15 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Your call to rand() should have a semi-colon (;) after it. That's likely the source of your syntax error. rand() returns an integer between 0 and RAND_MAX. An easy way to get a random number between 0 and some_number is to do:
Code:
int num = rand() % upper_limit;
If upper_limit is 50 it will return a number between 0 and 49 inclusive. The random number generator needs to be seeded if you want a different random number every time. srand() is used to do this. A common seed is the current time. You only want to call srand() once in your program, so somewhere before your first call to rand() you might do: srand(time(0));
 
Old 10-06-2005, 12:21 PM   #3
lmvent
LQ Newbie
 
Registered: Sep 2005
Posts: 24

Original Poster
Rep: Reputation: 15
Okay I did that and the syntax errors went away. Now I get:

waldo.cpp: In function 'int main()':
waldo.cpp:41: name lookup of 'i' changed for new ISO 'for' scoping
waldo.cpp:21: using obsolete binding at 'i'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ syntax error...HELP!! lmvent Programming 3 09-30-2005 02:40 PM
Syntax error phernand Programming 1 05-10-2005 12:04 PM
Many errors when 'make'ing (example: error: syntax error before `::' token) darkblade Linux - Software 5 03-02-2005 03:00 PM
C++ syntax error before :: token HELP, i cant find the syntax error :( qwijibow Programming 2 12-14-2004 06:09 PM
ERROR running make when installing Nvidia drivers (syntax error) randyriver10 Linux - Software 5 02-21-2004 04:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration