Correct way to enter data into an array?

Hello I am trying to assign some values to an array using a struct as the base. The struct is global above the main function. Is the way below the correct way to assign values to the array? When I print my array to the screen, none of the values that are printed are the values that I enter (its gibberish).

Input entry[MAX_SIZE];

entry[tester].ch = ch;
entry[tester].counter++;


struct Input
{
char ch;
int counter;
};









?
Last edited on
More code, please, showing how you input and output the data.
Hello Melkiy. Below is my code. I am entering the characters from a file. I am trying to enter and count the characters.


#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;

struct Input
{
char ch;
int counter;
};

const int MAX_SIZE = 26;
void intializeCounters (Input entry[]);
int testChar(Input entry[], char ch, int& answer);
void notEntered (Input entry[], int& count, char ch);

int main()
{
char ch;
int answer;
ifstream inData;
Input entry[MAX_SIZE];

inData.open("test.txt");

intializeCounters(entry);

inData.get(ch);
int count = 0;
while (inData)
{
if (isalpha(ch))
{
if (isupper(ch))
{
ch = (tolower(ch));
}

testChar(entry, ch, answer);

if (answer != 1)
{
notEntered(entry, count, ch);
}

}
cout << entry[count].ch;
cout << entry[count].counter;
inData.get(ch);
answer = 0;
}

system("pause");
}








void intializeCounters (Input entry[])
{
for (int count = 0; count < MAX_SIZE; count++)
{
entry[count].counter = 0;
}
}



int testChar(Input entry[], char ch, int& answer)
{
for (int test = 0; test < MAX_SIZE; test++)
{
if (ch == entry[test].ch)
{
entry[test].ch = ch;
entry[test].counter++;
answer = 1;
}
}
}


void notEntered (Input entry[], int& count, char ch)
{
int tester;

tester = count;

entry[tester].ch = ch;
entry[tester].counter++;
count++;
}


Below is the output I get..


0 0 0n0n0 0 0 0ê0ê0ê0ê0ê0ê0ê0ê0ê0d1d1x0x0x0x0x0x0x0x0x0Press any key to continu
e . . .



And here are the characters that are entered from the test.txt file.

jjJu fjdkfj
jfjjka;rm 23kjf;
Last edited on
Don't compitely understand your intentions, but at the lines
1
2
	cout << entry[count].ch;
	cout << entry[count].counter;

you use count as index of the array, where there have not been any assignment yet, so there is garbage there. Maybe entry[count-1] would give better results?
My intentions are to:
1. Input a character from a file.
2. If the character is alpha, check to see if it is upper case.
3. If the character is upper case, convert it to a lower case character.
4. Check the array to see if the character has already been entered.
a. If the character has already been entered, increment the counter in the array.
5. If the character has not been entered, enter the character into the array and increment the counter.
6. Repeat the process until all the characters have been processed from the file.
7. Print the array and its contents.
Topic archived. No new replies allowed.