Jump to content

Not Giving Up But Need Help


Archerzz

Recommended Posts

none of this is changing the end result is there something wrong with my formula?

514737[/snapback]

 

Ok, try this:

 

where you have

 

cout << fortunes[number];

 

try putting in ths:

 

cout << fortunes[0];
cout << fortunes[1];
cout << fortunes[2];
cout << fortunes[3];

 

Just make sure your strings are stored the way you think they are. If running that spits all four variables in order like you typed them, then you're good.

 

Also...

 

I don't think this is right

 

(rand() % 4)

 

As far as I know, rand() should return a decimal between 0 and 1. So if you want it to give you 0,1,2,or 3, shouldn't it be (rand() * 3)?

 

Try putting a cout statement in there to spit out the random number after it's generated and run it a few times to make sure your randoms are what you want.

 

See a pattern here?

 

Just start adding cout statements everywhere. That's how I debug. Make sure things are how you think they are every step of the way. Doing this works with any program.

Share this post


Link to post
Share on other sites

using

(rand() * 3); gives me a error in windows??????????

 

 

Ok I dont think I did the string right because when I change

 

cout << fortunes[number]

to

cout << fortunes[2]

 

it still prints out the same one as before and that is the last one in the string...

 

ok when using an array like that I have to declare it outside of the main function???

 

 

 

WOOOOOOOOTTTTTTTTTT IT WORKS. All I had to do what move

 

stirng fortunes[4] = ....

 

outside the main function and it works.

 

WOW so simple :D

 

Here is complete code :D

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

string fortunes[4] = {"Only Death Awaits you", "Your 1337ness is a inspiration", "On the binary finger shirt everyone is giving you 00000100", "Watch out for that car"};

int main()
{ 
   srand(time(NULL));
   
   cout << "Press any button to reveal your fortune....(wooo mystical)\n\n";
   system ("pause");
   
   cout << "\n";
   cout << "Your Fortune is.....\n\t";
   
   int number = (rand() % 4);
   
   cout << fortunes[number];
   
   return 0;
   
}

Share this post


Link to post
Share on other sites

using

(rand() * 3); gives me a error in windows??????????

514754[/snapback]

 

EDIT:

 

I installed DevCPP, couldn't help it :P

 

Ok, the random number line you had was right. This gives me 0-3 as output

 

number = rand() % 4;

 

 

However,

 

   
cout << fortunes[0] << "\n";
cout << fortunes[1] << "\n";
cout << fortunes[2] << "\n";
cout << fortunes[3] << "\n";

 

Gives the same output four times, so the way you're population your array of strings is not working, I think.

 

EDIT EDIT#

 

This should work:

 

#include <iostream>
#include <string>
//#include <stdlib>
using namespace std;

int main()
{
  srand(time(NULL));
  
  int number;
  string fortunes[4] = {"Only Death Awaits you", "Your 1337ness is a inspiration", "On the binary finger shirt everyone is giving you 00000100", "Watch out for that car"};
  
  cout << "Press any button to reveal your fortune....(wooo mystical)\n\n";
  system ("pause");
  
  cout << "\n";
  cout << "Your Fortune is.....\n\t";
  
  number = rand() % 4;
  
  cout << fortunes[number] << "\n";

  system ("pause");
  
  return 0;
  
}

 

When you declare and populate your array of strings, you need to use "{" and "}", not "(" and ")".

Share this post


Link to post
Share on other sites

Glad it's working now, but moving that declaration outside the main function.... I don't think that's making any difference. The reason it's working now is because of the curly brackets {}.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...