C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 16th, 2012, 07:43 PM
rtier20 rtier20 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 rtier20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 48 sec
Reputation Power: 0
General - Enum help

I need to write a program which outputs the president with which number he was. I feel like I'm close, but can't seem to figure it out. (I know I didn't complete the list of presidents)
#include <iostream>
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;
}

Reply With Quote
  #2  
Old January 30th, 2013, 12:39 PM
BritanyP BritanyP is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 BritanyP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 27 sec
Reputation Power: 0
Quote:
Originally Posted by rtier20
I need to write a program which outputs the president with which number he was. I feel like I'm close, but can't seem to figure it out. (I know I didn't complete the list of presidents)
#include <iostream>
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;
}


You never defined any president as "num" therefore, when typing a number, it's not finding the president.
In fact, num is only defined as the number you're typing

Reply With Quote
  #3  
Old March 4th, 2013, 01:42 PM
yakpimp yakpimp is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 22 yakpimp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 7 sec
Reputation Power: 0
You can't actually use an enum for this. Since an enum is simply a value that you can use in your code in place of some number. It's mainly to make your code more readable.

What you probably want to do is define an array which contains the names of the presidents. Then look up the president based on the number that was passed in.

So if your array was

"George"
"Bob"
"Bill"
"Thomas"

Each one of the names would be associated with array index being the order in which they were President. index 0 would be George, index 2 would be Bill.

Then you can just do a simple look up and print out the string at that array index.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Enum help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap