GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Mar-2010, 10:17
Petr_Kropotkin Petr_Kropotkin is offline
Junior Member
 
Join Date: Feb 2010
Posts: 77
Petr_Kropotkin is on a distinguished road

Weird printing problems


I am writing a lexical analyser. Almost done except for one minor problem,
one my my functions can't count words and I don't know why ?

I got it to work twice then it crapped out. Here is the code.
CPP / C++ / C Code:
[code]

void word_stats()
{
 int stats[8];
  int len;
  char *word="hello";
len=strlen(word);

switch(len)
{
case 2:
 stats[0]++;
 break;
case 3:
 stats[1]++;
 break;
case 4:
 stats[2]++;
 break;
case 5:
 stats[3]++;
 break;
case 6:
 stats[4]++;
 break;
case 7:
 stats[5]++;
 break;
case 8:
 stats[6]++;
 break;
case 9:
 stats[7]++;
 break;
}
}


void print_out(int stats[8])
{
 cout<<endl;
 cout<<"Word length                Occurance"<<endl;
 cout<<"-----------------------------------------"<<endl;
 cout<< "2                            "<< stats[0]<<endl;
 cout<< "3                            "<< stats[1]<<endl;
 cout<< "4                            "<< stats[2]<<endl;
 cout<< "5                            "<< stats[3]<<endl;
 cout<< "6                            "<< stats[4]<<endl;
 cout<< "7                            "<< stats[5]<<endl;
 cout<< "8                            "<< stats[6]<<endl;
 cout<< "9                            "<< stats[7]<<endl;
}


int main()
{/* some string entered in here through cin.getline()
token=strtok(sentance," .");
 while (token != NULL )
 {
    word_stats(token);
     token=strtok(NULL, ". ");
}
print_out(stats);
[/code]
I don't know why print_out reads empty. Hrlp.
  #2  
Old 09-Mar-2010, 10:45
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 815
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: wierd printing problems


I don't see where print_out() gets called. I see it in your maim() function but it seems to be commented out and incomplete. I don't see a main() function either. More than likely, your problem lies in the fact that you expect the stats[] in word_stats() to be the same array as the one passed into print_out(). This is not the case because it is a local variable for the word_stats() function. Instead, maybe you should take it in as a parameter like you did in the print_out() function.
  #3  
Old 09-Mar-2010, 10:52
Petr_Kropotkin Petr_Kropotkin is offline
Junior Member
 
Join Date: Feb 2010
Posts: 77
Petr_Kropotkin is on a distinguished road

Re: wierd printing problems


Quote:
Originally Posted by fakepoo
I don't see where print_out() gets called. I see it in your maim() function but it seems to be commented out and incomplete. I don't see a main() function either. More than likely, your problem lies in the fact that you expect the stats[] in word_stats() to be the same array as the one passed into print_out(). This is not the case because it is a local variable for the word_stats() function. Instead, maybe you should take it in as a parameter like you did in the print_out() function.

maim is main() .
  #4  
Old 09-Mar-2010, 10:54
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 354
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: wierd printing problems


I think this is what you're trying to accomplish:

CPP / C++ / C Code:
void word_stats(char * word, int stats [8])
{
	int len=strlen(word);
	if (len >= 2 && len <= 9) ++stats[len - 2];
}


void print_out(int stats[8])
{
	cout<<endl;
	cout<<"Word length                Occurance"<<endl;
	cout<<"-----------------------------------------"<<endl;
	cout<< "2                            "<< stats[0]<<endl;
	cout<< "3                            "<< stats[1]<<endl;
	cout<< "4                            "<< stats[2]<<endl;
	cout<< "5                            "<< stats[3]<<endl;
	cout<< "6                            "<< stats[4]<<endl;
	cout<< "7                            "<< stats[5]<<endl;
	cout<< "8                            "<< stats[6]<<endl;
	cout<< "9                            "<< stats[7]<<endl;
}


int main()
{
	char sentence [] = "I bent my wookie.";
	int stats [8];
	for (int i=0; i<8; ++i) stats[i] = 0;
	for (char * token = strtok(sentence," .?!"); token; token=strtok(NULL, " .?!"))
	{
		word_stats(token, stats);
	}
	print_out(stats);
}

The stats array in your word_stats function is only local to that function. When the function returns, the array no longer exists, so the function isn't really doing anything. You need to give it the array as a parameter (or use a global variable). I also got rid of the big switch statement, and changed the for loop to a while loop (that's just a matter of preference, the way you had it was fine).
__________________
www.blake-foster.com
  #5  
Old 09-Mar-2010, 16:47
Petr_Kropotkin Petr_Kropotkin is offline
Junior Member
 
Join Date: Feb 2010
Posts: 77
Petr_Kropotkin is on a distinguished road

Re: wierd printing problems


Quote:
Originally Posted by Blake
I think this is what you're trying to accomplish:

CPP / C++ / C Code:
void word_stats(char * word, int stats [8])
{
	int len=strlen(word);
	if (len >= 2 && len <= 9) ++stats[len - 2];
}


void print_out(int stats[8])
{
	cout<<endl;
	cout<<"Word length                Occurance"<<endl;
	cout<<"-----------------------------------------"<<endl;
	cout<< "2                            "<< stats[0]<<endl;
	cout<< "3                            "<< stats[1]<<endl;
	cout<< "4                            "<< stats[2]<<endl;
	cout<< "5                            "<< stats[3]<<endl;
	cout<< "6                            "<< stats[4]<<endl;
	cout<< "7                            "<< stats[5]<<endl;
	cout<< "8                            "<< stats[6]<<endl;
	cout<< "9                            "<< stats[7]<<endl;
}


int main()
{
	char sentence [] = "I bent my wookie.";
	int stats [8];
	for (int i=0; i<8; ++i) stats[i] = 0;
	for (char * token = strtok(sentence," .?!"); token; token=strtok(NULL, " .?!"))
	{
		word_stats(token, stats);
	}
	print_out(stats);
}

The stats array in your word_stats function is only local to that function. When the function returns, the array no longer exists, so the function isn't really doing anything. You need to give it the array as a parameter (or use a global variable). I also got rid of the big switch statement, and changed the for loop to a while loop (that's just a matter of preference, the way you had it was fine).

That's a COOL solution. Why didn't I think of that.
  #6  
Old 09-Mar-2010, 20:21
Petr_Kropotkin Petr_Kropotkin is offline
Junior Member
 
Join Date: Feb 2010
Posts: 77
Petr_Kropotkin is on a distinguished road

Re: wierd printing problems


Quote:
Originally Posted by Petr_Kropotkin
That's a COOL solution. Why didn't I think of that.

One final problem It's a biggy. It doesn't tally the words .
Here is the code.
CPP / C++ / C Code:
[code]
void word_stats(char sentance[30])
{   
  short int len;
  char *token;
 token=strtok(sentance,".");
    while (token !=NULL )
    {
     len=strlen(token);
     if (len >= 2 && len <= 9)  stats[len - 2]++;
     token=strtok(NULL, ".");
    }

void read_out()
{
 cout<<endl;
 cout<<"Word length                Occurance"<<endl;
 cout<<"-----------------------------------------"<<endl;
 cout<< "2                            "<< stats[0]<<endl;
 cout<< "3                            "<< stats[1]<<endl;
 cout<< "4                            "<< stats[2]<<endl;
 cout<< "5                            "<< stats[3]<<endl;
 cout<< "6                            "<< stats[4]<<endl;
 cout<< "7                            "<< stats[5]<<endl;
 cout<< "8                            "<< stats[6]<<endl;
 cout<< "9                            "<< stats[7]<<endl;
}




int main()
{


 char sentance[15];
 int count=0;

cout<<"Enter in a line->";

    cin.getline(sentance,50);

letter_stats(sentance);
same_word_count(sentance);
word_stats(sentance);
read_out();

 system("pause");
}

[/code]

I put the parsing loop (while loop) word_stats in the main(), and the affect is
the same. I tried passing parameters to read out, it still didn't help...
  #7  
Old 09-Mar-2010, 20:46
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 354
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: wierd printing problems


You're no longer including a space in the delimiters for strtok. I've fixed the problem below (and I added some other punctuation marks). It would be best to put the delimiters in a variable, so that if you ever change them, you only need to change them in one place.

I don't see letter_stats or same_word_count defined in your code, so I commented them out.

I've made a few other changes too (call them suggestions):

I'm assuming that stats is a global variable that got left out by mistake when you posted your code. I added it at the top.

I added a for loop to your read_out function to replace all the couts.

I changed all occurrences of "sentance" to the correct spelling.

I changed the length of your sentence variable to 50, so that it matches the buffer size you pass to cin.getline.

CPP / C++ / C Code:
int stats [8];
const char delim [] = " .?!,";

void word_stats(char sentence[30])
{   
	short int len;
	char * token;
	token = strtok(sentence, delim);
	while (token != NULL)
	{
		len=strlen(token);
		if (len >= 2 && len <= 9)  stats[len - 2]++;
		token=strtok(NULL, delim);
	}
}

void read_out()
{
	cout<<endl;
	cout<<"Word length                Occurance"<<endl;
	cout<<"-----------------------------------------"<<endl;
	for (int i=0; i<8; ++i) 
	{
		cout<<(i + 2)<<"                            "<< stats[i]<<endl;
	}
}

int main()
{
	char sentence [50];
	cout<<"Enter in a line->";
	cin.getline(sentence,50);
	cout<<sentence<<endl;
	//letter_stats(sentence);
	//same_word_count(sentence);
	word_stats(sentence);
	read_out();
	system("pause");
}
__________________
www.blake-foster.com
  #8  
Old 09-Mar-2010, 20:54
Petr_Kropotkin Petr_Kropotkin is offline
Junior Member
 
Join Date: Feb 2010
Posts: 77
Petr_Kropotkin is on a distinguished road

Re: wierd printing problems


word_stats still does not show the total.
  #9  
Old 09-Mar-2010, 20:58
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 354
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: wierd printing problems


Could you post ALL of your code? And whatever output it's giving you? When I run the code I posted above, it works exactly as it's supposed to.
__________________
www.blake-foster.com
  #10  
Old 09-Mar-2010, 21:21
Petr_Kropotkin Petr_Kropotkin is offline
Junior Member
 
Join Date: Feb 2010
Posts: 77
Petr_Kropotkin is on a distinguished road

Re: wierd printing problems


Quote:
Originally Posted by Blake
Could you post ALL of your code? And whatever output it's giving you? When I run the code I posted above, it works exactly as it's supposed to.
ok
CPP / C++ / C Code:
[code]
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
using  namespace std;

int letters[26];
  char sentance[15],text[60];
 int len;

void word_stats(char sentance[30])
{

    short int len;
    int stats[9];
    char * token;
    token = strtok(sentance, ".");
    while (token !=NULL )
    {
            len=strlen(token);
            if (len >= 2 && len <= 9)  stats[len - 2]++;
            cout<<len;
            token=strtok(NULL, ".");
    }

 }


void print_out(int stats[9])
{

    cout<<endl;
            cout<<"Word length                Occurance"<<endl;
            cout<<"-----------------------------------------"<<endl;
            for (int i=0; i<8; ++i)
            {
                    cout<<(i + 2)<<"                            "<< stats[i]<<endl;
            }
    }






void letter_stats(char text[50])
{
    int i;

    for(i=0;text[i]!='.';i++)
    {
    switch(text[i])
    {
    case 'a':
     letters[0]++;
     break;
    case 'b' :
     letters[1]++;
     break;
     case 'c':
    letters[2]++;
    case 'd':
    letters[3]++;
    case 'e' :
    letters[4]++;
     break;
   case 'f':
   letters[5]++;
     break;
    case 'g':
    letters[6]++;
     break;
    case 'h' :
    letters[7]++;
     break;
   case 'i':
    letters[8]++;
     break;
    case 'j':
   letters[9]++;
     break;
    case 'k' :
    letters[10]++;
     break;
   case 'l':
     letters[11]++;
     break;
    case 'm':
    letters[12]++;
     break;
    case 'n' :
    letters[13]++;
     break;
   case 'o':
    letters[14]++;
     break;
    case 'p':
    letters[15]++;
     break;
    case 'q' :
     letters[16]++;
     break;
   case 'r':
    letters[17]++;
     break;
    case 's':
     letters[18]++;
     break;
    case 't' :
   letters[19]++;
     break;
   case 'u':
    letters[20]++;
     break;
    case 'v':
     letters[21]++;;
     break;
    case 'w' :
     letters[22]++;
     break;
   case 'x':
    letters[23]++;
     break;
    case 'y':
   letters[24]++;
     break;
   case 'z':
    letters[25]++;
     break;

 }
}
    cout<<letters[0]<<" a's"<<endl;
    cout<<letters[1]<<" b's "<<endl;
    cout<<letters[2]<<" c' s "<<endl;
    cout<<letters[3]<<" d's' "<<endl;
    cout<<letters[4]<<" e's"<<endl;
    cout<<letters[5]<<" f's "<<endl;
    cout<<letters[6]<<" g' s "<<endl;
    cout<<letters[7]<<" h's' "<<endl;
    cout<<letters[8]<<" i's"<<endl;
    cout<<letters[9]<<" j's "<<endl;
    cout<<letters[10]<<" k' s "<<endl;
    cout<<letters[11]<<" l's' "<<endl;
    cout<<letters[12]<<" m's"<<endl;
    cout<<letters[13]<<" n's "<<endl;
    cout<<letters[14]<<" o' s "<<endl;
    cout<<letters[15]<<" p's' "<<endl;
    cout<<letters[16]<<" q's"<<endl;
    cout<<letters[17]<<" r's "<<endl;
    cout<<letters[18]<<" s' s "<<endl;
    cout<<letters[19]<<" t's' "<<endl;
    cout<<letters[20]<<" u's"<<endl;
    cout<<letters[21]<<" v's "<<endl;
    cout<<letters[22]<<" w' s "<<endl;
    cout<<letters[23]<<" x's' "<<endl;
    cout<<letters[24]<<" y's "<<endl;
    cout<<letters[25]<<" z' s "<<endl;

}


void same_word_count(char text[50])
{
    int i,count,samewords[15]={0};
    cout<<"Words                              occurances     "<<endl;
    cout<<"----------------------------------------------"<<endl;

    for(i=0;text[i]!='.';i++)
    {
    for(count=1; text[count]!='.' ;count++)
    if(text[i]==text[count])
        samewords[count]++;

    }

    for(count=0; text[count]!='.' ;count++)
    if (text[count]==' ')
        cout<<endl;
     else
    {
      cout <<text[count] <<samewords[count];
  }
}




int main()
{

    int stats[9]={0},count=0;
 char sentance[15];

cout<<"Enter in two lines";
while(count++<2)
{
    cin.getline(sentance,50);
    strcat(text,sentance);
}
letter_stats(text);
same_word_count(text);
word_stats(text);

print_out(stats);
 system("pause");
}
[/code]


I tried global and local. One time stats gave me garbage when I made it
local. Then I initialised it ,and it gaves me 0's args...
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
IE 7 Problems colliegirl Computer Software Forum - Windows 6 05-Jun-2008 08:10
Problems while burning CD's netnut Computer Software Forum - Windows 16 17-Jan-2008 23:45
Printing problems--->ARG! toddm123 .NET Forum 0 31-Mar-2005 12:51
Chaintech Geforce 5600 FX problems bartster74 Computer Hardware Forum 8 04-May-2004 13:16
Serious Problems: Exabyte Products and Service pkazmercyk@comc Computer Hardware Forum 2 05-Feb-2004 07:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:02.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.