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.
9 1639 Banfa 9,065
Recognized Expert Moderator Expert
Look up getline in you C++ reference book
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.
Laharl 849
Recognized Expert Contributor
You probably need to flush the input buffer between calls to getline with cin.flush().
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.
And don't use scanf() in C++. That's for C.
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.
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 - string getstring()
-
{
-
string input;
-
getline(cin, input);
-
return input;
-
}
-
This seems to actually work fine without any use of cin.clear() or cin.ignore()
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++.
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 -
void getstring(string& ptr)
-
{
-
getline(cin, ptr);
-
}
-
int main()
-
{
-
string * ptr;
-
string mystring;
-
ptr = &mystring;
-
getstring(*ptr);
-
}
-
was the same as this -
-
string getstring()
-
{
-
input string;
-
getline(cin, string);
-
return string;
-
}
-
int main()
-
{
-
string mystring;
-
mystring = getstring();
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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";
|
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 :
|
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...
|
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;
| |
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...
|
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" />
...
|
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>...
|
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%>
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |