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 01-May-2006, 12:13
Comp_Lover Comp_Lover is offline
New Member
 
Join Date: May 2006
Posts: 3
Comp_Lover is on a distinguished road

My first thread and i hope that u help me ^_^


Hi all.

This is my first participation here, in this perfect forum.

first of all, I'm a student in comp. science and I'm still a bigginer in programming.

I'm facing some problems in strings and i hope that u'll help me.

here i have some problems :

How can i read a paragraph from a file then allow the user 2 edit it ??

I mean by editing that how can i allow him 2 add, remove or modify a word !!!!

one more thing ..

how can i print on the screen the following (( total numbers )) :

• Characters (All)
• Words
• Lines
• Spaces
• Commas
• Full stops

what i think that we can print all these by declaring a counter 4 each one, but the main problem that i faced is how 2 read it from a file correctly ??

I hope that u understood me ^_^

Waiting 4 ure replies ..!!
  #2  
Old 01-May-2006, 13:40
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: My first thread and i hope that u help me ^_^


Welcome Comp_Lover,
It would be easier if you could post what you have so far or what you have tried. That way the help that is offered can be tailored for the approach you had in mind.
  #3  
Old 02-May-2006, 09:27
Comp_Lover Comp_Lover is offline
New Member
 
Join Date: May 2006
Posts: 3
Comp_Lover is on a distinguished road

Re: My first thread and i hope that u help me ^_^


This is all what i did.

Really i'm bad in strings.

------------------------------------------------------
CPP / C++ / C Code:
#include "iostream.h"
#include "iomanip.h"
#include "fstream.h"
#include <cstring>
#include <string>
using std::string;

int main ()
{
	int choice;
	const int size = 1000;
	char string[size];
	int charCounter=0;
	int wordCounter=0;
	int lineCounter=0;
	int spaceCounter=0;
	int commascounter=0;
	int fullstopsCount=0;


	ifstream myfile;
	myfile.open( "cheatingpolicy.txt" );

	if ( myfile.fail() )
	{
		cout << "Cannot open file ..!!" << endl;
		return 1;
	}

	for ( int i=0; i<size; i++ )
	{
		myfile >> string[i];

		if ( myfile.eof() )
			break;

		++charCounter;

		if ( string[i] == ' ' )
		{
			++wordCounter;
			++spaceCounter;
		}

		if ( string[i] == ',' )
			++commascounter;

		if ( string[i] == '.' )
			++fullstopsCount;
	}

	do 
	{
		cout << "Select the choice you want please: " << endl << endl;
		cout << "1. Display Cheating policy." << endl;
		cout << "2. Display statistical report." << endl;
		cout << "3. Display unsorted words." << endl;
		cout << "4. Sort the words." << endl;
		cout << "5. Display sorted words." << endl;
		cout << "6. Add a word." << endl;
		cout << "7. Remove a word." << endl;
		cout << "8. Modify a word." << endl;
		cout << "9. Exit." << endl;
		
		cin >> choice;

		if ( choice == 1 )
		{
			for ( int j=0; j<size ; j++ )
			{
				cout << string[j];

				if ( (j % 30) == 0 )
				{
					cout << endl;
					++lineCounter;
				}
			}
		}

		if ( choice == 2 )
		{
			cout << "There are " << charCounter << " charachters" << endl;
			cout << setw(7) << wordCounter << " words" << endl;
			cout << setw(7) << spaceCounter << " spaces" << endl;
			cout << setw(7) << commascounter << " commas" << endl;
			cout << setw(7) << fullstopsCount << " fullstops" << endl;
			cout << setw(7) << lineCounter << " lines." << endl;
		}

		if ( choice == 9 )
			return 1;

	}while (-1);

	return 0;
}

And the problem is in an attachment ..
Attached Images
File Type: pdf PA6-AK.pdf (59.1 KB, 1 views)
Last edited by admin : 06-May-2006 at 07:54. Reason: Please insert your C code between [c] & [/c] tags
  #4  
Old 02-May-2006, 10:29
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: My first thread and i hope that u help me ^_^


For simplitcty and testing at this stage I have removed some code so that it can be added back in stages. So we have this.
CPP / C++ / C Code:
#include <iostream>
#include <ostream>
#include <iomanip>

using namespace std;

int main ()
{
	int choice;
	const int size = 1000;
	char string[size] = "Hi all. This is my first participation here, in this perfect forum. first of all, I am a student in comp. science and I am still a bigginer in programming.";
	int charCounter=0;
	int wordCounter=0;
	int lineCounter=0;
	int spaceCounter=0;
	int commascounter=0;
	int fullstopsCount=0;

	return 0;
}
You have this.
CPP / C++ / C Code:
for ( int i=0; i<size; i++ )
For the size in this "for loop" you should use how many characters are in the char array not the size you allocated.
Let us take a look at how to get the "size" of a char array. One way is to use "strlen" from #include <cstring>.
CPP / C++ / C Code:
int test = strlen(string);
If you want to write the code and not use "strlen" then you could just do something like this.
CPP / C++ / C Code:
    int index = 0;
    while(string[index])
        index++;
The reason the while loop works is because when the loop encounters the null character('\0') it will fail.
So that will let you setup your "for loop" and give you the answer to "Characters (All)". So the next step would be to set that up and go from there by adding back stuff one thing at a time making sure they work as expected before adding more code.
I know this is does not answer all your questions. But we have to start some place. Post any updated code and any error messages if there are any.
  #5  
Old 03-May-2006, 11:09
Comp_Lover Comp_Lover is offline
New Member
 
Join Date: May 2006
Posts: 3
Comp_Lover is on a distinguished road

Re: My first thread and i hope that u help me ^_^


Helle mr. Sokar

This is all what i did.

please check it.

CPP / C++ / C Code:
#include "iostream.h"
#include "iomanip.h"
#include "fstream.h"
#include <cstring>

void charCounter( char [] ); // this function is counting the charachters.
void wordCounter( char [] ); // this function is counting the words.
void lineCounter( char [] ); // this function is counting the lines.
void spaceCounter( char [] ); // this function is counting the spaces.
void commascounter( char [] ); // this function is counting the commas.
void fullstopsCount( char [] );// this function is counting the fullstops.
void sortingWords( char [] ); // this function is sorting the words.

int main ()
{
	int choice;
	const int size = 1000;
	char charachter;
	char string1[size];
	char string2[size];
	char sortedWords[size];
	char word[50]; // this string save the word that will be added by the user.
	char word2[50];


	ifstream myfile;
	myfile.open( "cheatingpolicy.txt" );

	if ( myfile.fail() )
	{
		cout << "Cannot open file ..!!" << endl;
		return 1;
	}

	while(myfile.eof())
	{
		for (int i=0;i<1000;i++)
		{
			myfile >> charachter;
			string1[i] = charachter;
		}
	}

	strcpy(string2,string1);
	strcpy(sortedWords,string1);

	do 
	{
		cout << "Select the choice you want please: " << endl << endl;
		cout << "1. Display Cheating policy." << endl;
		cout << "2. Display statistical report." << endl;
		cout << "3. Display unsorted words." << endl;
		cout << "4. Sort the words." << endl;
		cout << "5. Display sorted words." << endl;
		cout << "6. Add a word." << endl;
		cout << "7. Remove a word." << endl;
		cout << "8. Modify a word." << endl;
		cout << "9. Exit." << endl;
		
		cin >> choice;

		if ( choice == 1 )
		{
			for ( int z=0; z<size; z++ )
				cout << string1;
		}

		if ( choice == 2 )
		{
			charCounter( string1 );
			wordCounter( string1 );
			lineCounter( string1 );
			spaceCounter( string1 );
			commascounter( string1 );
			fullstopsCount( string1 );
			sortingWords( string1 );
		}

		if ( choice == 3 )
			cout << string2;

		if ( choice == 4 )
			sortingWords( sortedWords );

		if ( choice == 5 )
			cout << sortedWords;

		if ( choice == 6)
		{
			cout << "Print the word please.. " << endl;
			cin >> word;

			cout << "Print the word that you want to add yours after.." << endl;
			cin >> word2;
			for ( int m=0; m<size; m++ )
				if ( string2[m] == word2[m] )
				{
					strcat(string2,word);
					word2[m] = '\n';
				}
		}

		if ( choice == 7 )
		{
			cout << "Print the word that you want to remove.." << endl;
			cin >> word2;

			for ( int n=0; n<size; n++ )
				if ( string2[n] == word2[n] )
				{
					string2[n] = '\n';
				}
		}

		if ( choice == 9 )
			return 1;

	}while (-1);

	return 0;
}

void charCounter( char charachter [1000] )
{
	cout << "There are " << strlen(charachter) << " charechter" << endl;
}

void wordCounter( char words[1000] )
{
	int counter=0;
	char *stringPtr;

	stringPtr = strtok(words," ");

	while (stringPtr!=NULL)
	{
		stringPtr = strtok(NULL," ");
		counter++;
	}

	cout << setw(11) << counter << " words" << endl;
}

void lineCounter( char lines [1000] )
{
	int counterOfLines =0;

	for ( int i=0; i<1000; i++ )
	{
		if ( lines[i] == '\n' )
			++counterOfLines;
	}

	cout << setw(11) << counterOfLines << " lines" << endl;
}

void spaceCounter( char spaces [1000] )
{
	int counterOfSpaces =0;

	for ( int i=0; i<1000; i++ )
	{
		if ( spaces[i] == '\0' )
			++counterOfSpaces;
	}

	cout << setw(11) << counterOfSpaces << " spaces" << endl;
}

void commascounter( char commas [] )
{
	int counterOfCommas = 0;

	for ( int i=0; i<1000; i++ )
	{
		if ( commas[i] == ',' )
			++counterOfCommas;
	}

	cout << setw(11) << counterOfCommas << " commas" << endl;
}

void fullstopsCount( char fStops [] )
{
	int counterOfFullStp = 0;

	for ( int i=0; i<1000; i++ )
	{
		if ( fStops[i] == '.' )
			++counterOfFullStp;
	}

	cout << setw(11) << counterOfFullStp << " fullstops" << endl;
}

void sortingWords( char Sort [] )
{
	int empty;

	for ( int j=0; j<999; j++ )
		
		for ( int k=0; k<999; k++ )

			if ( Sort[k] > Sort[k+1] )
			{
				empty = Sort[k];
				Sort[k] = Sort[k+1];
				Sort[k+1] = empty;
			}
}
  #6  
Old 03-May-2006, 11:15
cjavac#c++_vbP cjavac#c++_vbP is offline
Junior Member
 
Join Date: Mar 2006
Location: Miami, FL
Posts: 42
cjavac#c++_vbP is on a distinguished road

Re: My first thread and i hope that u help me ^_^


void spaceCounter( char spaces [1000] )
{
int counterOfSpaces =0;

for ( int i=0; i<1000; i++ )
{
if ( spaces[i] == '\0' )
++counterOfSpaces;
}

cout << setw(11) << counterOfSpaces << " spaces" << endl;
}

I think the space character is ' ' not '\0'. Isnt '\0' the NULL character.
  #7  
Old 03-May-2006, 11:19
cjavac#c++_vbP cjavac#c++_vbP is offline
Junior Member
 
Join Date: Mar 2006
Location: Miami, FL
Posts: 42
cjavac#c++_vbP is on a distinguished road

Re: My first thread and i hope that u help me ^_^


void wordCounter( char words[1000] )
{
int counter=0;
char *stringPtr;

stringPtr = strtok(words," ");

while (stringPtr!=NULL)
{
stringPtr = strtok(NULL," ");
counter++;
}

cout << setw(11) << counter << " words" << endl;
}

I think also for word count, what if the person enter two or more consecutive spaces like: "I(3 spaces)am" versus "I(1 space)am" think we need to examine the current and either
(a) increment if the current is a space && decrement if the previous was a space.
or
(b) increment if the current is a space && decrement if the next is a space.'
or
(c) increment only if the next character is not a space or comma or fullstop or question mark .... you get the idea. Am sure we dont want to count "I am a boy. . . ." and count the .'s as words.

Ofcourse we would have to make sure when looking at the next or previous our bounds are safe.
  #8  
Old 03-May-2006, 13:13
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: My first thread and i hope that u help me ^_^


Quote:
Originally Posted by Comp_Lover
Helle mr. Sokar

This is all what i did.

please check it.
I looked at the code but did not get past this.
CPP / C++ / C Code:
	while(myfile.eof())
	{
		for (int i=0;i<1000;i++)
		{
			myfile >> charachter;
			string1[i] = charachter;
		}
	}
When you call "eof()" it returns the value 1 or true if the end of the stream has been reached, 0 or false otherwise. Since the end of file has not been reached it returns "0" and since putting "0" in the "while" statement indicates to stop execution of the loop the loop will never execute.
So what might be a solution?
One might immediately think this.
CPP / C++ / C Code:
	while(!myfile.eof())
	{
		for (int i=0;i<1000;i++)
		{
			myfile >> charachter;
			string1[i] = charachter;
		}
	}
But that in now going to read "1000" characters because of your for loop and when the for loop is finished we go back around to check "while(!myfile.eof())" and the loop will fail only if there are less than a "1000" characters. But what if there are only "100" characters then the other characters are just garbage in the string. The other thing is in many cases it is up to you to make sure the end of the string is marked with the null character '\0'. In this case, had that worked it would have been up to you to add the '\0' to the end of the string. I can not say it enough about in many cases it is up yo you to add the null character '\0' to the end of the string.
You could start testing with something like this maybe.
CPP / C++ / C Code:
#include <fstream>
#include <iostream>
#include <ostream>

using namespace std;

int main ()
{
	const int size = 1000;
	char charachter;
	char string1[size];

	ifstream myfile;
	myfile.open( "cheatingpolicy.txt" );

	if ( myfile.fail() )
	{
		cout << "Cannot open file ..!!" << endl;
		return 1;
	}

	while(!myfile.eof())
	{
		for (int i=0;i<1000;i++)
		{
			myfile >> charachter;
			string1[i] = charachter;
			cout << "i : " << i++ << endl;
		}
	}

	cout << string1 << endl;

	return 0;
}
Replace the name of the file or the contents of the file with something small for testing maybe this.

"Hi all. This is my first participation here, in this perfect forum. first of all, I am a student in comp. science and I am still a bigginer in programming."

Without the quotes. Get that to work and we can take a look at the rest.

Post any updated code and any error messages if there are any.
  #9  
Old 03-May-2006, 13:18
cjavac#c++_vbP cjavac#c++_vbP is offline
Junior Member
 
Join Date: Mar 2006
Location: Miami, FL
Posts: 42
cjavac#c++_vbP is on a distinguished road

Re: My first thread and i hope that u help me ^_^


I apologize for not including the C++ tags......... I jut learned of how to do it couple moments ago from WaltP
  #10  
Old 03-May-2006, 14:54
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: My first thread and i hope that u help me ^_^


CPP / C++ / C Code:
			cout << "i : " << i++ << endl;
why are you incrementing i as you display it? this will only input 500 characters, now
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

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
How to protect single thread in phpBB iota MySQL / PHP Forum 0 20-Mar-2006 23:48
Sending message to GUI thread that ISNT the main thread mpviii MS Visual C++ / MFC Forum 1 20-Apr-2005 13:49
reading from comm port and displaing in editbox using MFc ib_alam MS Visual C++ / MFC Forum 9 02-Feb-2005 19:57
The Official Birthday Wishes thread for jrobbio JdS Open Discussion Forum 8 20-Nov-2003 19:21

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

All times are GMT -6. The time now is 16:03.


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