Can't Seem To Get This Loop To Execute Properly

Howdy guys,

I'm supposed to design a program for my computer science class that:

1) Outputs an array of random characters.
2) Calculates how many times each character appears in the array
(with lower or upper case not taken into account)
3) Outputs the number of times each character appears in the array

The loop I wrote to calculate the number of times each character appears
doesn't seem to work. All it does is output the size of the array of characters + 1.

Here is my code:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Ryan Vasichko - 1043181
//Assignment 6

#include <cstdlib>
#include <cmath>
#include <iostream>

using namespace std;

void generateArray(char charArray[], int size, int seed);
void displayArray(char charArray[], int size);
void calculateLetterCount(char charArray[], int charCount[], int size);
void displayLetterCount(int charCount[], int size);


int main() {
    int size, seed, charCount[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    char charArray[100];
    
    cout << "Seed number: " << "\n";
    cin >> seed;
    cout << "\n" << "Array size: " << "\n";
    cin >> size;
    cout << "\n";
    generateArray(charArray, size, seed);
    displayArray(charArray, size);
    cin.get();
    calculateLetterCount(charArray, charCount, size);
    displayLetterCount(charCount, size);
    cin.get();
}

void generateArray(char charArray[], int size, int seed) {
     int count = 0;
     char arrayHelper[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 
     'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 
     'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
     while(count++ < size) {
           charArray[count] = arrayHelper[rand() % 52]; }
     }
     
void displayArray(char charArray[], int size) {
     int count = 0;
     while(count++ < size) {
           cout << charArray[count];
           }}
           
void calculateLetterCount(char charArray[], int charCount[], int size) {
     int a = 0, b = 0, c = 1;
     char upperCase = 'A', lowerCase = 'a';
     do {
         do {
             if(charArray[a] == upperCase++ || lowerCase++)
             charCount[b++]++;}
          while(c++ < 26);
          upperCase = 'A', lowerCase = 'a', b = 0, c = 0;}
     while(a++ < size);
     a = 0; }       
             
void displayLetterCount(int charCount[], int size) {
     char character = 'A';
     int count = 0;
     cout << "\n";
     do {
         cout << character++ << ": " << charCount[count] << "\n"; }
         while(count++ < 25);
         character = 'A'; }


If I ran this with inputs of 50 for my seed and 10 for the amount of random
characters to generate, it would return


A: 11
B: 11
C: 11
D: 11

and so on, all the way to Z: 11

The 11 being the number of random characters + 1.

Wtf?! Does anyone have any ideas what is causing this?
Try using for instead of while

1
2
for(int x=0;x<5;x++)
     cout << "Hi" << endl;


What this does is run the loop 5 times. The for function is broken up into 3 parts, initializing, condition, and processing. The first part, x=0, is where your count variable is initialized. The next part, x<5 is the condition to run the loop so while x is less then 5, it will run. The last part is what happens after every loop. Here, x is increased by 1. You can put anything you want in these parts to make your code run the way you want it, but this is a general description to help you get started and change that one piece of code:

[code]
do
{
...
}while(count++ < 25)
[\code]

If I had to guess, your program is not adding 1 to count each round so you are displaying only the first part of the array ([0] part) which happens to be 11. If you use the F10 key to run your code line by line or set up a break point with F9, you can see what exactly is in the variable and see how they are reacting line by line so you can understand the mistake. This is a great way to troubleshoot so you are not waiting forever for responses from people like me to help you which sometimes could take a while if we are not online.
Topic archived. No new replies allowed.