473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

about "getline"

#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
6 2363
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
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
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
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: aGAric | last post by:
i use std::getling to read a line in unicoude file to buffer,like: WCHAR buf wifstream in; std::getling(buf,in,100); but i can't get the right result,it seems can only read by 1 byte;e.g first...
4
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h \\ where I assume getline() is inner. #include <stdlib.h> int main() { int max=15; char line; getline(line,max);...
3
by: caoliangbj | last post by:
Hi, When I capture the SIGINT event, I encounter a probelm. Please look at the following code, if you don't press any key, the 'getline()' will block there all the time. When I press...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.