Hi,
I was given this problem:
"The user will enter two letters. These letters will be stored in a string using cin.getline. Replace the first letter entered with the second letter entered." The next problem we have will be to replace the letters in a sentence, but I'm stuck on this first part. Heres what I have done so far:

#include <iostream>
#include <cstring>
using namespace std;
int main(void)
{
		
		const int charString = 2;
		char sentenceHolder[charString];
		int index;
		char exitOnInput;
	
	cout << "Enter two character's" << endl;
	cin.getline(sentenceHolder, charString);
	
	
	for(index=0; index < charString; index++)
	{
		cout << sentenceHolder[index];
		}
		cout << "Enter a letter followed by 'enter' to exit" << endl;
		cin >> exitOnInput;
		return 0;
		
		}

LOL I have no Idea how to do this... Any help would be greatly appreciated.

Well I've been fooling around and have come up with this:

#include <iostream>
#include <cstring>

using namespace std;
int main(void)
{
		
		const int charString = 2;
		char sentenceHolder1[charString];
		char sentenceHolder2[charString];
		int index1;
		char exitOnInput;
	
	cout << "Enter one character" << endl;
	cin.getline(sentenceHolder1, charString);
	
	cout << "Enter another character" << endl;
	cin.getline(sentenceHolder2, charString);
	
	
	for(index1=0; index1 < charString; index1++)
	{
		sentenceHolder1[index1] = sentenceHolder2[index1];
	
	cout << sentenceHolder1[index1];
}
		cout << "Enter a letter followed by 'enter' to exit" << endl;
		cin >> exitOnInput;
		return 0;
		
		}

It replaces values entered in the first string with values in the second string, but I dont think that this is what I'm supposed to do. Is there a way to get 2 chars, store them in the same string and replace the second letter in the string with the first letter??

This article has been dead for over six months. Start a new discussion instead.