hey again,
code below works but it tends to give some info which shouldn't be there:
(for example a - 0 where 'a' is a letter and 0 is the times the letter was repeated)

#include <iostream>
#include <stack>
#include <fstream>
using namespace std;

int main()
{
    fstream fin1("E:\\iras.txt", ios::in | ios::binary);
    if (!fin1) { cout<< "error"; return 0; }
//========================================================
    string text;
   while (!fin1.eof())
   fin1 >> text;
   const char *textCopy=text.c_str();
//========================================================
    stack <char> symbol;
    int index=0;
//========================================================
cout<< "letter/digit count: \n";
    for (char j='0'; j< 'z'; j++)
    {
        symbol.push(j);
        for (int i=0; i<text.size(); i++)
        {
            if (textCopy[i] == symbol.top())
            {   index++;   }
        }
        if ((index > 0) && (symbol.top() >= '0' && symbol.top() <= '9') ||
(symbol.top() >= 'A' && symbol.top() <= 'Z')  || (symbol.top() >= 'a' && symbol.top() <= 'z'))
//these lines check for the symbols i need
        {
            cout<< j << " - " << index <<endl;
            index=0;
            symbol.pop();
        }
        else index=0;
    }
fin1.close();
    return 0;
}

i generated chars using the following code

#include <iostream>
#include <fstream>
#include <stack>
using namespace std;

int main()
{
    fstream fout1("E:\\iras.txt", ios::out | ios::binary);
    if (!fout1)  { cout<< "error"; return 0; }

    char symbol;
    stack <char> storage;
    srand(time(NULL));
    for (int i=1; i<300; i++)
    {
        symbol=48 + rand() % 74;
if ((symbol >= 'A' && symbol <= 'Z') || (symbol >= 'a' && symbol <= 'z') ||
 (symbol >= '0' && symbol <= '9'))
        storage.push(symbol);
       fout1 << storage.top();
    }
    fout1.close();

    return 0;
}

and also, any quick tips on what should have better been left out / done differently in this task?

Recommended Answers

All 8 Replies

For your utility to create the text files, I think you're excluding 'z', you might have to expand your rand() function a little. You don't need to bother with the stack in your file creation utility either, since you pop immediately after pushing, you can save 2 steps and just output your random symbol directly. For your main program ... didn't look at it too closely yet, but I'm not sure you should be using a stack for this, unless you are required to?

ah, so it would seem. fixed that. but what im mainly wondering about is why

if ((index > 0) && (symbol.top() >= '0' && symbol.top() <= '9') ||

(symbol.top() >= 'A' && symbol.top() <= 'Z') || (symbol.top() >= 'a' && symbol.top() <= 'z'))

doesn't work properly

quick edit: using stack wasnt required but that seemed like a good sollution

Still haven't looked through the code but you might need to add some parens around all of your symbol tests (all the tests that are ||.

Ok, starting to look through it... first thing i see is that you keep inputing strings into the variable "text" until the end of the file, so text will only have the last string in the file. Then you convert this string to array of char, loop through the array once for each letter you count.
An alternative would be to read the file one character at a time and keep an array with the running count of the symbols you encounter as you encounter them. Hint: when you are done going through the file, the number of A's in the file would be found at counterArray[65] . This way you save copying strings and you only have to loop through the text one time.

now that you tell, it does seem like a nice idea, however i want to make clear with the version above :)
you suggested adding some extra parens in the line responsible for the checking, however if some parens would be missing wouldn't it be seen in a massive way? now out of 300 chars it returns about 1-2 errorous so i think the problem must be elsewhere

What happens if you just remove the (index>0) from your test?

Also, in your if block, all you really need to do in there is output your line and in either case you set index=0; , so you don't need the else... you can just set it outside the if block. Along with this, if you are still using a stack, you can pop the stack just after the if block. You could also just be using j in the place of symbol.top() .

commented: been helpful and provided a good set of tips +1

gave a double check and it did lack 1 set of parens..thaks for your help tymk

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.