Click here to Skip to main content
15,936,903 members
Please Sign up or sign in to vote.
2.50/5 (4 votes)
See more:
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
int main () 
{
  string myString = ""; 
	cout<<"enter the sentence"<<endl;
	getline(cin,myString);


myString.insert(10, 1, '\n'); 
myString.insert(20, 1, '\n');
myString.insert(30, 1, '\n');
myString.insert(40, 1, '\n');
myString.insert(50, 1, '\n');
myString.insert(60, 1, '\n');
cout << myString << endl;


    return 0;
}</string></iostream>


or

using namespace std;
int main () 
{
  string myString = ""; 
	cout<<"enter the sentence"<<endl;

	getline(cin,myString);
	 
    	cout.width(10);
        cout <<myString << "\n";


it does not have any error but it does not work
Posted
Updated 3-Apr-11 6:30am
v5
Comments
Yanick Salzmann 3-Apr-11 10:25am    
Hi yay hello,

Just for clarification:
Your program should take a sentence from the user and then display it but split it up so that always 10 characters are printed in one line? Or did I misunderstand your question?

Greetings
Yanick
shakil0304003 3-Apr-11 10:26am    
really unclear, what you want!!!
Alan N 3-Apr-11 10:31am    
This is question is impossible to answer as no one will be able to tell what your requirements are. From the example I would guess that you want to randomly insert or delete spaces and output between 7 and 12 characters per line!
Alan N 3-Apr-11 10:50am    
Sorry that is not clearer. Your input was "hello How are you I am so happy" and in the output sometimes you keep the spaces between words and sometimes you remove them. Look at your second line "hello iamha". You have also converted to lower case which you never mentioned before.

If you are able to state your requirements PRECISELY then designing the programme will be easy.
Alan N 3-Apr-11 11:38am    
OK that's better but still not good enough.

You want 10 non-space characters per line.

How do you decide what the actual length of the line will be?

In the example you have 12 characters per line including the spaces.

In the second line how did you choose which spaces to remove?

In the third line how did you decide where to add spaces?

The biggest problem for any develeoper (this don't depend on the language you use) is to articulate what the program should do. If you can do that the program is as good as ready. Do you want help - please tell somebody what you're willing to do.
Regards.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 1:02am    
Good point! My 5.
--SA
Using cin is probably not your best option for this, since it breaks on a space character, see this article (tells you problems with cin and shows you how to use getline instead):
http://www.cplusplus.com/forum/articles/6046/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900