I coded a small program that should do exactly what you need. The only difference is that you tell it how many objects you want to pick from (10 being max) and then it will ask you to individually list each object. Finally it will randomize one of the objects and print it to the screen! I hope this helps...
on another note, instead of hassling the user and not letting them enter spaces, why not make a simple function to get rid of all of the spaces? like so:
1 2 3 4 5 6 7 8 9 10
void eatspaces(char* str)
{
int i = 0;
int j = 0;
while((*(str + i) = *(str + j++)) != '\0')
if (*(str + i) != ' ')
i++;
return;
}
Ah ha! As I check my thread again, there's 2 people who magically answered both of the things that I was about to do to improve my program!
TheNoobie, thank you so much! This is exactly what I was going to do and you bet me to it. But my code that I was going to do is make a huge algorithm and have a lot of if statements (silly me, I can't believe that you made it so much easier! I didn't know there was such a command named 'for'!).
ascii, again, thank you! I was wondering if there would be an 'advanced' way to let people enter spaces instead of using getline.
This is just absolutely genius! Thank you so much guys! *goes and fiddles with the awesome code provided*
EDIT: I don't like to use 'using namespace std', i always like to type 'std::blahblah'... is that a problem? Which one is better? What one do professionals use?
usingnamespace /*any*/; is maybe O.K. if you're dealing with only one library at any one time, but it feels a lot more organized, it helps avoid some naming conflicts, and it improves code readability when you don't use it and especially when you have several libraries (with namespaces).
I personally never use any usingnamespaces unless I absolutely have to. ;)
+1 to that - I've never been too keen on usingnamespace. It undoes all the good work of the library developers who put their library in a namespace in the first place :P
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <limits>
int main()
{
int numOfOptions;
srand(time(NULL));
std::cout << "How many things do you have that you want to do, but can't decide? (1-10): ";
std::cin >> numOfOptions;
std::string options[numOfOptions];
for(int i = 0; i < numOfOptions; i++)
{
std::cout << "Enter the things " << i << ": ";
std::getline(std::cin, options[i]);
}
std::string r = options[rand() % numOfOptions];
std::cout << "You should: " << r << std::endl << std::endl;
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
The getline command used to work but not it doesn't because of (most likely) the for loop command.
Would there be another way to allow spaces? ascii's function gets rid of the spaces, and I want to keep the spaces in. If there isn't another way to allow spaces, how could I fix this error? Either way, I appreciate the help guys.
Amazing how this forum has so many encouraging, helpful people.
When you get anything using >>, it'll get exactly what it needs and stop there. That means that if you put in something like
How many things do you have that you want to do, but can't decide? (1-10): 10
Then when you >> it, you'll get the 10 but the '\n' in cin's buffer that got there after you hit [Enter] will stay there. When you call getline, it'll see that the buffer isn't empty and get the newline from it, which is of course is the character that it's set to discard and finish upon reaching. It doesn't think it has any reason to wait for your input.
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <limits>
int main()
{
int numOfOptions;
srand(time(NULL)); //Random 'seed' to make rand() actually randomly pick instead of choosing the same one
std::cout << "How many things do you have that you want to do, but can't decide? (1-10): ";
std::cin >> numOfOptions;
std::cin.ignore();
std::string options[numOfOptions]; //creates arrays with the name 'options'
for(int i = 0; i < numOfOptions; i++)
{
std::cout << "Enter the things " << i << ": ";
std::getline(std::cin, options[i]);
}
std::string r = options[rand() % numOfOptions];
std::cout << "You should: " << r << std::endl << std::endl;
std::cout << "Press ENTER to exit...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
I added std::cin.ignore right below after it says std::cin >> numOfOptions;.