homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam

The Geek Culture Forums!


Post New Topic  New Poll  Post A Reply
my profile | directory login | | search | faq | forum home
  next oldest topic   next newest topic
» The Geek Culture Forums!   » Techno-Talking   » Math-a-holics and Code Junkies   » C++

 - UBBFriend: Email this page to someone!    
Author Topic: C++
Sam K
Newbie Larva
Member # 2836

Rate Member
Icon 9 posted July 11, 2004 02:23      Profile for Sam K   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
code:
  #include <iostream>
#include <string>
using namespace std;

int main()
{
string quote[5] = { quote1, quote2, quote3, quote4, quote5};

int i, r;
for(i = 1; i < 5; i++)
{
r = (rand() % 4) + 1;
}

cout << quote[r] << endl;

return 0;
}

It's a-beroken [Frown]

heh, okay. For the record I am new to C++...

--------------------
Learning so much, knowing so little
-----
CannedLaughter.net
-----
[email protected]

Posts: 7 | From: England | Registered: Jul 2004  |  IP: Logged
The Famous Druid

Gold Hearted SuperFan!
Member # 1769

Member Rated:
4
Icon 1 posted July 11, 2004 06:27      Profile for The Famous Druid     Send New Private Message       Edit/Delete Post   Reply With Quote 
Ok, I'm assuming you've just omitted the declaration and initialisation of quote1 .. quote5 for brevity.

And I'm also assuming that the objective is to print one of those quotes, at random.

Firstly, the expression r = (rand() % 4) + 1 is wrong, it'll give you a number in the range 1..4, when you really want 0..4, so try

r = (rand() % 5)

I'm puzzled as to why you call rand in a loop 4 times, throwing the first 3 results away, seems pretty pointless.

You'll find that you always get the same quote being printed, this is because you're not initialising the random number sequence, which means you get the 'default' initialisation every time. Try putting

srand(time(NULL));
at the start of your program (you'll need to include time.h), this will give you a new random sequence every time you run it.


Hope this helps, if it does, you are morally obliged to donate 5 quid to the Red Cross. [Wink]

--------------------
If you watch 'The History Of NASA' backwards, it's about a space agency that has no manned spaceflight capability, then does low-orbit flights, then lands on the Moon.

Posts: 10702 | From: Melbourne, Australia | Registered: Oct 2002  |  IP: Logged
quantumfluff
BlabberMouth, a Blabber Odyssey
Member # 450

Member Rated:
5
Icon 1 posted July 11, 2004 10:30      Profile for quantumfluff     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Sam K:
It's a-beroken

In what way is it broken? Won't compile? Dumps core? Prints unexpected results?
Posts: 2902 | From: 5 to 15 meters above sea level | Registered: Jun 2000  |  IP: Logged
spungo
BlabberMouth, a Blabber Odyssey
Member # 1089

Member Rated:
4
Icon 1 posted July 11, 2004 11:16      Profile for spungo     Send New Private Message       Edit/Delete Post   Reply With Quote 
If it's beroken, then you should befix it, already. Unless your berain dead. [Wink]

--------------------
Shameless plug. (Please forgive me.)

Posts: 6530 | From: Noba Scoba | Registered: Jan 2002  |  IP: Logged
Sam K
Newbie Larva
Member # 2836

Rate Member
Icon 1 posted July 11, 2004 13:05      Profile for Sam K   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
Spungo: I'm berain [Razz] er...."ded"
Druid: Thanks for the srand..., but its the:
code:
     string quote[5] = { quote1, quote2, quote3, quote4, quote5}; 

line that is giving me the problem: [Confused] [Confused]

 -

and...
quantumfluff: Won't compile, errors, see above ^

--------------------
Learning so much, knowing so little
-----
CannedLaughter.net
-----
[email protected]

Posts: 7 | From: England | Registered: Jul 2004  |  IP: Logged
quantumfluff
BlabberMouth, a Blabber Odyssey
Member # 450

Member Rated:
5
Icon 1 posted July 11, 2004 13:34      Profile for quantumfluff     Send New Private Message       Edit/Delete Post   Reply With Quote 
Then your program listing *isn't* an abstraction, but the complete thing. My advice is to read a C++ reference first and learn the syntax.
Posts: 2902 | From: 5 to 15 meters above sea level | Registered: Jun 2000  |  IP: Logged
The Famous Druid

Gold Hearted SuperFan!
Member # 1769

Member Rated:
4
Icon 1 posted July 11, 2004 15:33      Profile for The Famous Druid     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Sam K:

Druid: Thanks for the srand..., but its the:
code:
     string quote[5] = { quote1, quote2, quote3, quote4, quote5}; 

line that is giving me the problem: [Confused] [Confused]

Ok, so what the compiler is saying is WTF is quote1, quote2, quote3, quote4, quote5 ?"

You need a few lines like this
const string quote1="Iraq has thousands of tons of WMDS, primed and ready to launch at 45 minutes notice";
const string quote2 = "Saddam was involved in the September 11 attacks on the USA";
const string quote3 = "I can cut taxes for the rich, increase spending, and still balance the budget";

--------------------
If you watch 'The History Of NASA' backwards, it's about a space agency that has no manned spaceflight capability, then does low-orbit flights, then lands on the Moon.

Posts: 10702 | From: Melbourne, Australia | Registered: Oct 2002  |  IP: Logged
angryjungman

Solid Nitrozanium SuperFan!
Member # 2434

Member Rated:
5
Icon 1 posted July 11, 2004 15:43      Profile for angryjungman   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
I just wrote a program like this in Python....

--------------------
Meh.

Posts: 634 | From: princeton, nj | Registered: Nov 2003  |  IP: Logged
Sam K
Newbie Larva
Member # 2836

Rate Member
Icon 1 posted July 12, 2004 11:31      Profile for Sam K   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by The Famous Druid:
quote:
Originally posted by Sam K:

Druid: Thanks for the srand..., but its the:
code:
     string quote[5] = { quote1, quote2, quote3, quote4, quote5}; 

line that is giving me the problem: [Confused] [Confused]

Ok, so what the compiler is saying is WTF is quote1, quote2, quote3, quote4, quote5 ?"

You need a few lines like this
const string quote1="Iraq has thousands of tons of WMDS, primed and ready to launch at 45 minutes notice";
const string quote2 = "Saddam was involved in the September 11 attacks on the USA";
const string quote3 = "I can cut taxes for the rich, increase spending, and still balance the budget";

Aaaaah, Druid, at the moment the code is supposed to consist of 5 quotes that it can pull at random, the quotes ARE quote1, quote2 ect.
so the code (if working) should throw out a random quote, which ATM would be quote1, quote2 ....quote5

--------------------
Learning so much, knowing so little
-----
CannedLaughter.net
-----
[email protected]

Posts: 7 | From: England | Registered: Jul 2004  |  IP: Logged
Sam K
Newbie Larva
Member # 2836

Rate Member
Icon 1 posted July 12, 2004 11:39      Profile for Sam K   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
I know, double post....

But I took in what Druid had just said and now the code works [Big Grin] [Big Grin] [Big Grin] [Big Grin]

code:
  #include <iostream>
#include <time.h>
#include <string>
using namespace std;

int main()
{
char YorN;
do{

srand(time(NULL));

const string quote1="Iraq has thousands of tons of WMDS, primed and ready to launch at 45 minutes notice";
const string quote2 = "Saddam was involved in the September 11 attacks on the USA";
const string quote3 = "I can cut taxes for the rich, increase spending, and still balance the budget";
const string quote4 = "random";
const string quote5 = "not as random";

string quote[5] = { quote1, quote2, quote3, quote4, quote5};



int i, r;
for(i = 1; i < 5; i++)
{
r = (rand() % 5);
}

cout << quote[r] << endl;

cout << "\nWould you like to enter another problem "
<< "(Y or N)? ";
cin >> YorN;
cout << endl << endl;
cin.get();
}while( YorN == 'Y');

return 0;
}

Plus a while loop to keep the quotes coming

THANK YOU DRUID!!! [hearts]

--------------------
Learning so much, knowing so little
-----
CannedLaughter.net
-----
[email protected]

Posts: 7 | From: England | Registered: Jul 2004  |  IP: Logged
ooby
Highlie
Member # 2603

Member Rated:
4
Icon 1 posted July 12, 2004 12:17      Profile for ooby     Send New Private Message       Edit/Delete Post   Reply With Quote 
I think you are still generating 4 random numbers and then throwing them away before using the last one.

Me, I'd throw that rand right into the quote[] and the quote into the cout, but that's a bit spaghettiesque.
something like: cout>>quote[rand(whatever)];

--------------------
"haven't you ever wondered if there's more to life than being really, really, rediculously good looking?"

Posts: 680 | From: South Jersey | Registered: Feb 2004  |  IP: Logged
dragonman97

SuperFan!
Member # 780

Member Rated:
4
Icon 1 posted July 12, 2004 15:20      Profile for dragonman97   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
Man, I very nearly wrote something like that this weekend [Razz] .

In fact, I did write it, with only 2 cases, but then I decided to do something else, and I think it still weighed in with far fewer lines [Razz] .

Of course, I probably would have just written the following in Perl [Big Grin] :
code:
#!/usr/bin/perl -w
use strict;

my @strings=(
"foo",
"bar",
"baz",
"xyzzy");

my $out_ind=int rand scalar @strings;
print "$strings[$out_ind]\n";

The longest part of that is the string population, which I broke up onto many lines for clarity. Yes, I believe that is readable Perl code [Wink] .

--------------------
There are three things you can be sure of in life: Death, taxes, and reading about fake illnesses online...

Posts: 9345 | From: Westchester County, New York | Registered: May 2001  |  IP: Logged
GameMaster
BlabberMouth, a Blabber Odyssey
Member # 1173

Member Rated:
4
Icon 1 posted July 12, 2004 15:57      Profile for GameMaster   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
code:
#include <iostream>
#include <time.h>
#include <string>
using namespace std;

int main()
{
char YorN;

//this only needs to happen once,
//and should be out of the loop.
srand(time(NULL));

string quote[5] = {"Iraq has thousands of tons of WMDS, primed and ready to launch at 45 minutes notice", "Saddam was involved in the September 11 attacks on the USA", "I can cut taxes for the rich, increase spending, and still balance the budget", "random", "not as random"};

do
{

cout << quote[(rand() % 5)] << endl;
//this is what you are looking to do.

do
{
cout << "\nWould you like to enter another problem "
<< "(Y or N)? ";
cin >> YorN;
}while(YorN != 'Y' || YorN != 'N')
cout << endl << endl;
//cin.get();//the >> opertator
//should be suficent
}while( YorN == 'Y');

return 0;
}



--------------------
My Site

Posts: 3038 | From: State of insanity | Registered: Mar 2002  |  IP: Logged
ooby
Highlie
Member # 2603

Member Rated:
4
Icon 1 posted July 13, 2004 05:23      Profile for ooby     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by dragonman97:
Man, I very nearly wrote something like that this weekend [Razz] .

In fact, I did write it, with only 2 cases, but then I decided to do something else, and I think it still weighed in with far fewer lines [Razz] .

Of course, I probably would have just written the following in Perl [Big Grin] :
code:
#!/usr/bin/perl -w
use strict;

my @strings=(
"foo",
"bar",
"baz",
"xyzzy");

my $out_ind=int rand scalar @strings;
print "$strings[$out_ind]\n";

The longest part of that is the string population, which I broke up onto many lines for clarity. Yes, I believe that is readable Perl code [Wink] .
mmmm.. sweet sweet Perl.

--------------------
"haven't you ever wondered if there's more to life than being really, really, rediculously good looking?"

Posts: 680 | From: South Jersey | Registered: Feb 2004  |  IP: Logged
Sam K
Newbie Larva
Member # 2836

Rate Member
Icon 1 posted July 13, 2004 10:10      Profile for Sam K   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by dragonman97:
In fact, I did write it, with only 2 cases, but then I decided to do something else, and I think it still weighed in with far fewer lines [Razz] .

sw33t t-shirt there dman [Wink]

--------------------
Learning so much, knowing so little
-----
CannedLaughter.net
-----
[email protected]

Posts: 7 | From: England | Registered: Jul 2004  |  IP: Logged


All times are Eastern Time  
Post New Topic  New Poll  Post A Reply Close Topic    Move Topic    Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:

Contact Us | Geek Culture Home Page

© 2018 Geek Culture

Powered by Infopop Corporation
UBB.classicTM 6.4.0



homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam