Problem with loop in random characters generation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ai123
    New Member
    • Oct 2011
    • 3

    Problem with loop in random characters generation

    I cant get my summary_Count function to work i figured it out that it stops working on the for loop part here

    Code:
    void summary_Count(char random[], int n)
    {
           
         int t=0;
         int count[26]={0};
        
         for(int a=0; a<=25; a++)
         {
         for(char i=97; char i=122; i++)
         {
         for(int j=0; j<=n; j++)
            {       
             if (random[j]=i) count[a]++;
            }}}    
         
         
       cout<<"\nSummary for the characters:"<<endl<<endl;
       for(char ch='a';ch<='z';ch++)
       {
       cout<<ch<<" = "<<count[t] <<endl;
       t++;
       }
         
    }
    The overall goal is to generate random charcters then count them and print how many a,b,c...ect. I made that for loop to count my letters but when i run it it stops right when it reaches the for loops. My full code is here its kind of mess though.

    Code:
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include <stdio.h>
    #include <ctype.h>
    
    using namespace std;
    void parameters(int& seed,int& n);
    char getChar(int seed);
    void printCharcter(char random[],int n);
    void summary_Count(char random[], int n);
    int main()
    {
    bool repeat=true;  
    int seed; 
    int n;
    char random[n];
    char again;
    srand(seed);
    do{
    parameters(seed, n); 
    
    for(int i=0; i<n; i++)
    {
    random[i]=getChar(seed);
    }
    printCharcter(random,n);
    
    for(int j = 0; random[j] != '\0'; j++)
    {
    random[j] = tolower(random[j]);
    }
    
    summary_Count(random, n);
    
    
    cout<<"\nDo you wish to try again? (Y/N) ";
    cin >> again;
    cout<<endl;
    if (again == 'n' || again == 'N') repeat=false ;
    else repeat=true ;
    }
    while(repeat==true);
     
     
     system("PAUSE");
     return 0;
    }
    
    void parameters(int& seed,int& n)
    {
    
       cout<<"Enter a seed number:";
       cin>>seed;
       cout<<"\nHow many charcters do you want generated:";
       cin>>n;
       if(n>100)
       {
            cout<<"Invlaid input, enter again"<<endl;
            cin>>n;
       }
            
            cout<<endl;
        
    }
    char getChar(int seed)
    {
      
       char random = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"[rand() % 52];
       return random; 
    }
    void printCharcter(char random[],int n)
    {
         cout<<"Charcters generated are:"<<endl;
         for(int i=0; i<n; i++)
         { 
           if (i% 10== 0) cout<<endl;
           cout<<random[i];
          
         }
    }
    void summary_Count(char random[], int n)
    {
           
         int t=0;
         int count[26]={0};
        
         for(int a=0; a<=25; a++)
         {
         for(char i=97; char i=122; i++)
         {
         for(int j=0; j<=n; j++)
            {       
             if (random[j]=i) count[a]++;
            }}}    
         
         
       cout<<"\nSummary for the characters:"<<endl<<endl;
       for(char ch='a';ch<='z';ch++)
       {
       cout<<ch<<" = "<<count[t] <<endl;
       t++;
       }
         
    }
  • ai123
    New Member
    • Oct 2011
    • 3

    #2
    for the if statement i forgot the second = but that still didn't fix my problem

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      I am little confused.. does it compile at all???

      anyway your for loop seems endless fix your forloop in the first code block. On line 9 the condition is always true

      Comment

      • ai123
        New Member
        • Oct 2011
        • 3

        #4
        oh cant believe i didnt see that yea it compiles but it dosent work,

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          You really only need one loop. Just loop through the random array and increment the count index based on the ascii value.

          Comment

          Working...