473,473 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Getting input the rightway?

10 New Member
I've recently started working with C++ and wondered if there was a "right" way for getting input from the console window. From what I've seen whenever I attempted to implement it more than once, cin>>string seems to fail on items of the "string" type. The use of getch()/scanf() isn't really satisfactory because getch() requires lots of garbage add ons to recognize the end of the string, and backspacing, while scanf() seems to run into the same problem as cin>>string. I've tried dozens of different methods for handling input from the console window, and most of them seem to have seemingly random errors if implemented more than once in the program. If anyone could provide suggestions or examples for getting input for the "string" type I would appreciate it.
Mar 26 '08 #1
9 1636
Banfa
9,065 Recognized Expert Moderator Expert
Look up getline in you C++ reference book
Mar 26 '08 #2
xlsmnt
10 New Member
Look up getline in you C++ reference book
It doesn't work correctly after multiple implementations, if I use, getline(cin, string); more than three times or so it begins to spit out random errors, and if I use it in a function that is implemented several times it spits out gibberish.
Mar 26 '08 #3
Laharl
849 Recognized Expert Contributor
You probably need to flush the input buffer between calls to getline with cin.flush().
Mar 26 '08 #4
Banfa
9,065 Recognized Expert Moderator Expert
I do not think flush is a member of basic_istream (type of cin) (at least not in standard C++) it is a member of basic_ostream.

This makes sense all mirrors the badness of calling fflush on an input stream in C (it's bad don't do it).

I think the cin.ignore may be the function to use.
Mar 26 '08 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
And don't use scanf() in C++. That's for C.
Mar 26 '08 #6
xlsmnt
10 New Member
I do not think flush is a member of basic_istream (type of cin) (at least not in standard C++) it is a member of basic_ostream.

This makes sense all mirrors the badness of calling fflush on an input stream in C (it's bad don't do it).

I think the cin.ignore may be the function to use.
cin.ignore() causes the function to eat the first character of every string after the first string, and I have no idea why. The upside is it is now providing me with strings instead of random ascii symbols.
Mar 26 '08 #7
xlsmnt
10 New Member
cin.ignore() causes the function to eat the first character of every string after the first string, and I have no idea why. The upside is it is now providing me with strings instead of random ascii symbols.
I found my own answer, the problem wasn't in getline, but in the variable I was referencing, for some reason my compiler didn't catch that I had a declared pointer "mystring" and a declared string "mystring" in the same function. So I wrote the getstring() function separately as
Expand|Select|Wrap|Line Numbers
  1. string getstring()
  2. {
  3. string input;
  4. getline(cin, input);
  5. return input;
  6. }
  7.  
This seems to actually work fine without any use of cin.clear() or cin.ignore()
Mar 26 '08 #8
Ganon11
3,652 Recognized Expert Specialist
Be careful; using getline like that, the program will look for any and all text from the start of the stream to the newline character ('\n, it's made when you press <Enter> or <Return>). It grabs that text, removes it from the stream, and returns it, but it leaves the '\n' still in the stream. This might mess you up if you call cin.get() or getline again after the original getline call. I ran into this problem a lot when I was learning C++.
Mar 26 '08 #9
xlsmnt
10 New Member
Be careful; using getline like that, the program will look for any and all text from the start of the stream to the newline character ('\n, it's made when you press <Enter> or <Return>). It grabs that text, removes it from the stream, and returns it, but it leaves the '\n' still in the stream. This might mess you up if you call cin.get() or getline again after the original getline call. I ran into this problem a lot when I was learning C++.
Well for some reason I'm not running into it now, does the system I'm compiling/running on effect that somehow?

Also I was wondering whether this
Expand|Select|Wrap|Line Numbers
  1. void getstring(string& ptr)
  2. {
  3. getline(cin, ptr);
  4. }
  5. int main()
  6. {
  7. string * ptr;
  8. string mystring;
  9. ptr = &mystring;
  10. getstring(*ptr);
  11. }
  12.  
was the same as this
Expand|Select|Wrap|Line Numbers
  1.  
  2. string getstring()
  3. {
  4. input string;
  5. getline(cin, string);
  6. return string;
  7. }
  8. int main()
  9. {
  10. string mystring;
  11. mystring = getstring();
  12. }
Mar 27 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Willoughby Bridge | last post by:
Hi - Having trouble getting a multipart series of forms to move to the next form. The problem line is: <form method="post" enctype="multipart/form-data" action="Data_Form1.php"> If the...
11
by: Ray Muforosky | last post by:
I have this: ------------ print "<FORM name=\"form3\" ACTION=\"cmdlog_rep.php\">\n"; print "<TD><INPUT TYPE=\"submit\" VALUE=\"Submit\"></TD>\n"; .. print "<INPUT type=\"HIDDEN\"...
4
by: gimme_this_gimme_that | last post by:
Hi, This is sort of a : How to build a Yes/No dialog box qquestion. Or perhaps a question about getting javascript variables from a pop-up window and processing them on a submit. This is...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
2
by: pm1ccc | last post by:
Hi, I have written following code and not getting back values in the same form ( i have used phpself) <?php $Fname = $_POST; $Lname = $_POST; $gender = $_POST; $food = $_POST; $quote =...
1
by: kallol | last post by:
Hi Everyone, I have a form designed with a table and the table is inside a <div> (Required. Can’t get rid of it). The div has style “height: 100% and scrollable: auto”. The issue: In IE, let...
1
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as...
9
vikas251074
by: vikas251074 | last post by:
I am not getting date value in spite of my good effort. This code was working in my last office where I work. Now I am trying to work at my home pc. but not getting date value. Any can help me why...
7
vikas251074
by: vikas251074 | last post by:
I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am...
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,...
1
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
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...

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.