Confused about vectors.

If I created a vector with string values, how do I call one of those strings for when the user inputs an integer. Ex) a vector contains zero, one ,two...etc. when the user inputs 3, the output should be three.

#include "std_lib_facilities.h"
int main ()
{

vector<string>nums;

nums.push_back("zero");
nums.push_back("one");
nums.push_back("two");
nums.push_back("three");
nums.push_back("four");
nums.push_back("five");
nums.push_back("six");
nums.push_back("seven");
nums.push_back("eight");
nums.push_back("nine");

int num;
string n;
cout << "Please enter a digit from 1 to 9:\n";

while (cin>>num)
for (int i=0; i < nums.size(); ++i) // see if the string is in numbers
if (nums[i]== n) num = i;
cout << "The spelled out value for " << num << " is " << nums[i];

return 0;


whould this work?
Last edited on
If the strings inside the vector correspond to the indices of the vector, why dont you just cout the vector at that index?

Example:

cout << "Enter a number:"
cin >> i;
cout << "The spelled out value for " << i << " is " << nums[i];.


No need for the for-loop or the if-statement. Just make sure the number inputted by the user is less than nums.size().
Last edited on
Topic archived. No new replies allowed.