I'm looking for a function call that will return a specific character from a string. I need to create a program that will ask the user for a word, ask for a number, then the program will display a character from the string that matches with the number.
So say a user enters the word flamingo, 4, then the program shows the letter i.
#include "pch.h"
#include "iostream"
#include <string>
usingnamespace std;
int main()
{
string city;
int charNum, charResult;
cout << "Input your favorite city: ";
cin >> city;
cout << "Which character you would like to display: ";
cin >> charNum;
charResult = ;
cout << "The user entered: " << city << endl;
cout << "The character at position " << charNum << " is: " << charResult << "\n\n";
system("pause");
return 0;
}
#include <iostream>
#include <string>
#include <iomanip>
int main()
{
std::string word ;
std::cout << "enter a word: " ;
std::cin >> word ;
std::size_t pos ;
std::cout << "enter the position of a character (a number, zero is the first position): " ;
std::cin >> pos ;
std::cout << "you entered " << std::quoted(word) << '\n' ;
if( pos < word.size() )
std::cout << "the character at position " << pos << " is '" << word[pos] << "'\n" ;
else std::cout << "invalid position " << pos << '\n' ;
}