Just wanted to say thanks to everyone who gave input on that horrid assignment I had a couple of weeks ago!

Anyway, I am back with new problems. To start, I just want help correctly writing out one particular void function which I am calling readLetters. Okay, so I wrote out a pseudo code of what I want readLetters to do and then also the code that I actually wrote. The ultimate goal of this function is to read in a series of characters the user inputs and put it into an array(later I will be choosing 4 particular letters and outputting the number of occurrences). Oh, and it can't be case sensitive. Also, I am not even sure yet what the parameters of this function need to be.

Psuedo code:
count=0;
Read a letter;
While the letter !='.'{
store the letter in the appropriate variable. For example, if count is
0, store the letter in letter0. If count is 1, store the letter in letter1.
And so on.

Add 1 to count;
Read the next letter;
}

Actual code I wrote(which I feel in my gut is horribly off):

void readLetters()
{
     cout<<"Enter a series of letters ('.' to quit): ";
     while (letter!='.')
     {     
     for(int d=0;d<ARRAY_SIZE,d++)
     {
      if (isalpha (ch))
      {
       if (islower (ch))
          ch=toupper (ch);
          
          freqCount[ch]=freqCount[ch]+1;
       }
       cin.get(ch);
      }
    }
}

As always, thanks for any help!

Recommended Answers

All 16 Replies

Can you use strings? Since you can access the string as an array with
the indexing operator, [] .

Yes I can use strings...didn't think of that but I'll try and see what I can do. Thanks!

Hours later and I'm coming up empty. This new code I wrote actually compiles(and I wish it didn't because at least I'd have some known errors to work on) but doesn't allow me to even enter anything. [WEIRD!] It just tells me to enter a series of letters and then sits there. For now, I am nixing the functions and putting everything in main until I learn how to do this...
Just so everyone is on the same page, this is what I am ultimately trying to do:
Write a program that reads in a sequence of characters entered by the user and terminated by a period('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurrences, listing each letter that occurred along with the number of times it occurred. All non-alphabetic characters must be ignored. Any characters entered after the period ('.') should be left in the input stream unprocessed. No limit may be placed on the length of the input. HINT: (1) Use an array with a struct type as its base type so that each array element can hold both a letter and an integer. OR (2) use an array to hold occurrence only and find an "alternative way" to store a letter. The table should not be case sensitive--for example, lower case 'a' and upper case 'A' should be counted as the same letter.

I'd rather do it using an array to hold occurrence only because we never went over structs and that was just adding another level of frustration.

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

const int ARRAY_SIZE=1000;

int main()
{
    char freqCount[ARRAY_SIZE];
    int numItems;
    char ch;
    int count;
    string letterInput;
    
    count=0;
    
    cout<<"Enter a sequence of characters(end with '.'): ";
    while(ch!='.')
    {
     for(int d=0;d<ARRAY_SIZE;d++)
     {
      if (isalpha (ch))
      {
       if (islower (ch))
          ch=toupper (ch);
 
          cin.get(ch);
          freqCount[count]=ch;
          count++;
       } 
      }
     }
     
     numItems=count;
     cout<<"The letter you entered is: ";
     for(count=numItems-1;count>=0;count--)
     {
      cout<<freqCount[count];
      }    
system("pause");
return 0;
}

Oh yeah, at the moment I'm just trying to test out if my letters actually went into the array. That's the point of the second loop. Ultimately that will be changed...

Okay, I completely changed everything again...at least I'm getting errors now but I don't know what to do. All I want to do is put these characters into an array. It seems like it should be easy, but I'm just not getting it.

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

const int ARRAY_SIZE=1000;
void Obtain(char letters, int occurrences);
void Print(const int freq);

int main()
{
    char letters[ARRAY_SIZE];
    char ch;
    int freq;
    int occurrences;
    
    Obtain(letters,occurrences);
    Print(freq);
    

//**************************************************************************
void Obtain(char letters,int occurrences)
{
     int m;
     while(letters!='.')
     {
     for(m=0;m<ARRAY_SIZE;m++)
     {
      cout<<"Enter a sequence of characters(end with '.'): ";
      if (isalpha(ch))
      {
      if (islower(ch))
         ch=toupper(ch);
      cin>>letters[m];
      cin.ignore(ARRAY_SIZE,'\n');
      }
     }
    }
}
//**************************************************************************
void Print(const int freq)
{
     cout<<letters[4];
}

These are the errors I'm getting:
D:\MyPrograms>g++ try2.cpp
try2.cpp: In function `int main()':
try2.cpp:17: error: invalid conversion from `char*' to `char'
try2.cpp:17: error: initializing argument 1 of `void Obtain(char, int)'
try2.cpp:23: error: a function-definition is not allowed here before '{' token
try2.cpp:42: error: a function-definition is not allowed here before '{' token
try2.cpp:44: error: expected `}' at end of input

Help, please! Thanks!

Ok, I see what you are trying to do. Here is an idea.

You know there are 256 different character that char can hold.

so create an array of 256

size_t allCount[256];

Now you need to read in character by character.

char ch = 0;
char end = '.';
while( cin.get(ch) && ch != end ) continue;

But when you read the character you can also increment its count at
the same count.

size_t allCount[256]
char ch = 0;
char end = '.';
while( cin.get(ch)  && ch != end ){
   allCount[ch] += 1;
}

//print out result

what you can also do inside the loop is check if ch is a valid character
to count, if so then increment it, otherwise skip it.

Tell me how that goes so far.

Thanks firstPerson!

So, this is the code I have so far. Now it seems to work fine, but at the end when I just ask for what is in allCount[4], it gives me the same long junk number...any ideas? Also, if the user types a '.' in between other letters, the loop just keeps going. I need it to stop when it encounters a period and leave anything afterwards.

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

int main ()
{
    size_t allCount[256];
    char ch = 0;
    char end = '.';
    cout<<"Enter a sequence of characters(end with '.'): ";
    while( cin.get(ch)  && ch != end )
    {
     if (isalpha(ch))
      {
      if (islower(ch))
         ch=toupper(ch);
      }
         cin.ignore(256,'\n'); 
         allCount[ch] += 1;      
    }

    cout<<allCount[4];
    
system("pause");
return 0;
}

You need to set every element in that array from junk to 0.

size_t allCount[256] = {0};

And take out that cin.ignore. You are ignoring the characters that are left in the buffer when you need to read them
and see if there is a '.'

Also try making a function to print the stats like so :

void printStat(size_t Array[], const size_t Size)
{
    //error check if you want
		
	cout<<"\n\nUpper character stats : \n\n";

	for(char i = 'A'; i <= 'Z' && i < Size; i++){
		cout<< i <<" count = " << Array[i] << endl;
	}
}

Thanks so much! I actually was able to take your suggestions and make functions and simplify the code in main. Thanks again...now I have a new question. I want to copy only the non-zero values from the first array into a second array.
Here is the code that works just fine:

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

void printStat(size_t allCount[], const size_t Size);
void getChar(size_t allCount[], size_t Size);

int main ()
{
    size_t allCount[256]={0};
    size_t Size;
    
    getChar(allCount,Size);
    printStat(allCount,Size);
    
system("pause");
return 0;
}

//**************************************************************************
void getChar(size_t allCount[], size_t Size)
{
    char ch = 0;
    char end = '.';
    
    cout<<"Enter a sequence of characters(end with '.'): ";
    while( cin.get(ch)  && ch != end )
    {
     if (isalpha(ch))
      {
      if (islower(ch))
         ch=toupper(ch);
      } 
         allCount[ch] += 1;      
    }
}

//****************************************************************************
void printStat(size_t allCount[], const size_t Size)
{

 
	cout<<"\n\nLetter:     Number of Occurrences\n\n";
 
	for(char i = 'A'; i <= 'Z' && i < Size; i++){
		cout<< i <<"     =     " << allCount[i] << endl;
	}
}

Here is my attempt to copy only the non-zero values into a 2nd array:

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

void printStat(size_t copyallCount[], const size_t Size);
void getChar(size_t allCount[], size_t Size);
void copygetChar(size_t allCount[], size_t Size);

int main ()
{
    size_t allCount[256]={0};
    size_t Size;
    size_t copyallCount[256]={0};
    
    
    getChar(allCount,Size);
    copygetChar(allCount,Size);
    printStat(copyallCount,Size);

    
system("pause");
return 0;
}

//**************************************************************************
void getChar(size_t allCount[], size_t Size)
{
    char ch = 0;
    char end = '.';
    
    cout<<"Enter a sequence of characters(end with '.'): ";
    while( cin.get(ch)  && ch != end )
    {
     if (isalpha(ch))
      {
      if (islower(ch))
         ch=toupper(ch);
      } 
         allCount[ch] += 1;      
    }
}

//****************************************************************************
void printStat(size_t copyallCount[], const size_t Size)
{

 
	cout<<"\n\nLetter:     Number of Occurrences\n\n";
 
	for(char i = 'A'; i <= 'Z' && i < Size; i++)
    {
		cout<< i <<"     =     " << copyallCount[i] << endl;
	}
}

//****************************************************************************
void copygetChar(size_t allCount[], size_t Size)
{
     int x;
     size_t copyallCount[256];
     
      
      for(x=0; x<4; x++)
      {
       if(allCount[x]!=0)
       {
        copyallCount[x]=allCount[x];
        }
      }
}

Problem is, when I go to print what's in the 2nd array, I get ALL zeroes.

Your copygetChar function is wrong. First you want to go through
the array from 'A' to 'Z' to check for its occurance. You are only going through 0 to 4.

Second initialize your variables. In this case, in your copygetChar function, initialize copyallCount[256] = {0};

Third why do you need that function. You can show the letters
that aren't recorded 0 times in you main function. It seems to me like
its a waste.

f

firstPerson, I noticed the 4 about 10 minutes after my last post. lol!! Anyway, I had already changed that and I also had initialized my 2nd array...still the same result. I thought it was a waste also, but I have a selection sort I want to apply and I'm afraid I might mess something up by tinkering with the original array.

firstPerson, I noticed the 4 about 10 minutes after my last post. lol!! Anyway, I had already changed that and I also had initialized my 2nd array...still the same result. I thought it was a waste also, but I have a selection sort I want to apply and I'm afraid I might mess something up by tinkering with the original array.

Do a test run.

Don't use that program. In fact comment out the whole program.

Then at the top, start a new program that tries to get the insertion
sort to work, with some random char arrays. After you get that to
work, then copy that function. Delete this program you just started,
and uncomment the old program. Then just add the sorting function
in there.

good idea...thus far I've been making a mess of this selection sort I found.

I've tried doing my selection sort two different ways and in both ways, the code compiles. However, when I run them, both programs crash at the point where they are supposed to print the results.
I'm questioning whether my sort function is to blame or if maybe I need to modify my print function...

Here's code 1:

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

const int ARRAY_SIZE=256;
void printStat(size_t allCount[], const size_t Size);
void getChar(size_t allCount[], size_t Size);
void selectionSort(size_t allCount[], size_t Size);

int main ()
{
    size_t allCount[ARRAY_SIZE]={0};
    size_t Size;
    int x,n;
    
    getChar(allCount,Size);
    selectionSort(allCount,Size);
    printStat(allCount,Size);
    
system("pause");
return 0;
}

//**************************************************************************
void getChar(size_t allCount[], size_t Size)
{
    char ch = 0;
    char end = '.';
    
    cout<<"Enter a sequence of characters(end with '.'): ";
    while( cin.get(ch)  && ch != end )
    {
     if (isalpha(ch))
      {
      if (islower(ch))
         ch=toupper(ch);
      } 
         allCount[ch] += 1;      
    }
}

//****************************************************************************
void printStat(size_t allCount[], const size_t Size)
{

 
	cout<<"\n\nLetter:     Number of Occurrences\n\n";
 
	for(char i = 'A'; i <= 'Z' && i < Size; i++){
		cout<< i <<"     =     " << allCount[i] << endl;
	}
}

//****************************************************************************
void selectionSort(size_t allCount[],size_t Size) 
{
    for (int pass=0; pass<Size-1; pass++) {
        int potentialSmallest = pass;  // assume this is smallest

        //--- Look over remaining elements to find smallest.
        for (int i=pass+1; i<Size; i++) {
            if (allCount[i] < allCount[potentialSmallest]) {
                //--- Remember index for latter swap.
                potentialSmallest = i;
            }
        }
        
        //--- Swap smallest remaining element
        int temp = allCount[pass];
        allCount[pass] = allCount[potentialSmallest];
        allCount[potentialSmallest] = temp;
    }
}

And here's code 2:

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

const int ARRAY_SIZE=256;
void printStat(size_t allCount[], const size_t Size);
void getChar(size_t allCount[], const size_t Size);
void selectionSort(size_t allCount[], size_t Size);
int indexOfSmallest(const size_t allCount[],int startingIndex,size_t Size);

int main ()
{
    size_t allCount[ARRAY_SIZE]={0};
    size_t Size;
    int x,n;
    
    getChar(allCount,Size);
    selectionSort(allCount,Size);
    printStat(allCount,Size);
    
system("pause");
return 0;
}

//**************************************************************************
void getChar(size_t allCount[], const size_t Size)
{
    char ch = 0;
    char end = '.';
    
    cout<<"Enter a sequence of characters(end with '.'): ";
    while( cin.get(ch)  && ch != end )
    {
     if (isalpha(ch))
      {
      if (islower(ch))
         ch=toupper(ch);
      } 
         allCount[ch] += 1;      
    }
}

//****************************************************************************
void printStat(size_t allCount[], const size_t Size)
{

 
	cout<<"\n\nLetter:     Number of Occurrences\n\n";
 
	for(char i = 'A'; i <= 'Z' && i < Size; i++){
		cout<< i <<"     =     " << allCount[i] << endl;
	}
}

//****************************************************************************
void selectionSort(size_t allCount[],size_t Size) 
{
    for (int count=0; count<Size-1; count++) 
    {
       swap(allCount[indexOfSmallest(allCount,count,Size)],allCount[count]);
    }
}

//****************************************************************************
int indexOfSmallest(const size_t allCount[],int startingIndex,size_t Size)
{
    int targetIndex=startingIndex;
    
    for(int count=startingIndex+1;count<Size;count++)
    {
     if (allCount[count]<allCount[targetIndex])
     {
      targetIndex=count;
      }
     }
     
    return targetIndex;
}

Another try: this one doesn't crash but it gives me all zeroes...

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

const int ARRAY_SIZE=256;
void printStat(size_t allCount[], const size_t Size);
void getChar(size_t allCount[], const size_t Size);
void sort(size_t allCount[], size_t Size);

int main ()
{
    size_t allCount[ARRAY_SIZE]={0};
    size_t Size;
    
    getChar(allCount,Size);
    sort(allCount,Size);
    printStat(allCount,Size);
    
system("pause");
return 0;
}

//**************************************************************************
void getChar(size_t allCount[], const size_t Size)
{
    char ch = 0;
    char end = '.';
    
    cout<<"Enter a sequence of characters(end with '.'): ";
    while( cin.get(ch)  && ch != end )
    {
     if (isalpha(ch))
      {
      if (islower(ch))
         ch=toupper(ch);
      } 
         allCount[ch] += 1;      
    }
}

//****************************************************************************
void printStat(size_t allCount[], const size_t Size)
{

 
	cout<<"\n\nLetter:     Number of Occurrences\n\n";
 
	for(char i = 'A'; i <= 'Z' && i < Size; i++){
		cout<< i <<"     =     " << allCount[i] << endl;
	}
}

//***************************************************************************
void sort(size_t allCount[], size_t Size)
{
for (int nStartIndex = 0; nStartIndex <ARRAY_SIZE; nStartIndex++)
{
int nSmallestIndex = nStartIndex;

for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex <ARRAY_SIZE; nCurrentIndex++)
{
if (allCount[nCurrentIndex] < allCount[nSmallestIndex])

nSmallestIndex = nCurrentIndex;
}

swap(allCount[nStartIndex], allCount[nSmallestIndex]);

}
}

...or do you think I should mark this thread as solved and start a new one for my current problem since what I am talking about has absolutely nothing to do with the title of this thread

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.