A bit of help with basic I/o
Apr 23, 2013 at 12:15am UTC
I'm trying to figure out how to take in each character (from a string) and use it in a switch. It's like a decoding program so it'll take in the string. Ex. abc and output "Aragorn Bilbo Celeborn". I think I'm supposed to use get.line but I'm not sure.
The code I have here now only does one character at a time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char letter;
cout << "Enter a letter" << endl;
cin >> letter;
switch (letter)
{
case 'a' : cout << "Aragorn " ; break ;
case 'b' : cout << "Bilbo " ; break ;
case 'c' : cout << "Celeborn " ; break ;
case 'd' : cout << "Durin " ; break ;
case 'e' : cout << "Eagle " ; break ;
case 'f' : cout << "Frodo " ; break ;
case 'g' : cout << "Gwaihir " ; break ;
case 'h' : cout << "Hobbit " ; break ;
case 'i' : cout << "Isildur " ; break ;
case 'j' : cout << "Justice " ; break ;
case 'k' : cout << "Kondor " ; break ;
case 'l' : cout << "Legolas " ; break ;
case 'm' : cout << "Mithrandir " ; break ;
case 'n' : cout << "Noldor " ; break ;
case 'o' : cout << "Orc " ; break ;
case 'p' : cout << "Pippin " ; break ;
case 'q' : cout << "Quickbeam " ; break ;
case 'r' : cout << "Rohan " ; break ;
case 's' : cout << "Saruman " ; break ;
case 't' : cout << "Thorin " ; break ;
case 'u' : cout << "Ungoliant " ; break ;
case 'v' : cout << "Valar " ; break ;
case 'w' : cout << "Warg " ; break ;
case 'x' : cout << "X-Garoth " ; break ;
case 'y' : cout << "Yrch " ; break ;
case 'z' : cout << "Zirak " ; break ;
default : cout << "That is not a letter." << endl; break ;
}
return 0;
}
Apr 23, 2013 at 12:23am UTC
you're exactly correct.
try this code
1 2 3 4 5 6 7 8 9 10
string letters;
cout << "enter a word: " << endl;
getline(cin, letters);
for (int i = 0; i<letters.size(); i++)
switch (letters[i]){
case 'a' : cout << "Aragorn " ; break ;
case 'b' : cout << "Bilbo " ; break ;
//ect
}
Apr 23, 2013 at 7:39am UTC
Awesome! This works perfectly. Thanks!
Topic archived. No new replies allowed.