How to change the output.

Can I change the output based on user input? (I don't have any code)

Sure can. The output of this program will change depending on user input.

1
2
3
4
5
6
7
8
9
10
11
12
13
# include <iostream>
# include <string>
using namespace std;
int main()

{
  string word;

  cout << "Enter word: ";
  cin >> word;
  cout << "\n  You entered: " << word;

}
No what I mean is, say a user was to input Hola I would have a if else statement that would be something, I guess, like this:
1
2
3
4
5
6
7
8
# include <iostream>
# include <string>
int main(){
if (User enters Hola){
cout << "Hello";
}
}

But thanks anyways! I'd really appreciate anyone answering this! Edit: What I'm aiming for here is kind of a translator. You know?
Last edited on
Going off Repeater's example, you could just do:

1
2
3
4
if (word == "Hola")
{
    cout << "Hello";
}


To match your code a bit more literally,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example program
#include <iostream>
#include <string>

bool user_enters(const std::string& str)
{
    std::string input;
    return (std::cin >> input && input == str);
}

int main()
{
    using std::cout;
    
    if (user_enters("Hola")) {
        cout << "Hello";
    }
}


A translator is really complicated because there is only a 1:1 mapping in the simplest of cases.
Okay yeah i see but i'm not going for like spanish. so for example i made a code e.g. A = p, b= i and so on. so if i would do your example how could i make the computer understand the difference from like a and b for example:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main() {
int letter;
if(letter== a ){
cout << "p";
}
}

And the user were to enter "ad" how could i have the computer distinguish a from d?

Thanks! I really appreciate any help you guys give! Sorry I'm pretty new to coding, and I'm just curious about some of this stuff.
Last edited on
its hard to answer this without you having a goal, but your goal seems to be 'random learning' which is fine (I do the same with some new material) but hard to guide you.

let me take a little crack:
char c; //this is a one byte integer suitable for storing english letters. int will work but its not the normal way.
string s; //this is a block of characters, can be a word or a sentence or even a full book!
c = 'a'; // 'a' is a letter. "a" is a string, though. note the different quote types.
s = "blah";
s[0] = 'z'; //s now contains zlah. [0] is the first characters, [1] is the second... so on.

you can use lookup tables for single letter swapping in english (ascii, single byte letters) easily. You also have tools with strings that can replace a letter with a string or string to string or whatever you need. This is where you need a goal before we can tell you how to do something, you need a bit more detail on what you want to do. Or you can review c++ string functions to see what you CAN do built in and beyond that you can craft your own tools to do other stuff if needed.
Last edited on
Okay here we go. (Thank you so much btw jonnin) So I created a "language" that I switched all the letters in. Here it is:
A = I
B = L
C = S
D = W
E = O
F = V
G = Y
H = D
I = U
J = J
K = G
L = K
M = M
N = ALX
O = E
P = R
Q = Q
R = F
S = B
T = H
U = A
V = C
W = Z
X = Mo
Y = P
Z = N

And what I want is a program that would ask the user to type in a phrase or word and it would translate it. For example: The user types in "Hello" and the computer returns: "Dokke"

I hope this is now clear for everybody!
@OP Oh, we have an encryption program, perhaps even a dictionary!! Good way to start.

Given the strings (eg "ALX") as part of the encoding the immediate question to be asked is are you familiar with <map>'s in C++ because setting one up and using it is ideal here. <map>'s are often called dictionary-style storage.

There are plenty of other ways from C-style arrays which the array police will leap at your throat about, or <vector>'s

https://www.cplusplus.com/reference/map/map/find/
Okay, thanks I'll try that!
Topic archived. No new replies allowed.