0

I'm just writing a little script to create some fake discord names.

To do this, I took a couple of .csv files with adjectives and nouns, imported them into vectors, and concatenated them. Ex:

vector<string> noun; vector<string> adj; infile.open("english-adjectives.txt"); while(infile.good()){ getline(infile,x); adj.push_back(x); } infile.close(); shuffle(begin(adj), end(adj), rng); 

I did the same thing for nouns, and then tried to concatenate them with a number, but I got a really weird result.

for (unsigned int i = 0; i < adj.size(); i++){ string temp; temp.append(adj[i]); temp.append(noun[i]); discord.push_back(temp); } for (unsigned int i = 0; i < discord.size(); i++){ cout << discord[i] << "#0001" << "\n"; } 

output:

#0001icresearch #0001downstairs #0001edfiddle 

When I remove the "#0001" part, it prints just fine.

honoredfiddle wanderby deliciousofficial 

Any ideas on why this is happening? I checked the newline chars in all my .csv files, and it's formatted for Unix, so I have no idea why this is happening.

3
  • 4
    Unrelated danger: while(infile.good()){ getline(infile,x); adj.push_back(x); } Test for valid stream, read from stream, store in container regardless of whether or not read from stream succeeded. You need to read, then test, not test then read. while(getline(infile,x)) { adj.push_back(x); } Commented Feb 8, 2022 at 0:45
  • 4
    Your CSV file was made on Windows and has Windows line endings (\r\n). But the computer you are running this on uses UNIX style line endings (\n) so each line in the CSV has a \r at the end that is not being removed as your program reads it. A \r moves the cursor to the left edge of the current line, so the number overwrites the name. There are multiple ways of removing the \r from the CSV file - or you could even do it inside this program after reading the data. One possible way: stackoverflow.com/questions/2528995/remove-r-from-a-string-in-c Commented Feb 8, 2022 at 0:45
  • I also see no evidence noun has the identical number of elements as adj, which is a gaping hole in this methodology. if there are fewer adjectives than nouns, it will "work". If there are more, this is a recipe for undefined behavior. Commented Feb 8, 2022 at 0:53

1 Answer 1

0

Answer from Jerry Jeremiah's comment

Your CSV file was made on Windows and has Windows line endings. But yhe computer you are running this on uses UNIX style line endings so each line in the CSV has a \r at the end that is not being removed. There are multiple ways of removing the \r from the CSV file - or you could even do it inside this program after reading the data.

Took it into notepad++, edited the newlines to be just LF, and that fixed it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.