How to randomly pick a variable

Pages: 12
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...

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
#include <iostream>
#include <ctime>
#include <string>
#include <vector>

using namespace std;

int main()
{
	int numOfObjects;
	vector<string> myObjects(10);
	cout << "This program will randomly choose an object for you." << endl;
	cout << "----------------------------------------------------" << endl;
	cout << "How many objects do you have (1-10): ";
	cin >> numOfObjects;

        // Check for input errors
	if (numOfObjects > 10 || numOfObjects < 0)
		return 1;

	for (int i = 0; i < numOfObjects; i++)
	{
		cout << "Enter object " << i << ": ";
		cin >> myObjects[i];
	}
	srand( time(NULL) );
	int randObject = rand() % numOfObjects;

	cout << "Your randomly chosen object is: " << myObjects[randObject] << endl;

	return 0;
}
Last edited on
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?
Last edited on
using namespace /*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 using namespaces unless I absolutely have to. ;)

-Albatross
Last edited on
+1 to that - I've never been too keen on using namespace. It undoes all the good work of the library developers who put their library in a namespace in the first place :P
Ah okay. Thank you!
Sorry to double post, (and post in this thread again as it is solved) but I didn't want to make another thread and waste this one.

Okay, so now I am back to the problem where the user can't input a space in his input. When I run the program it outputs this:

1
2
3
How many things do you have that you want to do, but can't decide? (1-10): 2
Enter the things 0: Enter the things 1: hey
You should: hey 


When it should be this

1
2
3
4
How many things do you have that you want to do, but can't decide? (1-10): 2
Enter the things 0: hi
Enter the things 1: hey
You should: hey 


My code is:

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
#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.
That's two words: trailing newline left over from the last cin statement that you could get rid of using std::cin.ignore().

-Albatross
What trailing newline left over from the last cin statement? Could you show me the code that is related to what you just said?
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.

-Albatross
Ahh I get it now! So what I need to do is to flush/reset the cin buffer.

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
#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;.

Thanks.
Last edited on
Topic archived. No new replies allowed.
Pages: 12