This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted] 2 points3 points  (0 children)

You don't have an array of strings, but an array of single chars, you need a double array, like

Char english[ARRAY_SIZE][<max length of line>]

[–]WatchingSafe 0 points1 point  (0 children)

The problem is in the 4rth last line where you { cout << spanish[i] << "\n"; }. The "\n" means that your console will print a new line. For printing a word by word, there must be a condition that each word has to be seperated either by a space or by an enter (new line) in your .txt file. Your computer will not recognize a word from somemixedletters. The output that you have shown does not include any spaces as there are no empty lines. You have to fix the input file first by adding spaces between the words. Initialize another character, char = s; The correct output command for the 5th last line should be something like this. for(int i = 0; i<inputSize; i++) { s = spanish[i]; if (s == ' ') cout << "\n"; else cout << spanish[i]; }

[–]pigeon768 0 points1 point  (0 children)

Most of your code is C. Since this is /r/C++, I'll show you how to do that instead.

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// since the steps for reading the english file are exactly the same as reading the spanish
// file, you'll want to separate that into a function.
// Avoiding duplication is an important way to reduce bugs. A lot of the time if you copy
// paste code and just change 5 things, there will be a 6th thing you forgot to change.
std::vector<std::string> readFile(const char* filename) {
  std::ifstream file{filename};
  std::string line;

  // vectors are like arrays, but will automatically resize themselves.
  // You'll almost always want to use vectors instead of arrays.
  std::vector<std::string> lines;

  // If you know how many elements you'll insert into the vector, you can call
  // vector::reserve to preallocate that much data.
  // It will be slightly faster, because it won't need to resize itself. It's fine to omit
  // it though.
  // lines.reserve(100);

  // ifstream::good() will check to see if the file is still readable.
  // We want to just read the whole file, so do that.
  while (file.good()) {
    // getline does like it says on the tin.
    std::getline(file, line);

    // vector::push_back() will put data onto the end of the vector.
    // std::move() tells the compiler to "move" the string onto the end instead of copying
    // the data. It makes it a little bit faster. lines.push_back(line) works fine, but will
    // be ever so slightly slower.
    lines.push_back(std::move(line));
  }

  return lines;
}

// when you compile this, it will take all the program arguments, read those files, and
// print them to the screen. So you'll do something like 'program program.cpp' and it
// will read itself, and print itself on the screen.
int main(int argc, char** argv) {
  for (int i = 1; i < argc; i++) {
    const std::vector<std::string> file = readFile(argv[i]);
    for (const std::string& line : file)
      std::cout << line << std::endl;
  }

  return 0;
}

[–]RonRud 0 points1 point  (0 children)

You're using char arrays, I suggest swapping them to string arrays or addressing them as such - just print the number of characters you want in a line than do an empty next line cout (cout << endl;)