Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,650 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,138 people online right now. Registration is fast and FREE... Join Now!




Terminating string with multiple characters.

 
Reply to this topicStart new topic

Terminating string with multiple characters.

zerogee
post 20 Aug, 2008 - 03:06 PM
Post #1


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 45

Hi, I need help. I need to ternimate a string with a '.' or '?' or '!' and then count the number of (user input) character that occur in the string. Problem is my code works if i end the string with \n. but when i code it to terminate the string with '.' or '?' or '!' it will not let me input a character and gives me a number of occurances that are not right.

CODE
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
int main ()
{
int userInput, num_comma;


string message;
  {
      
   cout << "Enter string that is terminating with a '.': \n";
   getline(cin, message, '.');

   cout << "\nString entered is: \n" << message;

   cout << "\n\nEnter a single character followed by the Enter key: ";
   userInput = cin.get();

  
  }

int num_commas = count(message.begin(), message.end(), userInput);  //num_commas holds the number of elements in text

cout << "\n\n" << "The letter occurs " << num_commas << " times in the string.\n\n";

return 0;
}


This post has been edited by zerogee: 20 Aug, 2008 - 04:04 PM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 20 Aug, 2008 - 03:10 PM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,440



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Do you just mean this:
cpp
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
int main ()
{
int userInput, num_comma;


string message;
{
cout << "Curtis Gassen Week 2 Project, #2\n";
cout << "======================================\n\n";

cout << "Enter string that is terminating with a '.': \n";
getline(cin, message, '.');

cout << "\nString entered is: \n" << message;

cout << "\n\nEnter a single character followed by the Enter key: ";
userInput = cin.get();


}

int num_commas = count(message.begin(), message.end(), userInput); //num_commas holds the number of elements in text

cout << "\n\n" << "The letter occurs " << num_commas << " times in the string.\n\n";

return 0;
}
?
More specifically: getline(cin, message, '.');

Oh, and cin.get() will accept enter without a character before it smile.gif
User is online!Profile CardPM

Go to the top of the page

zerogee
post 20 Aug, 2008 - 03:20 PM
Post #3


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 45

QUOTE(gabehabe @ 20 Aug, 2008 - 04:10 PM) *

Do you just mean this:
cpp
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
int main ()
{
int userInput, num_comma;


string message;
{
cout << "Curtis Gassen Week 2 Project, #2\n";
cout << "======================================\n\n";

cout << "Enter string that is terminating with a '.': \n";
getline(cin, message, '.');

cout << "\nString entered is: \n" << message;

cout << "\n\nEnter a single character followed by the Enter key: ";
userInput = cin.get();


}

int num_commas = count(message.begin(), message.end(), userInput); //num_commas holds the number of elements in text

cout << "\n\n" << "The letter occurs " << num_commas << " times in the string.\n\n";

return 0;
}
?
More specifically: getline(cin, message, '.');

Oh, and cin.get() will accept enter without a character before it smile.gif

sorry, i modify the code right after i posted. I already know this code;
CODE
getline(cin, message, '.');
but when the code is entered this way it will not allow a user input for a character to search.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 20 Aug, 2008 - 03:24 PM
Post #4


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,440



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Ahh, the second buffer question of the night.

It stores crap in a temporary buffer after the getline, so you need to make a call to cin.ignore(); right after getline, like so:
cpp
   getline(cin, message, '.');
cin.ignore();

Hope this helps smile.gif
User is online!Profile CardPM

Go to the top of the page

zerogee
post 20 Aug, 2008 - 03:30 PM
Post #5


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 45

QUOTE(gabehabe @ 20 Aug, 2008 - 04:24 PM) *

Ahh, the second buffer question of the night.

It stores crap in a temporary buffer after the getline, so you need to make a call to cin.ignore(); right after getline, like so:
cpp
   getline(cin, message, '.');
cin.ignore();

Hope this helps smile.gif

I tried the
CODE
getline(cin, message, '.');
   cin.ignore();

but it still will not allow me to enter a character to search. The program complies with no errors and 2 warnings that point to this line;
CODE
int num_commas = count(message.begin(), message.end(), userInput);  //num_commas holds the number of elements in text

but i do not think this is the problem
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 21 Aug, 2008 - 03:13 AM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,440



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


You declare userInput as an int. Try declaring it as a char. smile.gif
User is online!Profile CardPM

Go to the top of the page

zerogee
post 21 Aug, 2008 - 03:23 AM
Post #7


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 45

QUOTE(gabehabe @ 21 Aug, 2008 - 04:13 AM) *

You declare userInput as an int. Try declaring it as a char. smile.gif

That worked, Thanks!! smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:08AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month