[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]

/prog/ - Programming

Programming board

Catalog

8chan Bitcoin address: 1NpQaXqmCBji6gfX8UgaQEmEstvVY7U32C
The next generation of Infinity is here (discussion) (contribute)
A message from @CodeMonkeyZ, 2ch lead developer: "How Hiroyuki Nishimura will sell 4chan data"
Name
Email
Subject
Comment *
File
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Embed
(replaces files and can be used instead)
Options
Password (For file and post deletion.)

Allowed file types:jpg, jpeg, gif, png, webm, mp4, pdf
Max filesize is 8 MB.
Max image dimensions are 10000 x 10000.
You may upload 1 per post.


File: 1440706343477.jpg (7.09 KB, 294x111, 98:37, render2.jpg)

ddf398 No.3137

What is the role of cin.get(); in the following code, I believe that it satisfies the requirement that the program wait for user input however the c++ refrence description of get() is confusing me


#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main () {
ifstream file ("inputfile.txt")
string line;
while(getline(file,line))
{
cout << line + "\n";
cin.get();
}
}

91d539 No.3138

>>3137

1. compile code

2. check behaviour

3. remove cin.get() and compile again

4. check behaviour again

I'll leave it to you to find out what you will accomplish by following these steps. A clue: it's related to your question,


91d539 No.3139

>>3137

You're missing a semicolon in there for fuck's sake.

Also. This was literally one google away:

>When using cin, you type in characters (keys on your keyboard) that appear in the command prompt. >That's what your program - specifically, cin 0 reads. Normally, it will automatically take as many characters as it needs to input. For instance, if you say

>cin >> myIntVar;

>and you type 37643, it gets all 5 characters, treats them as an integer, and correctly puts 37643 into myIntVar.

>By using cin.get(), you get only one of those characters, and it is treated as a char.

>Sometimes, the command prompt will close as soon as the program finishes, meaning you can't see the output. Putting cin.get() forces the program to wait for the user to enter a key before it can close, and you can see the output of your program.

Get your shit together if you ever want to amount to something in your life, anon.




[Return][Go to top][Catalog][Post a Reply]
Delete Post [ ]
[]
[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]