about "getline"

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??
 
V

Victor Bazarov

#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.


What do you mean "stop"? Exit? Finishes execution?
i can't continue
typing the string!! why and how can i change the code to reach my
purpose??

You might want to use 'cin.ignore' after the 'cin>>space;'. The cin
buffer still contains the \n you used to push the number in, and the
'getline' hangs onto it, so the string you get is empty, most likely.

V
 
D

Default User

#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;

You tell the user to enter a string of less than 70, then complain if
it's longer than 9? What's your logic there?




Brian
 
R

Ralph D. Ungermann

int main()
{
string s; [...]
char str[s.length()];

AFAIK, the above code won't compile.
Has this been added to the standard?
Or, at least, is it a common extension?
 
V

Victor Bazarov

Ralph said:
int main()
{
string s; [...]
char str[s.length()];

AFAIK, the above code won't compile.
Has this been added to the standard?
Nope.

Or, at least, is it a common extension?

Not sure how common it is. GNU C++ has it. Borland used to
(don't know if it still does).

V
 
J

Jim Langston

#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??


Try 2 then pressing enter and see if it works.

cin.ignore should be used however.
 
M

Marcus Kwok

#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??


This was answered the first time you posted it.
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/34a8a55c66c298b3/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top