473,657 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about "getline"

#include<iostre am>
#include<cstrin g>
#include<string >
#include<iomani p>
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:"<<en dl;
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 2377
br*******@gmail .com wrote:
#include<iostre am>
#include<cstrin g>
#include<string >
#include<iomani p>
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:"<<en dl;
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<iostre am>
#include<cstrin g>
#include<string >
#include<iomani p>
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:"<<en dl;
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*******@gmai l.comwrote in message
news:11******** **************@ y66g2000hsf.goo glegroups.com.. .
#include<iostre am>
#include<cstrin g>
#include<string >
#include<iomani p>
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:"<<en dl;
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<iostre am>
#include<cstrin g>
#include<string >
#include<iomani p>
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:"<<en dl;
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
1970
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 word of unicode file is "0xfffe" ,if use getline ,it may 0x00ff 0x00fe in my buffer do you have any idea,except use c lib like fgetws
4
2795
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); system("PAUSE");
3
4933
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 'ctrl+c', the function 'getline()' still block there. My question is how can I exit from "getline()" while I just press 'ctrl
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6167
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.