C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 11th, 2008, 05:54 PM
RyanD RyanD is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 7 RyanD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 23 m 29 sec
Reputation Power: 0
Letter count program

Okay, I'm in a huuuge rush to get these programs done by friday.

COPY PASTA:
You are to write a letter counting program to try to discover what letters of the alphabet are most commonly used. Your program will assume that all letters are CAPITAL letters (If they are lower case, you must convert them!), and will read in a specified number of (up to) 80-character lines. At the end, your program should print each letter followed by a BAR GRAPH showing how many times it was used. For example, your output might look like:
A ********** (10)
B *** (3)
C ** (2)


Okie dokie, anyways I have basically the whole program written but after I enter a phrase I get an error saying

"Stack around letterfreqs" is corrupted. It won't work.

Can someone please:
-Help me find out why this is happening
-Give tips to ensure correct output


Heres the code:

Code:
//Letter count -	Nov.7 - Ryan Doran
//             -    This program asks the user to input (up to) 80 character lines
//                  After the lines are inputted, a bar graph displaying the letter
//                  amount of times used in sarts and a number is shown.

#include <iostream>
#include <conio.h>
using namespace std;

//Prototypes
void initfreqs(int letterfreqs[26]);   //Initializes frequencies of the numbers to 0.
void processlines(int letterfreqs[26], char templ8[26] );  //Processes the information in the lines
                                                           //(How many of each letter etc.)

void printout(int letterfreqs[26], char templ8[26]);  //Prints in a bar graph


int main(void)
{
int letterfreqs[26]; //No blanks or spaces
char templ8[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','  M','N','O','P','Q','R','S','T','U','V','W','X','Y'  ,'Z'};     
                     //26 Character template



initfreqs(letterfreqs );    //Initialize, setting all to 0.
processlines(letterfreqs, templ8 ); //This asks how many lines there will be, 
                                    //and then updates the letter frequencies for that many lines
printout(letterfreqs, templ8);     //{Produces the Bar Graphs!}

return 0;
}//Main


void initfreqs(int letterfreqs[26])
//letterfreqs - the frequency in which a letter appears in a phrase
{
 int count;      //Loop counter

 //Set all elements in letterfreqs to 0
 for (count = 0; count <26; count++)
      letterfreqs[count]=0;
}//initfreqs


void processlines(int letterfreqs[26], char templ8[26] )
//letterfreqs - the frequency in which a letter appears in a phrase
//templ8 - Used to count when a letter pops up
{
 int pos;     //Loop counter
 int howmany; //How many lines want to be entered
 int alpha;   //Used to count how many of one letter are in each line.
 int count;   //Loop counter
 char newline[81];  //81 characters of input from the user

 cout<<"How many lines of (up to) 81 characters? "<<endl;
 cin>>howmany;

 for(count = 1; count <= howmany; count++)
 {

  //Read in 1 line (see davidLETTERman)
  cout<<"Enter phrase (up 80 characters): ";
  pos=0;
  do{newline[pos] = toupper(_getche());
     pos++;
  }while ((newline[pos-1]!='\r')&&(pos < 80));
  {//Find which letter it alphabet
   alpha = 0;
   while(newline[pos] != templ8[alpha])
         alpha++;
   //If found - update the frequency
   letterfreqs[alpha]++;
   }//while
 }//for
}//Processlines

//Printout
//like lotto 6/49
void printout(int letterfreqs[26], char templ8[26])
{
 int pos;   //Loop counter
 int stars; //Loop counter


	for(pos=0; pos<26; pos++)
	{cout<<templ8[pos];
	for (stars=1; stars <= letterfreqs[pos]; stars++)
		     cout<<"*";
	   cout<<endl;
     } // for 
}//Printout




 



Thanks for any help!

Reply With Quote
  #2  
Old November 12th, 2008, 04:25 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 1
For a start you definitely don't need the temp18 array, you can work out the letters easier in code.

Reply With Quote
  #3  
Old November 12th, 2008, 04:44 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 1
Was very close to working, i got it going and tidied it up a bit.

Code:
//Letter count -	Nov.7 - Ryan Doran
//             -    This program asks the user to input (up to) 80 character lines
//                  After the lines are inputted, a bar graph displaying the letter
//                  amount of times used in sarts and a number is shown.

#include <iostream>
#include <conio.h>
using namespace std;

//Prototypes
void initfreqs		(int *letterfreqs);   //Initializes frequencies of the numbers to 0.
void processlines	(int *letterfreqs);  //Processes the information in the lines
void printout		(int *letterfreqs);  //Prints in a bar graph

int main(void)
{
	int letterfreqs[26];//No blanks or spaces

	initfreqs(letterfreqs );	//Initialize, setting all to 0.
	processlines(letterfreqs);	//This asks how many lines there will be, 
								//and then updates the letter frequencies for that many lines
	cout << endl;

	printout(letterfreqs);     //{Produces the Bar Graphs!}

	cout << "Hit Space To Exit\n";

	while ( getch() != 32 );

	return 0;
}//Main

void initfreqs(int *letterfreqs) //letterfreqs - the frequency in which a letter appears in a phrase
{
	for (int count = 0; count <26; count++) //Set all elements in letterfreqs to 0
		letterfreqs[count] = 0;
}//initfreqs

void processlines(int *letterfreqs) //letterfreqs - the frequency in which a letter appears in a phrase
{
	int howmany; //How many lines want to be entered

	cout << "How many lines of (up to) 81 characters? "<<endl;
	cin >> howmany;

	for(int count = 1; count <= howmany; count++)
	{
		char newline[81];  //81 characters of input from the user
		int pos = 0;

		cout<<"Enter phrase (up 80 characters): ";

		do
		{
			//Read in 1 line (see davidLETTERman)
			newline[pos] = toupper(_getche());

			// this bit was outside the do/while loop so not run properly
			int alpha = newline[pos] - 65;

			if(alpha >=0 && alpha <= 25)
				++letterfreqs[alpha];
	
			++pos;
		}
		while ( (newline[pos-1]!='\r') && (pos < 80) );

	}//for
}//Processlines

//Printout - like lotto 6/49
void printout(int *letterfreqs)
{
	for(int pos=0; pos<26; pos++)
	{
		cout<< (char)(pos + 65);

		for (int stars=1; stars <= letterfreqs[pos]; stars++)
			cout<<"*";

		cout<<endl;
	} // for 
}//Printout

Reply With Quote
  #4  
Old November 12th, 2008, 09:36 PM
RyanD RyanD is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 7 RyanD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 23 m 29 sec
Reputation Power: 0
Thanks a bunch! (Although what did you do when you changed my parameters? lol.)

Reply With Quote
  #5  
Old November 13th, 2008, 02:34 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 1
pointer (the *) and arrays ( the []) are actually both passed to functions as a pointer (to the first element in the case of an array) in c / c++.

I prefer using the * myself but it could a matter of taste.

I usually err on the side of less typing.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Letter count program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT