Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,231 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,312 people online right now. Registration is fast and FREE... Join Now!




Strings, letter frequencies, letter count, no global variables

 
Reply to this topicStart new topic

Strings, letter frequencies, letter count, no global variables, Strings without using global variables

christinamoon
3 Nov, 2007 - 06:41 PM
Post #1

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 20


My Contributions
wub.gif Can someone tell me what it is missing in this program? I feel like something is missing! The program has to have a a function to read the string from the user, another function to count the number of times each letter occurs in the string, and another function to compute the letter frequencies. Also a function to display the letters, the letter counts, and the letter frequencies all this without using global variables. Here is what I have so far:
CODE
#include <stdafx.h>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    int index;
    string test_string;
    do
    {
        cout << "Enter a test string: ";
        getline(cin, test_string);
    } while(test_string.length() == 0);
    double letter_frequency[26];
    int letter_count [26];
    for(int index = 0; index < 26; index++)
    {
        letter_frequency[index] = 0;
        letter_count[index] = 0;
    }
    
    int number_of_letters = 0;
    int test_string_length = static_cast<int>(test_string.length());
    for(int index = 0; index < test_string_length; index++)
    {
        char symbol = test_string[index];
        symbol = toupper(symbol);
        if('A' <= symbol && symbol <= 'Z')
        {
            int letter_index = symbol - static_cast<int>('A');
            letter_count[letter_index]++;
            number_of_letters++;
        }
    }
    if(number_of_letters > 0)
        for(int index = 0; index < 26; index++)
            letter_frequency[index] = static_cast<double>(letter_count[index]) / number_of_letters;
    
    cout << setprecision(2) << fixed;
    for(index = 0; index < 26; index++)
        cout << setw(6) << static_cast<char>(index + static_cast<int>('A')) << setw(6) << letter_count[index] << setw(6) << letter_frequency[index] << endl;
    return 0;
}



User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Strings, Letter Frequencies, Letter Count, No Global Variables
4 Nov, 2007 - 10:01 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
looking pretty good so far... just need to break it up into functions.
User is offlineProfile CardPM
+Quote Post

christinamoon
RE: Strings, Letter Frequencies, Letter Count, No Global Variables
4 Nov, 2007 - 12:43 PM
Post #3

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 20


My Contributions
QUOTE(NickDMax @ 4 Nov, 2007 - 11:01 AM) *

looking pretty good so far... just need to break it up into functions.


Thank you NickDMax, can you please give me an example of how to "break it up into functions". I am not that good with functions!

Christina. rolleyes.gif

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Strings, Letter Frequencies, Letter Count, No Global Variables
4 Nov, 2007 - 01:02 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,357



Thanked: 51 times
Dream Kudos: 25
My Contributions
Here is one example:
CODE

string getString(void)
{
    string test_string;
    do
    {
        cout << "Enter a test string: ";
        getline(cin, test_string);
    } while(test_string.length() == 0);
    return test_string;
}

and in your main function
CODE

string str1;
str1 = getString();


User is online!Profile CardPM
+Quote Post

talksr
RE: Strings, Letter Frequencies, Letter Count, No Global Variables
4 Nov, 2007 - 02:13 PM
Post #5

New D.I.C Head
*

Joined: 11 Oct, 2007
Posts: 7


My Contributions


Hi, I am only a beginner in this, but my compiler was saying it could not find the first include statement you had.
Which was:
#include <stdafx.h>

So I removed it, and then the program ran fine however after entering the string, the program quit and did not appear to display the string back to screen.
So I added a
system ("pause");
just before return 0;
towards the end of the code.

However the output that coming out at the moment, I cannot explain.
When entering in the string 'test', I am getting:

QUOTE
A 0 0.00
B 0 0.00
C 0 0.00
D 0 0.00
E 1 0.25
F 0 0.00
G 0 0.00
H 0 0.00
I 0 0.00
J 0 0.00
K 0 0.00
L 0 0.00
M 0 0.00
N 0 0.00
O 0 0.00
P 0 0.00
Q 0 0.00
R 0 0.00
S 1 0.25
T 2 0.50
U 0 0.00
V 0 0.00
W 0 0.00
X 0 0.00
Y 0 0.00
Z 0 0.00


Note that all the letters used to make 'test' have a number and all of the ones that don't have 0 0.00

This post has been edited by talksr: 4 Nov, 2007 - 02:19 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Strings, Letter Frequencies, Letter Count, No Global Variables
4 Nov, 2007 - 02:45 PM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
@talksr:
stdafx.h is a precompiled header for Visual Studio. It only works in VS, but fortunately none of the contents are required in addition to the other #include's.

system("PAUSE") is a system-dependent way of getting the console window to stick around after the program has completed. For other, more portable (better?) ways of doing this, see this thread.

What were you expecting the output to be? The intent is to tally the characters entered, and display the counts and relative frequencies of each of those characters - and that seems to be what you're getting from this.


@christinamoon:
are you sure that you want to be combining upper and lower case like that? The implementation of that part is just fine, but you may want to make sure that you're actually supposed to do that, rather than tally upper and lower case separately.

Otherwise, seems to be working. Doesn't accept an empty entry (if the user just hits enter, it doesn't tally everything to zero), but that may be intentional behavior.

User is offlineProfile CardPM
+Quote Post

christinamoon
RE: Strings, Letter Frequencies, Letter Count, No Global Variables
4 Nov, 2007 - 02:53 PM
Post #7

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 20


My Contributions
QUOTE(talksr @ 4 Nov, 2007 - 03:13 PM) *

Hi, I am only a beginner in this, but my compiler was saying it could not find the first include statement you had.
Which was:
#include <stdafx.h>

So I removed it, and then the program ran fine however after entering the string, the program quit and did not appear to display the string back to screen.
So I added a
system ("pause");
just before return 0;
towards the end of the code.

However the output that coming out at the moment, I cannot explain.
When entering in the string 'test', I am getting:

QUOTE
A 0 0.00
B 0 0.00
C 0 0.00
D 0 0.00
E 1 0.25
F 0 0.00
G 0 0.00
H 0 0.00
I 0 0.00
J 0 0.00
K 0 0.00
L 0 0.00
M 0 0.00
N 0 0.00
O 0 0.00
P 0 0.00
Q 0 0.00
R 0 0.00
S 1 0.25
T 2 0.50
U 0 0.00
V 0 0.00
W 0 0.00
X 0 0.00
Y 0 0.00
Z 0 0.00


Note that all the letters used to make 'test' have a number and all of the ones that don't have 0 0.00




Hi talksr and thank you for your reply, the program does the following:
The program reads the string from the user, counts the number of times each letter occurs in the string, and computes the letter frequencies. Also displays the letters, the letter counts, and the letter frequencies all this without using global variables.

What I am working on right now is to use functions to make the program do the same things! jjhaag is right stdafx.h is a precompiled header for Visual Studio, I am using Visual Studio 2008 and it can be written: #include <stdafx.h> or #include "stdafx.h"

This post has been edited by christinamoon: 4 Nov, 2007 - 03:08 PM
User is offlineProfile CardPM
+Quote Post

christinamoon
RE: Strings, Letter Frequencies, Letter Count, No Global Variables
4 Nov, 2007 - 03:03 PM
Post #8

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 20


My Contributions
QUOTE(jjhaag @ 4 Nov, 2007 - 03:45 PM) *

@talksr:
stdafx.h is a precompiled header for Visual Studio. It only works in VS, but fortunately none of the contents are required in addition to the other #include's.

system("PAUSE") is a system-dependent way of getting the console window to stick around after the program has completed. For other, more portable (better?) ways of doing this, see this thread.

What were you expecting the output to be? The intent is to tally the characters entered, and display the counts and relative frequencies of each of those characters - and that seems to be what you're getting from this.


@christinamoon:
are you sure that you want to be combining upper and lower case like that? The implementation of that part is just fine, but you may want to make sure that you're actually supposed to do that, rather than tally upper and lower case separately.

Otherwise, seems to be working. Doesn't accept an empty entry (if the user just hits enter, it doesn't tally everything to zero), but that may be intentional behavior.




Hello jjhaag! The way it is written works fine for the moment, because I have to submit it tonight, my concern right now is to use functions to make it work the same way! Functions are hard to write. Hopefully I will finish it tonight. Thank you for your reply!

Christina.


User is offlineProfile CardPM
+Quote Post

talksr
RE: Strings, Letter Frequencies, Letter Count, No Global Variables
7 Nov, 2007 - 06:11 AM
Post #9

New D.I.C Head
*

Joined: 11 Oct, 2007
Posts: 7


My Contributions
QUOTE(christinamoon @ 4 Nov, 2007 - 03:53 PM) *

QUOTE(talksr @ 4 Nov, 2007 - 03:13 PM) *

Hi, I am only a beginner in this, but my compiler was saying it could not find the first include statement you had.
Which was:
#include <stdafx.h>

So I removed it, and then the program ran fine however after entering the string, the program quit and did not appear to display the string back to screen.
So I added a
system ("pause");
just before return 0;
towards the end of the code.

However the output that coming out at the moment, I cannot explain.
When entering in the string 'test', I am getting:

QUOTE
A 0 0.00
B 0 0.00
C 0 0.00
D 0 0.00
E 1 0.25
F 0 0.00
G 0 0.00
H 0 0.00
I 0 0.00
J 0 0.00
K 0 0.00
L 0 0.00
M 0 0.00
N 0 0.00
O 0 0.00
P 0 0.00
Q 0 0.00
R 0 0.00
S 1 0.25
T 2 0.50
U 0 0.00
V 0 0.00
W 0 0.00
X 0 0.00
Y 0 0.00
Z 0 0.00


Note that all the letters used to make 'test' have a number and all of the ones that don't have 0 0.00




Hi talksr and thank you for your reply, the program does the following:
The program reads the string from the user, counts the number of times each letter occurs in the string, and computes the letter frequencies. Also displays the letters, the letter counts, and the letter frequencies all this without using global variables.

What I am working on right now is to use functions to make the program do the same things! jjhaag is right stdafx.h is a precompiled header for Visual Studio, I am using Visual Studio 2008 and it can be written: #include <stdafx.h> or #include "stdafx.h"


Ah thanks for your help guys. I have learned some new things from christinamoon's example! icon_up.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 06:08AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month