Unrequired input? Is this possible?

This sounds a lot more elementary than it is, but due to the cin function requiring input before the rest of the program will continue, I can't get my program to do what it's supposed to.

The idea is that the program takes an inputted letter, then depending on the letter, turns it into a number and makes the number and subjects it to all kinds of edits (to encrypt the letter). I want the user to input a word as seperate characters (with spacing in between each character). I've already decided how I'm going to change it to a number (using #define), all I need is for the input not to be required. I have a char array with 100 elements, but if I use the cin function, they will have to enter 100 characters (they could input "t h e" and then 97 0s, but that would be ridiculous...) so I need a function which will allow them to enter as many letters (up to 100) as they want, and change them into numbers. Spaces will become zeros, A will become 19 (to make it seem more random), etc.

Is there such a function which will allow them to input characters through a DOS prompt so that I can store them in an array? I can't find it on the internet.

This is my code so far:
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
34
35
36
37
38
39
40
41
42
// give each letter seemingly random values
#define a 19
#define b 29
#define c 39
#define d 49
#define e 59
#define f 69
#define g 79
#define h 89
#define i 99
#define j 18
#define k 28
#define l 38
#define m 48
#define n 58
#define o 68
#define p 78
#define q 88
#define r 98
#define s 17
#define s 27
#define t 37
#define u 47
#define v 57
#define w 67
#define x 77
#define y 87
#define z 97
#define space 0 // 0 will become space, so that any values that are not inputted will be ignored


#include <iostream>

using namespace std;

void Decode [char alph[100]) {

}

void Encode(char alph[100]) {
// They will need to input values into the array...
}
Last edited on
I would use an std::string instead of chars, that way you can use getline();

Read here for getline():
http://www.cplusplus.com/forum/articles/6046/

Then you can simply iterate over the string to get the individual characters.
Thanks. I'll give that a go...

Looks good so far.
and using of #define is no good style of programming, and I don`t think so that #define is thing that you need
Last edited on
Hm, I was thinking it might be unnecessary... I think it might be better just to do this:

if (/*the letter is a*/) /*the found letter*/=99

or something. I'm reading a tutorial on the find() function at the moment, so I can hopefully find and convert all letters of the alphabet. I'm hoping I can place them back into the string properly... so I don't get input: "How do you do?", output: "odd of? wHo you "
I've gotten this far:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

void Decode(string numbers) {
}

void Encode(string words) {
     
string::size_type
loca=words.find("A",0),
locb=words.find("B",0),
locc=words.find("C",0),
locd=words.find("D",0),
loce=words.find("E",0),
locf=words.find("F",0),
locg=words.find("G",0),
loch=words.find("H",0),
loci=words.find("I",0),
locj=words.find("J",0),
lock=words.find("K",0),
locl=words.find("L",0),
locm=words.find("M",0),
locn=words.find("N",0),
loco=words.find("O",0),
locp=words.find("P",0),
locq=words.find("Q",0),
locr=words.find("R",0),
locs=words.find("S",0),
loct=words.find("T",0),
locu=words.find("U",0),
locv=words.find("V",0),
locw=words.find("W",0),
locx=words.find("X",0),
locy=words.find("Y",0),
locz=words.find("Z",0),
loc_=words.find(" ",0);

if (loca!=string::npos) loca=19;
if (locb!=string::npos) locb=28;
if (locc!=string::npos) locc=37;
if (locd!=string::npos) locd=46;
if (loce!=string::npos) loce=55;
if (locf!=string::npos) locf=64;
if (locg!=string::npos) locg=73;
if (loch!=string::npos) loch=82;
if (loci!=string::npos) loci=91;
if (locj!=string::npos) locj=19;
if (lock!=string::npos) lock=28;
if (locl!=string::npos) locl=37;
if (locm!=string::npos) locm=46;
if (locn!=string::npos) locn=55;
if (loco!=string::npos) loco=64;
if (locp!=string::npos) locp=73;
if (locq!=string::npos) locq=82;
if (locr!=string::npos) locr=91;
if (locs!=string::npos) locs=19;
if (loct!=string::npos) loct=28;
if (locu!=string::npos) locu=37;
if (locv!=string::npos) locv=46;
if (locw!=string::npos) locw=55;
if (locx!=string::npos) locx=64;
if (locy!=string::npos) locy=73;
if (locz!=string::npos) locz=82;
if (loc_!=string::npos) loc_=0;

cout << words;
}

int main() {
string ende;
cout << "Encode or Decode? [En/De]\n";

getline(cin, ende);
cin.clear();

if (ende=="En" || ende=="en") {
  cout << "Enter a word or words to be encoded" << endl;
  cout << "A file will be created afterwards.\n";
  string words="";
  getline(cin, words);
  Encode(words);
}

else if (ende=="De" || ende=="de") {
  cout << "This function is to decode a sequence of numbers, or to play around\n";
  cin.clear();
  cout << "Please enter a sequence of numbers: ";
  string numbers;
  getline(cin, numbers);
}

cin.ignore();
cin.get();

return 0;
}


It does not work. It just outputs the word that was inputted, and I am unsure where to go from here (on my Encode function). Any suggestions? Am I doing this completely wrong? (Probably. This isn't important, but it'd feel good if I could say "Yeah, I wrote a program that turns letters into numbers, what did you do?").

EDIT: (return was missing for whatever reason...)
Last edited on
you input variable "words" not change it and then output "words", try to change it in function Encode()
or try to output by character
Thats a good idea, I'll give it a go. Thanks.
Strings also have a replace() as well, so you don't have to do the replacing manaually. Just mystring.replace(mystring.find('a'), 25); or something similar.
Thanks!
Topic archived. No new replies allowed.