Strings..... C++

Hey i just start to play with some strings and i have a little trouble to write something....

I want to write a that allows to input 10 lines of text... I dont know how do you tell the program that after 10 lines of text, you cant wirte anymore.....

Im using c++ language
How about something like this:


1
2
3
4
5
std::string lines[10];
for (int i=0; i<10; ++i)
{
  std::cin >> lines[i];
}
closed account (zb0S216C)
My version of Moschops's code:
1
2
3
4
5
6
std::string Lines[ 10 ];
for( int At_line( 0 ); At_line < 10; At_line++ )
{
    std::cout << "Line " << At_line << ": ";
    getline( std::cin, Lines[ At_line ] );    // Allow white-space in our strings.
}


+1 to Moschops for the code in his post.
Last edited on
Ok, this string make more sense now :D. But can someone pls explain the difference between Moschops and Frameworks code.... I know that the first code doesnt allow to do white-spaces, the second one does. I dont completely understand how is that achived in second code. (Sry for bad english..)


Ty for further explenation =)

P.S

I have modfied Frameworks code like this:

1
2
3
4
5
6
    for (int i=0; i<10; i++)
    {
        cout<<"Line "<<i+1<<": ";
        getline(cin, lines[i]);
        cout<<endl;
    }


Is this the same ?
Last edited on
closed account (zb0S216C)
Basically, getline( ) catches all white spaces until the enter or return key is pressed.

Your version of the code, Sibuns, is the same. The only difference I see is that the names are shorter( but less descriptive ) and you've replaced << At_line << with i+1. Other than that, it's basically the same.
Last edited on
Ok, now i have another problem...

I would like to count words in this whole string, but i dont know how to write the part of the code when you tell that whole text you wrote is in new string...exp lines[i]= string whole_text. Am i even thinking in the right way? I have imagine that i would do this like this. First i store the whole text in one string. Than i would get the length of whole string and do:

1
2
3
4
5
6
for(int i=0; i<whole_text.length; i++)
          {
               if(lines[i] == ' ' ||lines[i] == '.' || lines[i] == ', ' || lines[i] == '!' || lines[i] == '?')
               num_words++;
           }
               


And i have the same problem when i want to count number of letters in the text.

Can someone pls help me:P

ty for your help
Your algorithm for counting words is sensible. You just need to consider the cases where someone might put multiple punctuation marks/spaces in a row.

Counting letters is easier; you can use the isalpha() function for that.
Ok i was playing around and i wonder if i can do it like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    void sentence(string lines)
   {
       int words = 1;   
       getline(cin, lines);
   
       int length = lines.length(); 
   
       for (int i = 0; length >i; i++)
       {
           if(lines[i] == ' ' ||lines[i] == '.' || lines[i] == ', ' || lines[i] == '!' || lines[i] == '?')
  
                words++;  
       }
        
        cout<<words;
   }
   


I wrote instead of void sentece(string lines[10]), void sentence(string lines)

Is this and the code i posted ok now? I just have some issues about the code working correctly
It looks like it would work, though you still need to consider the issue I mentioned above. And you may want to start "words" at 0, as it's possible you could have a string with only spaces/punctuation, which ought to have 0 words in it.
yea i will work with that later.. :D I need to get this code to work now.... :D.... It doesnt count my words correctly :D.. Any idea why.... It always return 1... dunno why...
closed account (zb0S216C)
I would like to count words in this whole string, but i dont know how to write the part of the code

You could try using strtok( ). This splits the given string into tokens. See here: http://www.cplusplus.com/reference/clibrary/cstring/strtok/

Of course, you don't have to modify the string just count the amount of tokens generated.
Last edited on
Ok i dont know whats wrong with this code... I have a function that user can input 10 lines...

1
2
3
4
5
6
7
8
9
void lines(string lines)
{
   for (int i=0; i<10; i++)
   {
      cout<<"Line "<<i+1<<": ";
      getline(cin, lines);
      cout<<endl;
   }
}


Than i have function to count the words

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
voidwords (string lines)
{
   int words = 0;   
   getline(cin, lines);
   
   int length = lines.length(); 
   
   for (int i = 0;i<length; i++)
   {
   if(lines[i] == ' ' ||lines[i] == '.' || lines[i] == ', ' || lines[i] == '!' || lines[i] == '?')

        words++;  
        }
        
        cout<<words;
}
   


And it doest work correct....

The function with input lines works good but when i use words function both functions stops working correctly...

-------------------------

Does this not work because i use getline(cin, lines); again in word count function?... Is there any other way that i can collect the whole text from all lines i input?

Last edited on
Topic archived. No new replies allowed.