-2

this is my current code , i have to print every world of my name on a new line

include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main ()
{
    string a;
    char j[100];
    int i, c, b; 
    cout <<"enter your full  name ";
        getline(cin,a);
        cout << " ur name is " << a << endl;
c=a.size(); 
for (b=0; b<=c; b++)
{
j[b]=a[b];
j[b]='\0';
}
system ("pause");
return 0; 
}

how can i print every part of my name on a new line ? ex : input : geroge ashley mark . output : george (newline) ashley (newline) mark

1 Answers1

0

This is a little convoluted way to do this, I prefer the method shown in the comments. However, if you want to avoid stringstreams, here is another way to achieve what you are looking for. It will also support comma separated names.

#include "stdafx.h"

#include "iostream"
#include "string"
using namespace std;
int main()
{
    string a;
    char j[100];
    int i, c, b;
    cout << "enter your full  name ";
    getline(cin, a);
    cout << " ur name is " << a << endl;
    c = a.size();

    bool space = false;

    for (auto iter = a.begin(); iter != a.end(); iter++)
    {
        if (isalpha(*iter) == false)
        {
            if (space == false)
            {
                cout << std::endl;
                space = true;
            }
        }
        else
        {
            cout << (*iter);
            space = false;
        }
    }
    system("pause");
    return 0;
}
user2970916
  • 1,146
  • 3
  • 15
  • 37