I'm working on this program for my C++ class. It works fine but I'm not quite happy with it. Basicly this accepts a string, pulls each character out and converts it to the COA equivalent. It reads the entire string including spaces, but I can't seem to get it to ouput the blank spaces again. Is there a simple way to do this?

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

void evalChars(string);

void main(){

	string myString("");
	string currentChar("");
	int nextChar = 0;
	int totalChars = 0;

	cout << "Enter the string: ";
	getline(cin, myString);
	cout << myString << endl;
	totalChars = myString.length();

	while(nextChar < totalChars){

	currentChar = myString.substr(nextChar, 1);

	nextChar++;

	evalChars(currentChar);

	}
	system("pause");

}

//==========================================

void evalChars(string currentChar){
	
	if((currentChar == "A")||(currentChar == "a")) cout << "Alpha" << endl;
	else if((currentChar == "B")||(currentChar == "b")) cout << "Bravo" << endl;
	else if((currentChar == "C")||(currentChar == "c")) cout << "Charlie" << endl;
	else if((currentChar == "D")||(currentChar == "d")) cout << "Delta" << endl;
	else if((currentChar == "E")||(currentChar == "e")) cout << "Echo" << endl;
	else if((currentChar == "F")||(currentChar == "f")) cout << "Foxtrot" << endl;
	else if((currentChar == "G")||(currentChar == "g")) cout << "Golf" << endl;
	else if((currentChar == "H")||(currentChar == "h")) cout << "Hotel" << endl;
	else if((currentChar == "I")||(currentChar == "i")) cout << "India" << endl;
	else if((currentChar == "J")||(currentChar == "j")) cout << "Juliet" << endl;
	else if((currentChar == "K")||(currentChar == "k")) cout << "Kilo" << endl;
	else if((currentChar == "L")||(currentChar == "l")) cout << "Lima" << endl;
	else if((currentChar == "M")||(currentChar == "m")) cout << "Mike" << endl;
	else if((currentChar == "N")||(currentChar == "n")) cout << "November" << endl;
	else if((currentChar == "O")||(currentChar == "o")) cout << "Oscar" << endl;
	else if((currentChar == "P")||(currentChar == "p")) cout << "Papa" << endl;
	else if((currentChar == "Q")||(currentChar == "q")) cout << "Quebec" << endl;
	else if((currentChar == "R")||(currentChar == "r")) cout << "Romeo" << endl;
	else if((currentChar == "S")||(currentChar == "s")) cout << "Sierra" << endl;
	else if((currentChar == "T")||(currentChar == "t")) cout << "Tango" << endl;
	else if((currentChar == "U")||(currentChar == "u")) cout << "Uniform" << endl;
	else if((currentChar == "V")||(currentChar == "v")) cout << "Victor" << endl;
	else if((currentChar == "W")||(currentChar == "w")) cout << "Whiskey" << endl;
	else if((currentChar == "X")||(currentChar == "x")) cout << "X-ray" << endl;
	else if((currentChar == "Y")||(currentChar == "y")) cout << "Yankee" << endl;
	else if((currentChar == "Z")||(currentChar == "z")) cout << "Zulu" << endl;
	else if(currentChar == "" ) cout << " " << endl;
	else if(currentChar == ",") cout << "," << endl;
	else if(currentChar == ".") cout << "." << endl;
	else if(currentChar == "!") cout << "!" << endl;
	else if(currentChar == "?") cout << "?" << endl;


}

Recommended Answers

All 3 Replies

>>else if(currentChar == "" ) cout << " " << endl;

change that "" to " " (with a space between the quotes)

else if(currentChar == "" ) cout << " " << endl;
	else if(currentChar == ",") cout << "," << endl;
	else if(currentChar == ".") cout << "." << endl;
	else if(currentChar == "!") cout << "!" << endl;
	else if(currentChar == "?") cout << "?" << endl;

why not just shorten tat to this: else cout << currentChar << endl;

Thanks for the thought on combining those last statements, it works and is much easier. Saddly, adding the space between the "" didn't solve the problem.

Adding the space did in fact work after all.... recompiling after cleaning the solution makes all the difference lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.