Rookie question

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";

return 0;
}
Last edited on
Post the code you have and then we can operate on it for you.
Hello Macfam13,

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.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
 using namespace std;
 

 int main ()
 {
     char letter;
 cout << "Please enter a letter: \n";
 cin >> letter;
 cout << "SSSSSSSSSSSSSSSS \n";
 for (int i = 0; i < 16; i++) {
  cout << letter;   
 }
cout << endl;
 return 0;
 }
Hello Macfam13,

Thank you. The use of code tags is better, but watch your indenting.

The for loop on line 11 just duplicates line 10, but the concept is still useful.

This should give an idea of what to do:
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
#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;
}


Hope that helps,

Andy
@Manga,

Sorry I confused you with OP, my bad.

Hello Macfam13,

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>
using namespace 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.

Hope that helps,

Andy
ok so I did not do the whole face. But you get the idea.

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
 #include <iostream>
 using namespace std;
 
void face(char c) {
    
    for(int i = 0; i < 46; i++) {
        if(i == 0) cout << " ";
     cout << c; 
     if(i == 17 || i == 34) cout << "\n ";
     if(i == 19 || i==25 || i==30 || i==36 || i==39 || i ==43) cout << " ";
    }
    cout << endl;
 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";   
}


 int main ()
 {
     char c;
 cout << "Please enter a letter: ";
 cin >> c;
 cout << endl << endl;
 face(c);
 
 return 0;
 }
We start with:
1
2
3
4
5
6
#include <iostream>

int main ()
{
  std::cout << " SSSSSSSSSSSSSSSSSS \n";
}

That program is hardcoded to print something immutable.

Andy did already have something much more flexible:
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>

int main ()
{
  std::string message = " SSSSSSSSSSSSSSSSSS ";
  std::cout << message << '\n';
}

The message is a variable that holds modifiable data.

Characters of a string object can be changed, added, removed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <algorithm>

int main ()
{
  std::string message = " SASSSSSSSSSSSSSSAS ";

  std::cout << message << '\n'; // original

  char old = 'A';
  char fix = '_';
  std::replace( message.begin(), message.end(), old, fix );

  std::cout << message << '\n'; // updated
}
Last edited on
Topic archived. No new replies allowed.