https://pastebin.com/Jquwu593
Could you guys give a look to my code because it prints me the same word averytime and i don't understand why it does that? Thanks in advice.
You do not explain at all what your code is supposed to do. It's so repetitive and unmanageable.
I think this might help you:
An integer can easily be converted into a character by a simple formula, by using the ASCII value of the integer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Example program
#include <iostream>
#include <string>
char int_to_char(int n)
{
// 'a' is equal to 97
returnstatic_cast<char>(n + 'a');
}
int main()
{
std::cout << int_to_char(0) << std::endl; // prints 'a'
std::cout << int_to_char(25) << std::endl; // prints 'z'
}
You can reduce your a() function to like 3 lines once you do this. Then you can start to make sense of what your code is actually doing.
1 2
event=rand()% 26+1; // you probably meant to do rand()%25 + 1?
word[ji+1] = int_to_char(event);
Also note you never change the value of "ji".
Are you allowed to use std::strings? They are much easier and safer to work with than your char arrays. Variable-length arrays are not standard in C++, so you shouldn't get used to using them.
1 2 3 4 5 6 7 8 9 10
#include <string>
#include <iostream>
int main()
{
std::string s;
int length;
std::cin >> length;
s.resize(n);
}
Those kind of errors better be detected quickly as they can cause serious problems later. Sometimes it can get a little harder but don't loose focus. If you find it a problem for you, you can always use some code security program. I have heard of called checkmarx. Not sure of their cost but sounds nice.
Anyway. good luck!
Ben.