I am having a problem with trying to copy a "char array to an int array". I have a code to randomly picked an amount of character and (in progress) print out how many occurrences, even if it's not in there a letter has. For now I'm only doing upper case letter to make sure it works, then I have to add the lower case letter also. I am having problems at line 15. The compiler says "incompatible types in assignment of int to int[100]". Any help is appreciated.

#include <iostream>
#include <cstdlib>
using namespace std;
int getInfo(int& seed, int& numOfChar);
int randomArray(char randomA[], int numOfChar, int seed);
int addLetter(int counter[], char randomA[], int numOfChar);

int main(){
    
int seed, numOfChar, counter[100] = {0};
char randomA[100];
    
    getInfo(seed, numOfChar);
    randomArray(randomA, numOfChar, seed);
    counter = (int) randomA;
    addLetter(counter, randomA, numOfChar);  
    for (int i=0; i<numOfChar; i++)
    {
        if(counter[i]){
                       cout << randomA[i] << " = " << counter[i];
                       }
    }
                            

  system("Pause");
  return 0;
}
//Function to Recieve information from user
int getInfo(int& seed, int& numOfChar)
{
 
         cout << "Enter the seed number: ";
         cin >> seed;
         cout << "\nNumber of characters: ";
         cin >> numOfChar;
         
}
//Function to assign random letter to randomA[] using ASCII code
int randomArray(char randomA[], int numOfChar, int seed)
{
     srand(seed);
                 for(int i=0; i < numOfChar; i++)
                 {
                         randomA[i] = rand()% ('Z'-'A') + 'A';
                         cout << randomA[i];
                         return randomA[i];
                         }
}
//Function to add 
int addLetter(int counter[], char randomA, int numOfChar)
{
    int counter2[100];
    
    for (int j=0; j<100; j++)
    {
        counter2[j] = counter[j];
    }
        
    for (int i=0; i<numOfChar; i++){
         if (counter[i] >= '65' || counter[i] <= '90') ++counter2[i];
                                   }
}

You might néed to specify the part of the array your getting from Ex variable[0] would send it to the address zero in the array.

I don't understand what you mean. Can you elaborate more.

If I declared a array like char Muffins[6] it would have five variables :
Muffins[0] Muffins[1] Muffins[2] Mufins[3] Muffins[4] And Muffins[5] (it starts at zero and the last one is for indexing stuff) if it is an array of chars you can remove the array (when you use it not declare it) and it would treat it as a string. If it's anything else you Usualy need to say what address (ex:[5]) it is at...

Watch out for numOfChar value. You could create segmentation fault while running.

Anyway, you should not simply cast a char array to an int array (if you can do... can't remember C++ syntax). You need to go through each value in the char array and convert to int. Also, there is no such char '65' because it becomes a string (length of 2 which is not a char).

Is counter a single variable or an array?

Counter is an array. I wanted to have counter be the ascii number array for the randomA, so then I could compare number line 60 with the ascii number array.

Watch out for numOfChar value. You could create segmentation fault while running.

Anyway, you should not simply cast a char array to an int array (if you can do... can't remember C++ syntax). You need to go through each value in the char array and convert to int. Also, there is no such char '65' because it becomes a string (length of 2 which is not a char).

Actually for numOfChar, I meant for it to be a call-by-refrence value. (if that's what you meant).

Also I will looke into the converting each value in the char array. I guess I would have to use a for loop for that.

Nope, I mean numOfChar is an input value from a user, correct? What if a user enters a value greater than 100?

Yes, you should go through a loop for an array. I doubt you could simply cast an array of a type to another array of the other type...

You can't assign an array to an array like that. Use a loop.

Taywin are you talking about using a do while loop so that if the person enter more than 100 or a negative number then it just keeps asking for the input.

Also I was thinking instead of getting a random "a-z" character I get a random 65-91 character. Then when I output it, static_cast the array so that it prints out the letter.

Yes, you should do that. It is safer to enforce users to not enter an invalid value that could crash your program.

That is a way to do it if you are doing it programmatically. It doesn't matter you initial it as int or char because you would have a different way to deal with the array. :)

Alright Thanks Everyone. I will continue on the program later today.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.