Single Random Output from Given List

I'm making a simple police-style terminal to be put into a game. I want to be able to type in a name (without any formatting concerns) and have a status (Wanted Y/N, Valid DL Y/N) put out by the program. Here's what I have so far, although I know it doesn't work. Can anyone help with this, or is it a lost cause?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
int nam;
 
 char status[4] = {'WANTED: NO, VALID DL: NO', 'WANTED: NO, VALID DL: YES', 'WANTED: YES, VALID DL: NO', 'WANTED: YES, VALID DL: YES'};
 
 cout << "ENTER A NAME TO SEARCH FOR \n";

cin >> nam;
 
 cout << status << endl;
 



 cin.get();
 system("PAUSE");
}
How are the entered name and status connected?
If you're not really interested in a correlation between the name and response, as of yet, you can do it this way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Police Radio.cpp : main project file.

#include <iostream>
#include <string>
#include <ctime>
#include <sstream> 

using namespace std;

int main()
{
string nam;
// nam is a string since you're looking for a name, not a number
int response;
// response is for a random number, 0 to 3
 string status[4] = {"WANTED: NO, VALID DL: NO", "WANTED: NO, VALID DL: YES", "WANTED: YES, VALID DL: NO", "WANTED: YES, VALID DL: YES"};
// char is for single characters 
 cout << "ENTER A NAME TO SEARCH FOR \n";

cin >> nam;
srand(static_cast<int>(time(0))); // seed the random number generator
response = rand()%4;
cout << status[response] << endl;
 
cin.get();
system("PAUSE");
}
Last edited on
Whitenite, thank you. That is perfect. I don't need any real connection to the name/result. I just want it to be random, as you have made it.

One thing, though. I would like to have it ask "SEARCH FOR ANOTHER NAME?" (Y/N). If yes, reset. If no, close.

Thanks!
You should use a struct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

struct People
{
    std::string name , wanted , valid_dl;
};

std::ostream& operator<<( std::ostream &stm , const People &ppl )
{
    return( stm << "Name: " << ppl.name << "\nWanted: " << ppl.wanted << "\nValid DL: " << ppl.valid_dl );
}

int main()
{
    People fred{ "fred" , "No" , "No" } , bob = { "bob" , "Yes" , "Yes" } , bill;
    bill.name = "Bill";
    bill.wanted = "Yes";
    bill.valid_dl = "No";
    std::cout << fred << std::endl << bob << std::endl << bill << std::endl;
}
Topic archived. No new replies allowed.