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 April 27th, 2006, 01:32 PM
danlk2 danlk2 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 7 danlk2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 26 sec
Reputation Power: 0
Angry C++ loop with strings help

Hi, im working with string and trying to take a string from input and count the number of words and letters in the string, here is what i have so far, it wont compile (im not sure why) and i dont know if my counting is working correctly, please help me out.








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

int main()
{

int words=1;
int count = 0;
string mystring;
char next;
do{
cout << "Please enter a sentance, or type "quit." to exit: \n";

while (cin.get(next) && next!='.' && next!='?')
{
mystring[count]=next;
count++;

}
for (int i=0; i<mystring.length(); i++)
{

if(mystring.at(i) == ' ')
words++;
}

cout << "\nYour sentance contains: \n";
cout << words <<" words\n";
cout << mystring.length() <<" letters";

} while (strcmp(mystring,"quit")==0);
return 0;
}

Last edited by danlk2 : April 27th, 2006 at 03:57 PM. Reason: title change

Reply With Quote
  #2  
Old April 27th, 2006, 04:00 PM
danlk2 danlk2 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 7 danlk2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 26 sec
Reputation Power: 0
i forgot to say that the reason for the !='.' and '?' is because i am supposed to stop inputting when i reach one of those two characters

Reply With Quote
  #3  
Old April 27th, 2006, 07:47 PM
danlk2 danlk2 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 7 danlk2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 26 sec
Reputation Power: 0
I got further, it will compile now but do absolutely nothing, i need some help soon guys!!!

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

int main()
{

int words=1;
int count = 0;
string mystring;
string quit;
quit = "quit";
char next;

cout << "Please enter a sentance, or type quit to exit: \n";


while (cin.get(next) && next!='.' && next!='?')
{
mystring[count]=next;
count++;
if (mystring=="quit")
{
break;
}
}
for (int i=0; i<mystring.length(); i++)
{

if(mystring.at(i) == ' ')
words++;
}

cout << "\nYour sentance contains: \n";
cout << words <<" words\n";
cout << mystring.length() <<" letters";


return 0;
}




help, im ripping my hair out, this was supposed to be a "simple" assignment and im real lost

Reply With Quote
  #4  
Old April 27th, 2006, 07:57 PM
Lobo426 Lobo426 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 6 Lobo426 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 25 m 35 sec
Reputation Power: 0
Cool Keep your hair

Here is a way that you can do it. It uses the built in "compare" statement to compare two strings. If you are on a linux based system you can type: "man string" to get the usage and a list of all identifiers. As for the ? & ., the only way to stop inputting as I have it, is to search the strings for each character. But rather than doing your project for you, the example below illustrates how strings work.

As for the code you posted...be VERY careful with Do While structures, if you find yourself using "do", don't, it makes for sloppy code, and can almost always been done using a while instead. Also

int main()
{
int words=0; // start at zero, as user may begin with ?, ., or quit
int letters = 0; // the total number of letters
string mystring; //a string to store information in
string _period( ".");
string _question("?");
string _quit("quit."); //<--must be entered with a period at the end lower case
// of course this can be changed
cout << "Please enter a sentence, or type \"quit.\" to exit: \n"<<endl;
cin>>mystring;
while( mystring.compare( _quit ) != 0 ){
if(mystring.compare( _period ) == 0 ||
mystring.compare( _question ) == 0 )
{
cout<<"words: "<<words<<endl;
cout<<"letters: "<<letters<<endl;
}
else
{
letters += mystring.size();
words++;
}
cin>>mystring;
}
return 0;
}

Reply With Quote
  #5  
Old April 27th, 2006, 09:57 PM
danlk2 danlk2 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 7 danlk2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 26 sec
Reputation Power: 0
it wont terminate the loop with the "?" for some reason, and i dont understand why, do you have any idea?

Reply With Quote
  #6  
Old April 28th, 2006, 03:01 PM
Lobo426 Lobo426 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 6 Lobo426 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 25 m 35 sec
Reputation Power: 0
Cool

Quote:
Originally Posted by danlk2
it wont terminate the loop with the "?" for some reason, and i dont understand why, do you have any idea?


The program is written to terminate on the input of "quit."; The ? and . need to be seperated from the string by white space, unless you want to search each string for the character, as for the way it is written, the program doesn't EXIT until you type quit., leaving the user to continue entering text. You can, of course, only have it cycle through until you get one of the 3 stop conditions.


Like I said, it will give you an idea of how to work with strings.

There are some really useful functions in class string:
s.find("..."); //finds the string within the object s
s.rfind("..."); //finds the string, searching backwards in s
s.find_first_of("..."); //returns subscript of found item, or string::npos if not found

many other functions exist, and many can be overloaded.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C++ help needed


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-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT