How do i write a progam that i input a group of strings and the computer would randomly give me a string from that group?
i tired to do this, and then i get stuck lol.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
string Word1,Word2,Word3,Word4,Word5;
cout<<"enter you subjects.\n";
cin>>Word1;
cin>>Word2;
cin>>Word3;
cin>>Word4;
cin>>Word5;
return 0;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
int main(void) {
srand(time(NULL));
std::string Words[3];
std::cout<<"enter you subjects.\n";
std::cout << "Word 1: ";
std::cin>>Words[0];
std::cout << "Word 2: ";
std::cin>>Words[1];
std::cout << "Word 3: ";
std::cin>>Words[2];
std::cout << Words[rand() % 3] << std::endl;
return 0;
}
|
That should work perfectly. Just randomly generate a number (0 - 3) for the index of the Words array.
Last edited on
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 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(0));
const int Max_Subjects=5;
string subject[5];
cout<<"enter your 5 subjects:\n\n";
cin>>subject[0];
cin>>subject[1];
cin>>subject[2];
cin>>subject[3];
cin>>subject[4];
cout<<"the subject would be:\n";
cout<<subject[rand()%5];
return 0;
}
|
thank you for helping.
Topic archived. No new replies allowed.