B
bryant058
#include<iostream>
#include<cstring>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
string s;
char *tokenptr;
int space;
cout<<"-Please enter the spacing:";
cin>>space;
cout<<"-Please enter a string of length<70:"<<endl;
getline(cin,s);
while(s.length()>9)
{
cout<<"The input string is too long. Please reenter a string
of length < 70:"<<endl;
getline(cin,s);
}
char str[s.length()];
for(int i=0;i<=s.length();i++)
{
str=s;
}
tokenptr=strtok(str," ");
while(tokenptr!=NULL)
{
cout<<tokenptr;
for(int i=0;i<space;i++)
cout<<" ";
tokenptr=strtok(NULL," ");
}
system("pause");
return 0;
}
------------------------------------------------------------------------------
In the program above, first, i use "cin" to assin a number to the
variable 'space'.Then i wanna use "getline" to save a string.But when
i execute the program,after typing the integer 2(the space),the
program cout
"-Please enter a string of length<70:" and stop. i can't continue
typing the string!! why and how can i change the code to reach my
purpose??
#include<cstring>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
string s;
char *tokenptr;
int space;
cout<<"-Please enter the spacing:";
cin>>space;
cout<<"-Please enter a string of length<70:"<<endl;
getline(cin,s);
while(s.length()>9)
{
cout<<"The input string is too long. Please reenter a string
of length < 70:"<<endl;
getline(cin,s);
}
char str[s.length()];
for(int i=0;i<=s.length();i++)
{
str=s;
}
tokenptr=strtok(str," ");
while(tokenptr!=NULL)
{
cout<<tokenptr;
for(int i=0;i<space;i++)
cout<<" ";
tokenptr=strtok(NULL," ");
}
system("pause");
return 0;
}
------------------------------------------------------------------------------
In the program above, first, i use "cin" to assin a number to the
variable 'space'.Then i wanna use "getline" to save a string.But when
i execute the program,after typing the integer 2(the space),the
program cout
"-Please enter a string of length<70:" and stop. i can't continue
typing the string!! why and how can i change the code to reach my
purpose??