repl.it
@justingraham/

Regional 2010 - Random Item Generator

C++

No description

fork
loading
Files
  • main.cpp
main.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <string>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<string> list;
	
	cout << "Random Item Generator";
	cout << "\nWritten by #598896";
	
	cout << "\n\n1) Add Item";
	cout << "\n2) Display All Items";
	cout << "\n3) Display Random Item";
	cout << "\n4) Quit";
	
	int selection;
	cout << "\n\nOption (1-4): ";
	cin  >> selection;
	
	// makes sure selection is a number and within the criteria
	while(cin.fail() || selection < 1 || selection > 4)
	{
		cin.clear();
		cin.ignore(99999, '\n');
		cout << "Incorrect choice, enter a number selection (1-4): ";
		cin  >> selection;
	}
	
	// to end the program if selection equals 4
	while(selection != 4)
	{
		
		// adds item
		if(selection == 1)
		{
			string additem;
			cout << "Enter the item name you want to add: ";
			cin  >> additem;
			list.push_back(additem);
			cout << "Item added!" << endl;
		}
		
		// displays list
		else if(selection == 2)
		{
			for(int i = 0; i < list.size(); i++)
			{
				cout << i + 1 << ") " << list[i] << endl;
			}
		}
		
		// shows random item
		else if(selection == 3)
		{
			if(list.size() != 0)
			{
				int randomIndex = rand() % list.size();
				cout << "Random item: " << list[randomIndex] << endl;
			} 
			else 
			{
				cout << "There are no items in the list." << endl;	
			}
		}
		
		cout << "\nOption (1-4): ";
		cin  >> selection;
		
		while(cin.fail() || selection < 1 || selection > 4)
		{
			cin.clear();
			cin.ignore(99999, '\n');
			cout << "Incorrect choice, enter a number selection (1-4): ";
			cin  >> selection;
		}
	}
	
	return 0;
}
Fetching token
?