Random String Array Problems

Hello. I'm sorry if this question has been asked before, but I can't find any topic, that has the same problem. So I am trying to make a kinda simple ciphering program. It would take user imputed letters and by using rand function it would make them into codes. For example, if user inputs "a" he gets either 1 or 2 or 3 or 4 or 5. Now when I try to make it I get some errors.
Here is the code:
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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

int main()
{
   string input;
   string a = "a";
   string b = "b";
   string c = "c";
   string d = "d";
   string e = "e";
   string f = "f";
// List goes to z ...

string aLine[] = {"B33","IPK","ATX","O2I","I4I"};
string bLine[] = {"VJ4","FJI","GB3","4TL","SF6"};
// List again goes to z...


top:
  cin >> input;


  if(input==a)
srand(time(0));
string aLine = rand() %5;
// then it should print out 1 of 5 codes
goto top;


Error: 'aLine' has a previous declaration as 'std::string aLine [5]'

Obviously I'm doing something wrong. I would also like to make Random function itself as another function (not in main function body)

Please help me. Thanks in advance.
Last edited on
closed account (o3hC5Di1)
Hi there,

The error means you already have defined "aLine" as a string array containing 5 members.

Check your code if you have not made a mistake mixing up aLine and aline (which are kind of ambiguous names).

Hope that helps,
All the best,
NwN
You deffined aLine twice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// here !!!
string aLine[] = {"B33","IPK","ATX","O2I","I4I"};
string bLine[] = {"VJ4","FJI","GB3","4TL","SF6"};
// List again goes to z...


top:
  cin >> input;


  if(input==a)
srand(time(0));
// and here !!!
string aline = rand() %5;
That's not the problem, because I forgot to change aLine in the code and when I posted the thread I mixed up the names.
closed account (o3hC5Di1)
Maybe post your actual code + the full error (including line numbers). :)

Thanks,

NwN
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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

int main()
{

   string input;
   string a = "a";
   string b = "b";
   string c = "c";
   string d = "d";
   string e = "e";
   string f = "f";
   string g = "g";
   string h = "h";
   string i = "i";
   string j = "j";
   string k = "k";
   string l = "l";
   string m = "m";
   string n = "n";
   string o = "o";
   string p = "p";
   string q = "q";
   string r = "r";
   string s = "s";
   string t = "t";
   string u = "u";
   string v = "v";
   string w = "w";
   string x = "x";
   string y = "y";
   string z = "z";



  string aline[] = {"B33","IPK","ATX","O2I","I4I"};

   string one = "1";
   string two = "2";
   string three = "3";
   string four = "4";
   string five = "5";
   string six = "6";
   string seven = "7";
   string eight = "8";
   string nine = "9";
   string zero = "0";



   string space = " ";
   string dot = ".";
   string comma = ",";
   string quest = "?";
   string excla = "!";
   string leparen = "(";
   string riparen = ")";



  top:
  cin >> input;


  if(input==a)
srand(time(0));
string aline = rand() %5;

goto top;

}


Here is the full code. Vlad, what do you suggest me doing? Because I'm not yet familiar with rand function enough :/
Last edited on
I do not understand what does "if user inputs "a" he gets either 1 or 2 or 3 or 4 or 5." mean?
closed account (o3hC5Di1)
1
2
3
4
//line 42:
 string aline[] = {"B33","IPK","ATX","O2I","I4I"};
//line 73:
string aline = rand() %5;


You are declaring 'aline' twice.
Either use a different name or use aline = rand()%5; to reassign.

All the best,
NwN
Last edited on
Each letter's code has 5 different values. In this case "a" has "B33","IPK","ATX","O2I" and "I4I". I want that the program would print out random code out of those 5. For instance, user inputs "a", then the program prints out "O2I", when user tries again, he get's another random code out of those 5.
NwN, when I do aline = rand()%5; I get error: incompatible types in assignment of 'int' to 'std::string [5]'|
What is the problem? If you have only five values for 'a' then use expression for the random number as

rand() % 5;
Or is the problem that with each letter some array is associated and each such array can has different size?
I think I understand what you're talking about...

In line 42, you define an array of 5 strings called aline.
But in line 73, you attempt to redefine the variable 'aline' as a string which you then try to initialize to an integer value between 0 and 4.

What you want to do is set some other variable to a random value choosen from string aline[5] , correct?
Last edited on
closed account (o3hC5Di1)
Try align = (string)(rand()%5);

N
I think the first problem you mentioned is what I'm struggling with. Can you write a code on how to make that, because when I do, I get errors and I'm probably doing something wrong :)
Thanks
atropos, I think so.
Sorry for double posting.

Nwn, I got these errors, when I did what you said:
error: 'align' was not declared in this scope|
error: invalid conversion from 'int' to 'const char*'|
error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|

Read atropos message, I think he described my problem more accurately.
Last edited on
closed account (o3hC5Di1)
In that case:

1
2
3
 
int randomint = rand()%5;
string othervalue = aline[randomint];


Is that what you mean?

NwN
NwN, Huge thanks to you. I got it working and the program outputs numbers in random. The only problem that I need that instead of numbers 0 - 4, I'd get those 5 different codes I mentioned before. Thanks again.
closed account (o3hC5Di1)
Don't thank me, thank astropos for clarifying ;)

If you want the actual numbers, just use the "randomint" value instead of "othervalue".

NwN
Thank you. Thanks everybody for helping me out :)
Last edited on
Topic archived. No new replies allowed.