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:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <ctime>
usingnamespace 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)
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.
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?
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
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.
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.