5 Replies - 7434 Views - Last Post: 12 February 2012 - 07:45 AM Rate Topic: -----

#1 cieru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-January 12

Counting Words in A String

Posted 10 February 2012 - 10:39 AM

Good Day! :) I was currently working on a C++ Program that will count the number of words in and entered string...I figured out already how to count the words...But we must repeat the process by using do while loop...That is the start of my problem...The loop is working fine...But in the secong run for counting the words, the output always result the number of words from the first trial :(...

here's my code:


#include<iostream>
using namespace std;
#include<string>

void newline();

int main()
{
	char s[100];
	char ans;
	int numWords = 0; 
	int i=0;
	do{
		numWords=0;
		cout <<"\nEnter a String: ";
		cin.getline(s,100);
		while(i<strlen(s))
		{

			if(s[i] != '.' && s[i] != '!' && s[i] != '?' 
                    && s[i] != ' ' && s[i] != ','){
				numWords++;
				while(s[i] != '.' && s[i] != '!' && s[i] != '?' 
                                  && s[i] != ' ' && s[i] != ',' && i < strlen(s)){
					i++;
				}
			}
			else {
			i++;
			}
			
		}
		cout<<"\nThe String contains : ";
		cout << numWords << endl;
		cout << "\nTry Again? (Y or N): ";
		cin >> ans;
		newline();
	}while(ans =='y' &&'Y');
	cout <<"\nThank You! Press Enter to Exit the Program...";

	return 0;
	system ("pause>0");
}

void newline()
{
	char s;
	do{
		cin.get(s);
	}while(s!='\n');
}




any suggestions? I've checked the program for a hundred times now..>,<...sorry for being like this...I'm totally a beginner :( Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Counting Words in A String

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Counting Words in A String

Posted 10 February 2012 - 11:50 AM

In the following snippet, after your first run:
	do{
		numWords=0;
		cout <<"\nEnter a String: ";
		cin.getline(s,100);
		while(i<strlen(s))

What is the purpose of your variable i in the second loop? What is it's value after your first run? What is the value of strlen(s)? What happens if strlen(s) for the second run is smaller than the previous run?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Counting Words in A String

Posted 10 February 2012 - 12:00 PM

You are just no reseting the i, do it after
numWords=0


do
i=0

Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,339
  • Joined: 31-December 10

Re: Counting Words in A String

Posted 10 February 2012 - 01:38 PM

It really looks like you're doing more work than you really have to. You should check out all the member functions for strings because you can do most of what you need with just those functions. For instance, you use the strlen function which is fine but another way would be too just use either the size() or length() member function to get the length of the string. Here's a good reference site: http://cplusplus.com/reference/.
Was This Post Helpful? 0
  • +
  • -

#5 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Counting Words in A String

Posted 10 February 2012 - 02:34 PM

@vividexstance - It's a C-string so it does not have those methods. Also I'd rather encourage him to use C-strings so he can learn how to manage and understand how things work. When I took my first lessons with programming in college we couldn't even use functions from "cstring" to actually learn as much as possible about char arrays and c-strings.
Was This Post Helpful? 0
  • +
  • -

#6 cieru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-January 12

Re: Counting Words in A String

Posted 12 February 2012 - 07:45 AM

Thank for the help :) I'll try to check the given links and new ideas to improve my program...

View Postshurd, on 10 February 2012 - 12:00 PM, said:

You are just no reseting the i, do it after
numWords=0


do
i=0



:D OH thanks! I tried to change the positioning of the i, and numWords within the program :))) It functions succesfully :D Thanks again :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1