I was solving a problem on codeforces, but I keep running into unwanted garbage values as output. What can I do?
#include <iostream>
#include <string>
#include <conio.h>
#include <set>
#include <vector>
using namespace std;
int main(){
int n, k;
cin >> n >> k;
char password[n];
for(int i=0; i<n; i++){
password[i]= 'a' + i%k;
}
cout << password;
}
I can't figure out what to do. It's driving me nuts!
passwordis not 0 terminated so it will keep printing until it finds a 0 or crashes or whatever other undefined behavior happens. Do yourself a favor and learn to usestd::stringinstead.