By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,411 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,411 IT Pros & Developers. It's quick & easy.

about "getline"

P: n/a
#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[i]=s[i];
}
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??

Apr 2 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
br*******@gmail.com wrote:
#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[i]=s[i];
}
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 2 '07 #2

P: n/a
br*******@gmail.com wrote:
#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
Apr 2 '07 #3

P: n/a
br*******@gmail.com wrote:
[...]
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?
Apr 2 '07 #4

P: n/a
Ralph D. Ungermann wrote:
br*******@gmail.com wrote:
[...]
>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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 2 '07 #5

P: n/a
<br*******@gmail.comwrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...
#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[i]=s[i];
}
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.
Apr 2 '07 #6

P: n/a
br*******@gmail.com wrote:
#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[i]=s[i];
}
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....8a55c66c298b3/

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 3 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.