Hi people I'm in urgency, I got an exam on monday with C++ and I might get a thing like
there is a sentence like "Jack went fishing on monday", and I need to count the number of
"m","e","a"s etc in it, I suspect you got to type a string with the sentence, then to insert it into a char array, and then do a linear search for the letters, but I don't know how to type a string WITH spaces in a way that the compiler doesn't forget that he needs to take every single word, not just the first, cuz when I type a string type element only the first element seems to considered by the compiler.
Second I've used the string.copy(chararray,20); thing and it works only for words not for a whole sentence with spaces, how do I do that ?? please it's very important for me thank you a lot.

PS:I believe you can put the words in a file then get the file with getline, but can't this whole file procedure be avoided ?

You don't need to use an ifstream to use getline. You can use getline with cin and thus read in more than one word. The program below asks the user to type in two sentences, then displays both. Unlike the >> operator, getline doesn't stop at the first space, so you can type entire sentences and read them in through getline, just like you were reading from a file.

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

int main ()
{
    string sentence1, sentence2;
    cout << "Please enter sentence 1: ";
    getline (cin, sentence1);
    cout << "Please enter sentence 2: ";
    getline (cin, sentence2);    
    cout << "Sentence 1: " << sentence1 << endl;
    cout << "Sentence 2: " << sentence2 << endl;    
    return 0;
}

Thank you VernonDozier, it works well, but now I got another problem imagine I got to write x different sentences and I got to analyse every single one of them, I've tried to store the getline into a string array
like
loop
getline(cin,stringarray)>>stringarray2;
again the cells of the stringarray2 "absorb" only the first word, what the hell is going on !!
Please how could I force the program to store somewhere x phrases, do I have to use dynamic alocation or what, please urgent, thank you.

I've figured out a way of doing it this is my code what do you guys think ?
Is it acceptable or far too clumsy, or very bad, good ?
Please it's important, thanks a lot.

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

void main(){
                  int N=0;
			
	string arraystring;
	string *arraystrings2;
	char chararray[30];
                cout<<"How many sentences ?"<<endl;
	cin >>N;
                 arraystrings2=new string[N];
			
	memset( chararray,'\0',30);
			
	for(int i=0;i<N;i++){
		              cout<<"type sentence :"<<i+1;
		              getline(cin,arraystring);
		              arraystring.copy(arraytableau,30);
		              arraystrings2[i]=chartableau;
                                              memset( chartableau,'\0',30);
                                              }
							 
			for(int i=0;i<N;i++){
							                                                               cout<<arraystrings2[i]<<endl
                                                                              }
                        }

I've figured out a way of doing it this is my code what do you guys think ?
Is it acceptable or far too clumsy, or very bad, good ?
Please it's important, thanks a lot.

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

void main(){
                  int N=0;
			
	string arraystring;
	string *arraystrings2;
	char chararray[30];
                cout<<"How many sentences ?"<<endl;
	cin >>N;
                 arraystrings2=new string[N];
			
	memset( chararray,'\0',30);
			
	for(int i=0;i<N;i++){
		              cout<<"type sentence :"<<i+1;
		              getline(cin,arraystring);
		              arraystring.copy(arraytableau,30);
		              arraystrings2[i]=chartableau;
                                              memset( chartableau,'\0',30);
                                              }
							 
			for(int i=0;i<N;i++){
							                                                               cout<<arraystrings2[i]<<endl
                                                                              }
                        }

I'm not sure if I understand completely, but is the whole task to get N different strings, each inputted by the user, and store them? If so, I think you are doing too much. I don't see the need for the "memset" code, I don't see the need for chararray variable, and I don't see the need for the arraystring variable or the "copy" code. Also, what is chartableau and arraytableau? If all you need is to have the user enter in N phrases and store them in an array of N strings, it seems to me this would work:

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

int main()
{
    int N=0;
    string *arraystrings2;
    cout<<"How many sentences ?"<<endl;
    cin >>N;
    arraystrings2=new string[N];

    for(int i=0;i<N;i++)
    {
        cout<<"type sentence " << i+1 << ": ";
        getline(cin,arraystring2[i]);
    }
							 
    for(int i=0;i<N;i++)
    {
       cout <<arraystrings2[i]<<endl
    }

    return 0;
}

Please note I have changed void main() to int main() and added return 0; at the end.

On a side note, the reason the spacing didn't line up in your post is because you have both tabs and spaces in the code. A tab is treated as eight spaces when you post here, so if it isn't eight spaces per tab in your IDE, it won't line up. You can either use tabs only or spaces only, or many IDEs have a format option where you can change tabs to spaces. if you use that option, it'll line up better.

Not sure if this is all you were looking for, but if it is, you were doing too much work in my opinion.

This article has been dead for over six months. Start a new discussion instead.