OK,so I am working on this code in which I have to enter a string of length 20.After which I will enter a value of variable n resulting into output string which will start from index n upto white space or end of line.
For ex:-if I enter string as John Smith and n=0 then it should output string as John and if n=2 then ohn.
I tried to code this program in C/C++ but vain.Segmentati on faults is the only result that I am getting.
Here is the code that I am working on.
I am also confused between cin and cin.getline functions.Sugge stions,hints are welcome.
Thanks in advance.
Cheers.
For ex:-if I enter string as John Smith and n=0 then it should output string as John and if n=2 then ohn.
I tried to code this program in C/C++ but vain.Segmentati on faults is the only result that I am getting.
Here is the code that I am working on.
Code:
#include <stdlib.h>
#include<string>
#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
char a[20];
int i,n;
cout<<"Enter the desired string";
cin.getline(a,20);
cout<<"Enter string number\n";
cin>>n;
if(n==0)
{
cout.write(a,20);
}
else
{
cout<<"The required string is \n";
i=n;
while(a[i]!=' '||a[i]!='\0')
{
cout<<a[i];
i++;
}
}
return 0;
}
I am also confused between cin and cin.getline functions.Sugge stions,hints are welcome.
Thanks in advance.
Cheers.
Comment