Still open for suggestions....

First, why can I not get this code to highlight and format to code tags?
Second, can anyone show me how to make this code display correctly when it runs
??????

below is the code...
I am new to coding....


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//Write a program that inputs a letter and outputs the corresponding I.C.A.O. alphabet word. 
//Turn the program into a function, use it to convert to a string input by the user into the series
//of words that would be used to spell it out phonetically.
  
#include <iostream>
#include <string>
using namespace std;

void convert(string);

int main()
{
	string input;

    // Prompt user to input

	cout << "Enter string:";   
	cin >> input;
	cout<< "Phonetic version is:";

	convert(input);
	cout<<endl;
	system ("pause");
}
//create function
	void convert(string input)
	{
		int i=0;
//declare variables
		char letter;
		int length=input.length();
//while loop
		while (i<length)
		{
			letter=input.at(i);
//all instances of letters
			if(letter='a'|| letter=='A')
				cout <<"Alpha";
			else if (letter==' b '|| letter=='B  ')
				cout <<" Bravo  ";
			else if (letter==' c '|| letter==' C ')
				cout <<" Charlie  ";
			else if (letter==' d '|| letter=='D  ')
				cout <<"Delta   ";
			else if (letter==' e '|| letter==' E ')
				cout <<" Echo  ";
			else if (letter==' f '|| letter==' F ')
				cout <<" Foxtrot  ";
			else if (letter==' g '|| letter==' G ')
				cout <<" Golf  ";
			else if (letter==' h '|| letter=='H  ')
				cout <<" Hotel  ";
			else if (letter=='  i'|| letter==' I ')
				cout <<" India  ";
			else if (letter==' j '|| letter==' J ')
				cout <<"  Juliet ";
			else if (letter==' k '|| letter==' K ')
				cout <<"  Kilo ";
			else if (letter==' l '|| letter==' L ')
				cout <<" Lima  ";
			else if (letter==' m '|| letter==' M ')
				cout <<"  Mike ";
			else if (letter==' n '|| letter==' N ')
				cout <<" November  ";
			else if (letter==' o '|| letter==' O ')
				cout <<" Oscar  ";
			else if (letter=='p  '|| letter==' P ')
				cout <<" Papa  ";
			else if (letter==' q '|| letter==' Q ')
				cout <<" Quebec  ";
			else if (letter=='r  '|| letter==' R ')
				cout <<" Romeo  ";
			else if (letter==' s '|| letter==' S ')
				cout <<" Sierra  ";
			else if (letter==' t '|| letter==' T ')
				cout <<"Tango   ";
			else if (letter==' u '|| letter==' U ')
				cout <<"Uniform   ";
			else if (letter==' v '|| letter=='V  ')
				cout <<"Victor   ";
			else if (letter==' w '|| letter==' W ')
				cout <<" Whiskey  ";
			else if (letter==' x '|| letter==' X ')
				cout <<" X-ray  ";
			else if (letter==' y '|| letter==' Y ')
				cout <<" Yankee  ";
			else if (letter==' z '|| letter=='Z  ')
				cout <<"Zulu   ";
			i++;
		}
} 

Last edited on
[code]Put your code here between the code tags for it to show up properly.[/code]

As for your second question, letter is supposed to be a char. Stuff like ' c ' is not a char because it does not have a length of 1. I hesitate to call it a string because you're using single quotes, not double quotes. I have no idea what to call it, but in any case, it is not a char.
thank you for the answer to my first question. are you saying that i need to declare letter as string and put all alphabet letters inside " "
????
Last edited on
No, i'm saying that you should take out all those spaces in your comparisons. Instead of letter==' c ', it should be letter=='c'. That applies for all the comparisons you have.
ok. i fixed it and thank you.. another question?
Last edited on
@justhappy

Remove the newlines in your text, and just use a space. cout <<"\nAlpha";
should just be cout <<"Alpha ";
Fixed it! Thank you both!
@justhappy

Sorry, but that just doesn't seem possible. If you have a space at the end of each phonetic word in the list and no newlines, it would be impossible for the words to run together. I'm thinking you removed the newlines, but did not put a space at the beginning of the word, or instead, at the end. One or the other, not both, or you'll have two spaces between words. I ran your program with these corrections as I described, and got the correct results of space words
you are correct, I missed the space correction... thanks again
Last edited on
Topic archived. No new replies allowed.