0

I've been trying for about 2 days and i think I've come close but still no cigar! I have a text file that looks like this:

++++++
+H   +
+    +
+    +
+   X+
++++++

if i try to print it out normally, everything works:

void Level::LevelPrinter()  //This prints out fine
{
    inputMap.open("Level1.txt");

    string input;

    while (getline(inputMap, input))
    {
    cout << input << endl;
    }

}

But when i try to place all these symbols into a 2D array I either get empty spaces in some forms of code that i tried; or, on my latest try that seems to be closest to what i need, i get weird symbols placed in correct positions. I can't figure out what's wrong...

This is the code i'm trying out now:

void Level::LevelPrinterArray()
{
    inputMap.open("Level1.txt");

    char MapSymbols;
    int Rows, Cols;

    {
        for (Rows = 0; Rows < 6; Rows++)
        {
            for (Cols = 0; Cols < 6; Cols++)
            {

                inputMap >> MapSymbols;

                MapLayoutArray[Rows][Cols] = MapSymbols;

                cout << MapSymbols;
            }
            cout << endl;
        }
    }


}

And this is what the console shows me:

╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠

btw, MapLayoutArray is just:

char MapLayoutArray[6][6];

RadLexus, i tried not using >> so it looks like this instead:

void Level::LevelPrinterArray()
{
    inputMap.open("Level1.txt");

    char MapSymbols;
    int Rows, Cols;

    {
        for (Rows = 0; Rows < 6; Rows++)
        {
            for (Cols = 0; Cols < 6; Cols++)
            {
                inputMap.get(MapSymbols);

                MapLayoutArray[Rows][Cols] = MapSymbols;

                cout << MapSymbols;
            }
            cout << endl;
        }
    }


}

That also prints out:

╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠

-----UPDATE------

using .close on the first code (the one i said works properly). Made the characters on the 2nd code appear as normal characters! the problem is - they now looks like this:

++++++

+H
+
+
 +
+
  +
+
  X+
+

Played around a bit with the code and now this code:

void Level::LevelPrinterArray()
{
    inputMap.open("Level1.txt");

    char MapSymbols;

    {
        for (int Rows = 0; Rows < 6; Rows++)
        {
            for (int Cols = 0; Cols < 6; Cols++)
            {

                inputMap.get(MapSymbols);

                    if (MapSymbols != '\n')
                {
                    MapLayoutArray[Rows][Cols] = MapSymbols;

                    cout << MapSymbols;
                }

                    else
                    {
                        cout << endl;
                    }   
            }
        }
    }

    inputMap.close();
    cout << endl;
}

causes this:

++++++
+H   +
+    +
+    +
+   X+
+
Press any key to continue . . .

So i'm extremely close but i can't get it to print the last line. I tried many things to get it to print the last line like making the first for loop "Rows <= 6" but that prints out the last line properly and crashes the console with an error... I'll play around with this some more... If any of you has an idea, let me know. I'll update here if i figure it out...

Mr.Trainee
  • 15
  • 5
  • It looks like you are storing pointers, not the actual character read. Remind me again, should `>>` work on a `char`? – Jongware Jan 29 '17 at 02:00
  • 1
    You've left out key information - in particular, what is `MapLayoutArray`? In any event, try providing a small but complete sample of code (in the sense that someone else can replicate your problem with it). Leaving out details, as you have, increases the odds substantially of leaving out information that is actually relevant to your problem. – Peter Jan 29 '17 at 02:07
  • sorry, map layout array is just - char MapLayoutArray[6][6]; i'll add that in there... Other than that, that's the entirety of the code relevant to this... Rad Lexus i also added a comment about what u said in the original question, not using >> didn't fix it... – Mr.Trainee Jan 29 '17 at 02:45
  • Might be a problem with the file's encoding. If it is ANSI encoded the code using `inputMap.get` should work. – A.S.H Jan 29 '17 at 03:12
  • I just made sure to save it with the ANSI encoding but the problem persists... UPDATE - weird letters fixed, having a different problem now. Updated at bottom of original question... – Mr.Trainee Jan 29 '17 at 03:46
  • @Mr.Trainee try iterating each line of the file as a string and save it to the array. Look at my answer here below. http://stackoverflow.com/a/41919623/6451874 – Ofer Arial Jan 29 '17 at 10:39

2 Answers2

0

First attempt

inputMap >> MapSymbols;

The operator >> ignores every white characters, including spaces.

Second and third attempts

inputMap.get(MapSymbols);

Using the right function to take into account spaces, but you forget that when you obtain a newline character '\n', you must not wait the next iteration to read the (good) next character. You might also encounter '\r' depending on the OS.

A solution

You can use std::ws in order to skip these whitespaces at the end of each line1:

for (std::size_t Rows = 0; Rows < 6; Rows++)
{
    for (std::size_t Cols = 0; Cols < 6; Cols++)
    {
        char MapSymbols;
        inputMap.get(MapSymbols);
        MapLayoutArray[Rows][Cols] = MapSymbols;
        std::cout << MapSymbols;

    }
    inputMap >> std::ws; // HERE
    std::cout << std::endl;
}

1 Assuming lines don't start with spaces, otherwise use ignore() member function.

O'Neil
  • 3,790
  • 4
  • 16
  • 30
0

I tried to solve your problem on my computer.

The change I made is that I iterate the current line and insert it to the array char by char.

this code works very well:

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

int main(){

    std::ifstream inputMap("C:\\Res\\Level1.txt");

    char arr[6][6];
    for (int i = 0; i < 6; i++)
    {
        std::string input;
        std::getline(inputMap, input);
        for (int k = 0; k < input.length(); k++)
        {
            arr[i][k] = input[k];
            std::cout << input[k];
        }
        std::cout << std::endl;
    }

    std::cout << std::endl;

    for (size_t l = 0; l < 6; l++)
    {
        for (size_t m = 0; m < 6; m++)
        {
            std::cout << arr[l][m];
        }
        std::cout << std::endl;
    }

    system("PAUSE");
    return 0;

}

The output is as desired (I copied it here).

For your purpose, just don't print it in the first or second time ;)

++++++
+H   +
+    +
+    +
+   X+
++++++

++++++
+H   +
+    +
+    +
+   X+
++++++
Ofer Arial
  • 1,129
  • 1
  • 10
  • 25
  • Thanks friend, worked like a charm! Do you think it might be impossible to do it the way i originally planned? using chars instead of strings? Not that i have a problem with using strings, but now i'm curious to find a way with chars. oh, and btw, i tried reading up on what the difference was between size_t and normal unsigned int but couldn't figure it out. Could you enlighten me by any chance? – Mr.Trainee Jan 29 '17 at 22:22
  • 1
    @Mr.Trainee I have edited my answer below to show you a solution. About `std::size_t`, have ou read it's [documentation](http://en.cppreference.com/w/cpp/types/size_t)? – O'Neil Jan 30 '17 at 01:14