"space" characters in array sequences are excluded

I usually use vectors, but since arrays can be extremely helpful in the parsing department, i have decided to explore them. I'm self taught for the record.

So, to start, i created a small program to help me out. Here is what it does: Function 1- creates a temporary "test file" to extract data from and into an array and displays the data from the array ( the data goes from file, to array, to display)

Function 2- gets individual characters using the cin>> function and displays the resulting character in 3 different forms: un-modified (origional character), _toupper(), and ch in string form (which i know isn't changed, but so what?).

So far I have concluded that:

a) Arrays do not recognize a space "character" at all. In fact when the data is extracted from the file, all the characters are there, but the spaces are excluded and so create the appearance of 1 long line of characters and the occasional period. Itwouldlookalotlikethisline. <- (see what i did? ;)

b) conversion from a string to a character just doesnt work with arrays, i tried string <--> char comparison techniques but to no avail. It's a one way street: string can only = char.

c) arrays do not like to be initialized using a string, since a string can be any length and therefore may even excede the declared array length. They can accept contant strings, but that doesnt exactly help me with parsing...

If i was to make a shifted alphabet encoder/decoder it would be near unreadable because the words would be run together. Or if i wanted to alphabetize a list of words, they wouldn't be separate. Is there any way for an array to accept a blank character? Or do i have to separate datavalues using another character?

I also have 1 more question: I want to be able to have a user input (like getline(cin, stringvar) but instead it puts each character into an array). Is that possible?
Last edited on
I have learned somthing new: _toupper(ch), where ch is an integer, yields a different result than _toupper(ch), where ch is a character.
Ok arrays can recognize a space char. first of all what typre of array are you using?
Second converting from a string to an array of chars does work but you have to get each indavidual char 1 by 1. If your reading from a file like it says at the begining use fstream and do something like
char ch;
while(!inFile.eof()){
inFile.get(ch)
ch=array[i];
i++;
}
that will get each char one by one and put it in an array. plus get() gets unformatted data if i recall properly and will put spaces in as well.

as for part c i have no clue what your trying to do here. if it is a string array it should be able to be initialized with strings just fine. If your trying to initialize a char array or an int array with a string yea it would be a problem.

as for the final question dont get the whole string at once use a while loop and get each char 1 by 1 and aassign it to the array till you hit the \n char from the user hitting enter.

If that doesnt help you post some of the code and ill take a look at it.
I should mention that I'm using Code::Blocks, so "inFile.get(ch)" is not a real iostream function.

I should also point out that you made ch = array[i] istead of the other way around.
Last edited on
Here is the function that gets each character from a text.txt file, puts it into an array, and displays it to the user:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int d_array_from_file()
{
    char fil[200];
    char arr;
    string line, fil_t;
    int c = 0;
    create_test_file();
    ifstream f;
    f.open("test.txt", ios::in);
    if(!f.is_open())
    {
        cls();
        cout<< "There was an error in opening the test file."<< endl;
        waituser();
        cls();
        return 0;
    }
    while(!f.eof())
    {
        c++;
        f>> arr;
        if(_toupper(arr) == 0)
        {
            c++;
            continue;
        }
        fil[c] = arr;
    }
    c++;
    fil[c] = '\0';
    c--;
    f.close();
    cls();
    cout<< fil<< endl;
    waituser();
    remove("test.txt");
    cls();
    _cl();
    return 0;
}


cls(), waituser(), _cl(), and create_test_file() are functions I wrote.

cls() - cls command

waituser() - creates an interface that adds onto existing display that prompts the user to press enter to continue, and then waits.

_cl() - clears the _getch() stream

creat_test_file() - was just easier to make this as a separate function due to the fact i may need it again in another function.

Now that you have some of the code, you may understand where I'm coming from. The function displayed will only return every character except spaces for some reason. I dont know why, but it just does.
sorry it was quick sudo code and InFile.get(ch) works perfectly fine if you use fstream like it said and declare an fsrteam named inFile.

its when your doing f>>arr that you get problems with no spaces. that >> operator only gets formatted data so its missing the spaces. You have to do

f.get(arr);
fil[c]=arr;

I tested this in dev and msvc10 and it worked in both. Im unsure about code blocks since i only use it on my linux comp but if its still causing problems ill try it on there
Last edited on
Ill try it... hold on.
OMFG TYTYTYTYTYTYTYTY!

I have been trying to figure this out since i started writing the **m language and now... FINALLY! lol. Time for some coded messages... ;)
lol no prob just glad you got it working. I know how hard it can be when your trying to teach yourself a programming language.
no kidding. Instead of encryption, i can use this. I wrote a passwords/accounts program (to store a good large number of my accounts and passwords) that needs some added protection. This will be just awsome!
Topic archived. No new replies allowed.