473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ 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 1639
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
1798
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 "action" parameter is set to the current form (Data_Form1.php) then the data posts to the table correctly, but it does not move to the next form If the...
11
1909
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\" name=\"year1\" >\n"; print "<INPUT type=\"TEXT\" name=\"when\" >\n"; print "</FORM>\n";
4
4923
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 what I'd like to have happen :
3
3677
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 first part) needs to get input from the user, and I want to do this with cin.getline also. The problem I am getting, is when I run the program, the...
2
2102
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 = $_POST;
1
3040
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 say, the form is bit longer and overflowed the page so the div has got vertical scroll bar. Now I am inserting data in the form. Inserted the 1st...
1
3176
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 follows: The code below is for the first page:session_start is in line 3 <link href="css/jobSheet.css" rel="stylesheet" type="text/css" /> ...
9
3531
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 this happens. This is my part of code given below. <%@ Language=VBScript%> <%Option Explicit%> <html> <head> <title>SABF</title>...
7
4429
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 surrendered to you. <%@ Language=VBScript%> <%Option Explicit%>
0
7410
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
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...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
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...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
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
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
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...

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.