Need Help Now. Need program working for tomorrow

#include <iostream>
#include <string>
#include <ctime>
#include <vector>

std::string State[] = { "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming" };
std::string Capital[] ={"Montgomery","Juneau", "Phoenix", "Little Rock","Sacramento", "Denver", "Hartford", "Dover", "Tallahassee","Atlanta", "Honolulu", "Boise", "Springfield", "Indianapolis","Des Moines", "Topeka", "Frankfort", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing", "St. Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Concord", "Trenton", "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus", "Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin","Salt Lake City", "Montpelier", "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne" };

double line_number = 0;

const int iState = sizeof (State) / sizeof (std::string);
const int iCapital = sizeof (Capital) / sizeof (std::string);

std::string inputString;

std::vector<std::string> StatesPicked;
std::vector<std::string> CapitalsPicked;

double Number = 1;

int iStateSelected;

int i;

bool multiCompare (std::vector<std::string>& vec, std::string value)
{
for (i = 0; i < vec.size (); i++)
{
std::cout << vec[i] << std::endl;
if (vec[i] == value)
{
std::cout << value << " state being tried" << std::endl;
return true;
}
}
return false;
}

int main()
{
while(true)
{
srand(time(NULL));
line_number = Number; //rand () % Number;

if(line_number == 1)
{
//the state selected randomly out of all the states
iStateSelected = rand() % 49; //iState;
//
if(StatesPicked.size() == 51)
{
std::cout << "states finished" << std::endl;
Number = 2;
}
else
{
if(!multiCompare(StatesPicked, State[iStateSelected]))
{
//sends the last state picked to the end of the StatesPicked
StatesPicked.push_back(State[iStateSelected]);

std::cout << StatesPicked.size() << std::endl;

std::string CapitalAnswer = Capital[iStateSelected];

std::cout << "What is the Capital of " << State[iStateSelected] << ": " << std::endl;

std::getline (std::cin, inputString, '\n');

if(inputString == CapitalAnswer)
{
std::cout << CapitalAnswer << " is the Capital of " << State[iStateSelected] << std::endl << std::endl;
}
else
{
std::cout << inputString << " is not the Capital of " << State[iStateSelected] << std::endl;
std::cout << "The Capital of " << State[iStateSelected] << " is " << CapitalAnswer << std::endl << std::endl;
}
}
else
{
std::cout << "choosing another state" <<std::endl;
iStateSelected = rand() % 49; //iState;
std::cout << iStateSelected << std::endl;
}
}
}
}
}

so I want this to run and pick a state randomly and ask what the capital of it is. I have a function that checks to see if the state chosen (goes through a vector of previously chosen states) is equal to any in the vector. I have a problem when they are equal. it will (instead of picking a new number randomly, it picks the same one and tests the same state. so i need the computer to always pick a different number. for example, right now it goes
picks: 24
checks...
24 found
picks another number: 24
check...
24 found
and repeats like that for a while then eventually picks a different number. please help me ASAP i need this program done or working by tomorrow.
I doubt that it will pick the same number EVERYTIME, I do often see repeated selections when using "rand()" though. If you're worried about it why not call "rand()" in a loop until new != old?
1
2
3
4
5
while(true)
{
    srand(time(NULL));
    ...
}

Place the srand call at the top of main(). srand sets where the pseudo-random sequence starts. http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

1
2
3
4
5
srand(time(NULL));
while(true)
{
    ...
}
Last edited on
Topic archived. No new replies allowed.