how do i get it so when the user enters an integer, it outputs the president cor
ID: 3660963 • Letter: H
Question
how do i get it so when the user enters an integer, it outputs the president corresponding to that integer? #include using namespace std; int main() { enum Presidents {George Washington=1 John Adams=2 Thomas Jefferson=3 James Madison=4 James Monroe=5 John Quincy Adams=6 Andrew Jackson=7 Martin Van Buren=8 William Henry Harrison=9 John Tyler=10 James K. Polk=11 Zachary Taylor=12 Millard Fillmore=13 Franklin Pierce=14 James Buchanan=15 Abraham Lincoln=16 Andrew Johsnon=17 Ulysses S. Grant=18 Rutherford B. Hayes=19 James A. Garfield=20 Chester A. Arthur=21 }; int num; cout << "Enter the number president: "; cin >> num; cout << "The president was " << num << endl; return 0; }Explanation / Answer
/* I think you are misunderstanding something about enum. What your program is doing looks like its trying to use the enum as if they were stored string array. Enum is more of allowing the INPUT to be the name of the president and you outputing the number the president was. also note that since cpp start indexing at 0, i added n as the 0th element in Presidents */ #include using namespace std; int main() { enum Presidents { n, George Washington, John Adams, Thomas Jefferson, James Madison, James Monroe, John Quincy Adams, Andrew Jackson, Martin Van Buren,William Henry Harrison, John Tyler, James K. Polk, Zachary Taylor, Millard Fillmore, Franklin Pierce, James Buchanan, Abraham Lincoln, Andrew Johsnon,Ulysses S. Grant, Rutherford B. Hayes, James A. Garfield, Chester A. Arthur}; Presidents num; cout > num; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.