c++ - Retrieving a variable from an array -


ok problem having have array contains 27 characters , need write function display specific character 0 - 25 based on user input.

the array constant string:

const string alphabet = "abcdefghijklmnopqrstuvwxyz"; 

and idea user enters value between 1 , 25 (int value) displays cosponsoring letter in array. doing using:

cout << alphabet[value]; 

my questions appropreate way create array , able retrieve value array way.

const string alphabet = "abcdefghijklmnopqrstuvwxyz";  int main() {     int value; //variable defining user entered value     cout << "please enter number between 0-25." << endl;     cin >> value;     if (value > 25) //if statement display "-1" if number entered past "z"         cout << "-1" << endl;     else         cout << alphabet[value];      return 0; } 

my questions appropreate way create array , able retrieve value array way.

although works use string:

int main() {     const string alphabet = "abcdefghijklmnopqrstuvwxyz";      int num = -1;     while (num < 0 || num > 25) {         cout << "enter number (0 - 25): ";         cin >> num;     }      cout << alphabet[num] << '\n'; } 

i use std::vector containing sequence of elements:

vector<char> alphabet(26); iota(begin(alphabet), end(alphabet), 'a'); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -