| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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; } |
|
#5
|
|||
|
|||
|
it wont terminate the loop with the "?" for some reason, and i dont understand why, do you have any idea?
|
|
#6
|
|||
|
|||
|
Quote:
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. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > C++ help needed |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|