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!
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:
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;
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
@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.
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
@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!
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!