Thread: array problem

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    array problem

    hello,
    I am reading in a file and storing it into an array,
    and i am trying to find which letter appears most frequently in the array.
    how can this be accomplished?

    this is what i have so far. oh ya, and the file has spaces , but when i read contents of array there are no spaces.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    
    
    string line;  // also tried   char line[200];
    
    
    int count = 0;
    char reply;
    ifstream infile;
    string myfile;
    ifstream inputFile;
    
    
    
    inputFile.open ("encrypted.txt");
    
    if(!inputFile)
     {
        cerr << "Can't open input file " << myfile << endl;
    	cout << "Press enter to continue...";
        exit(1);
    }
    
    while (inputFile.peek() != EOF) {
    
    inputFile >> line;
    cout << line;
    }
    
        cin >> reply;
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't read strings with spaces in them with >>.

    If you want you can read a character at a time. To count, you can also use expressions inside the [brackets] of an array index -- so if you can just figure out how to get 'a' to be 0, and 'b' to be 1....

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to read lines of text with spaces, then use istream::getline() http://www.cplusplus.com/reference/i...m/getline.html or string::getline() http://www.cplusplus.com/reference/string/getline.html

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    ok i solved the space issue with getline, but not sure what you are talking about with
    to count most frequent letter in array? how can i do this?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    still trying to read the file and find the most frequent letter.
    but somethings terribly wrong. I hope someone can help me see what i am doing so wrong.
    what i have so far:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    void charactercount ( char ch , int list[]);
    void copytext ( ifstream &intext, char &ch, int list[]);
    void writetotal (int list[]);
    
    
    
    int main()
    {
    
    int lettercount[26];
    char ch;
    
    char reply;
    ifstream infile;
    string myfile;
    ifstream inputFile;
    
    inputFile.open ("encrypted.txt");
    
    if(!inputFile)
     {
        cerr << "Can't open input file " << myfile << endl;
    	cout << "Press enter to continue...";
        exit(1);
    }
    
    
    inputFile.get(ch);
    
    while (inputFile)
    {
    	copytext(inputFile, ch, lettercount);
    	
    	inputFile.get(ch);
    }
    
    writetotal(lettercount);
    
    inputFile.close();
    
    cin >> reply;
        return 0;
    
    }
    
    void charactercount( char ch , int list[])
    {
    
    int index;
    
    ch = toupper(ch);
    
    index = static_cast<int>(ch) - static_cast<int>('A');
    
    if ( 0 <= index && index < 26)
    list[index]++;
    
    }
    
    void copytext ( ifstream &intext, char &ch, int list[])
    
    {
    
    	while ( ch != '\n')
    	{
    		charactercount(ch, list);
    		intext.get(ch);
    	}
    
    
    }
    
    void writetotal (int list[])
    {
    
    	int index;
    	for ( index = 0; index < 26; index++)
    	cout << static_cast<char>(index + static_cast<int>('A')) << " Count = " << list[index] << endl;
    }
    output:
    Code:
    A Count = -858992861
    B Count = -858993305
    C Count = -858993454
    D Count = -858992948
    E Count = -858992917
    F Count = -858992749
    G Count = -858993236
    H Count = -858993382
    I Count = -858993337
    J Count = -858993450
    K Count = -858993363
    L Count = -858993456
    M Count = -858992890
    N Count = -858993355
    O Count = -858993243
    P Count = -858993182
    Q Count = -858992487
    R Count = -858993265
    S Count = -858993303
    T Count = -858993052
    U Count = -858992940
    V Count = -858993425
    W Count = -858993438
    X Count = -858993169
    Y Count = -858993277
    Z Count = -858992891

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Most people count by starting at 0, rather than "p20487p-8asdfij" as you are.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What he meant is, you need to initialize your count array.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    thank you cyberfish
    I think i know what to do now.
    fixed...
    Code:
    void initialize(int list[])
    {
    
    int j;
    
    for(j = 0; j < 26; j++)
    list[j] = 0;
    
    }
    Last edited by mrsirpoopsalot; 02-02-2009 at 01:24 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of writing a function to initialise the array, actually initialise the array:
    Code:
    int lettercount[26] = {0};
    By the way, you should indent your code consistently. Also, in the future you might want to consider using a std::map<char, int> to map the letters to their counts.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    laserlight,
    not sure about
    std::map<char, int>

    would this have been an easier way of doing what i did?
    I will also use google to find out what i can about map

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by mrsirpoopsalot View Post
    laserlight,
    not sure about
    std::map<char, int>

    would this have been an easier way of doing what i did?
    I will also use google to find out what i can about map
    It will be C++-ier way to do the thing
    also it will solve the problem of out-of bounds access when your char is not one of the set 'A'-'Z'

    and another problem - if the codes of the chars above are not sorted
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void charactercount( char ch , int list[])
    {
    
    int index;
    
    ch = toupper(ch);
    
    index = static_cast<int>(ch) - static_cast<int>('A');
    
    if ( 0 <= index && index < 26)
    list[index]++;
    
    }
    There is no need for a cast in the red code - it's all done by implicit cast.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM