How would I write a code that takes input from a user, then uses the letter they type to replace another letter already in the program. For example, I have a picture I made real quick with the letter S (supposed to be a superhero image made with a letter, but this site doesn't import correctly), but I want that to change the S to whatever the user selects with their input. I know I need to request a letter, and store it as a char, but I don't know how to change something already in the program. Thank you all in advance.
#include <iostream>
using namespace std;
int main ()
{
cout << " SSSSSSSSSSSSSSSSSS \n";
cout << " SS SSSSSS SSSSS SSS \n";
cout << " SS SSS SSSS SS \n";
cout << " SS SSSS SSS SS \n";
cout << " SSSSSSSSSSSSSSSS SS \n";
cout << " SSSSSSSSSSSSSSSSSSSSSS \n";
cout << " SS SSSSSSS \n";
cout << " SSSSSS SSSS \n";
cout << " SSSSSSSSSS \n";
cout << " SS SS \n";
cout << " SS";
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
In your posted code char letter; should be inside "main" not a global variable.
The last "cout" prints a constant string of letters. There is no way to change this. This would need to be defined as a string variable that could be changed before you use the variable in a "cout" statement.
As Manga said post the code you have, so anyone reading or responding can help you with what you need.
I think I understand what you want. I will see what I can come up with.
#include <iostream>
#include <string>
#include <limits>
#include <cctype>
//using namespace std; // <--- Best not to use.
int main()
{
char letter{};
std::string msg{ "SSSSSSSSSSSSSSSS" };
std::cout << "Please enter a letter: ";
std::cin >> letter;
letter = std::toupper(letter);
// <--- Use the member functions of the "string" class to change the "S"s in msg to what was entered in "letter".
// <--- http://www.cplusplus.com/reference/string/basic_string/
// <--- Or use a for loop to access each element of the string "msg" and change only the letters thet you need.
std::cout << msg << std::endl;
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
Please do not change code in your posts unless to change a minor mistake. This confuses people who come in to the thread after the change.
Original code as best as I remember it:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace std;
char letter;
int main()
{
cout << "Please enter a letter: \n";
cin >> letter;
cout << "SSSSSSSSSSSSSSSS \n";
return 0;
}
Based on your revised code I know of no way to change the "S"s in the "cout' statements. My first thought is to create a copy of the original source code file and read this file to make the changes that you need using the string member function "substr(...)" to extract the part with the "S"s into a new variable. using a for loop you can change the "S"s to a new letter. As the program reads each line it will need to write each line to a new file thus preserving the copy for later use.
The program is doable, but will depend on your knowledge of using file streams.