Thread: what is The Peroblem In This Code ?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Unhappy what is The Peroblem In This Code ?

    Code:
    # include <iostream>
    using namespace std;
    # include <cctype>
    # include <cstring>
    
    void readLine (char*);
    void printLine (char*);
    void display_Count_of_Words_Lenght (char*);
    void dispaly_Number_of_Words_In_Sentence (char*);
    void display_Uppercase_Frequency (char*);
    void dispaly_SubString_Frequency (char*);
    void replace_string_with_stars (char*);
    
    void main ( )
    {
    void (* AnalyseText[7])(char *)={readLine, printLine, display_Count_of_Words_Lenght,
     dispaly_Number_of_Words_In_Sentence , display_Uppercase_Frequency,
           replace_string_with_stars };
    
       int choice;
    
    cout<<"Enter a choice :";
    cin>>choice;
     
    while (choice >=0 && choice <7){
     (* AnalyseText[choice])(choice);}
    
    
    
    }
    
    
    
    void readLine (char* string1){
     cout<<"Enter a line of text:\n";
        cin.getline( string1, 100 );
    }
    
    
    void printLine (char* string1){
    
    cout<<"The line of text is:"<<string1<<endl;
    }
    
    
    void display_Count_of_Words_Lenght (char* string1){
    int lenght;
    int count[ ]={0};
    
    for (int i=0; i<10; i++)
     cout[i]=0;
    
    char *token= strtok (string1, " ");
    while (token != '\0'){
     lenght= strlen (token);
    
     if(lenght<=10)
    
      count[lenght-1]++;
    
    
     token= strtok ('\0', " ");
    }
    
    for(int j=0; j<10; j++)
    cout<<"lenght"<<j<<":"<<count[j]<<"occurences";
    
    }
    
    
    
    
    void dispaly_Number_of_Words_In_Sentence (char* string1){
    int count=0;
    
    char* token= strtok (string1, " ");
    
    while (token != '\0'){
    
     
     count++;
    
     token= strtok ('\0', " ");
    
    }
    
    for (int i=0; *(string1 +i) !='\0'; i++)
     cout<<"sentence"<<i<<":"<<count<<"words";
    
    }
    
    
    void display_Uppercase_Frequency (char* string1){
    
    int count=0;
    
    for(int i=0; *(string1+i) != '\0'; i++){
     if(  isupper (*(string1+i)) )
      count++;}
    cout<<"isupper: "<<count;
    }
    
    
    
    
    void dispaly_SubString_Frequency (char* string1){
    
    int count=0;
    char* string2;
    
    cout<<"Enter a string :\n";
    cin.getline (string2, 100);
    
    char * ptr= strstr (string1, string2);
    
    while (ptr != '\0'){
     count++;
    
     ptr= strstr (ptr+1, string2);}
    
    cout<<"The string"<<ptr<<"is repeated"<<count<<"times";
    }
    
    
    void replace_string_with_stars (char* string1){
    
    char *string2;
    cout<<"Enter a string :\n";
    cin.getline (string2, 100);
    
    char *ptr = strstr (string1, string2);
    
    while ( ptr != '\0' ){
    
     strcpy (string1, "*****");
    
     ptr= strstr (ptr+1, string2);
    }
    
    }

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Nobody wants to look at lines and lines of code for some random problem they have no clues about. Is it a compilation error, and if so what is the error? Is it at run-time…does it crash, or is the output wrong? Ect.
    So you tell us what the “Peroblem” is and I am sure plenty of people here will gladly look and try and figure out the cause of your problem.


  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You have a syntax error, cout[i] should be count[i].

    You have big problems with your functions as you prototyped them with a char and your trying to pass an integer.
    Sent from my iPad®

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You have big problems with your functions as you prototyped them with a char and your trying to pass an integer.
    chars and ints are often interchangeable. But that doesn't mean it's always good practice.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by sean_mackrory
    chars and ints are often interchangeable. But that doesn't mean it's always good practice.
    C++ is strongly typed, which means you shouldn't pass an integer in unless your casting it first. Also, its not char in the first place, its pointer to char, which means its trying to use the data passed in as the memory location.

    Also, the first thing I noticed in the posters code, was the "void main" its int, not void.

    My first MAJOR suggestion, is look at this
    Quote Originally Posted by almodmer
    Code:
    while (choice >=0 && choice <7){
     (* AnalyseText[choice])(choice);}
    }
    Last edited by Xipher; 11-11-2005 at 09:29 PM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM