[ / / / / / / / / / ] [ dir / ask / dcaco / fur / hypno / o / sl / ttgg / u ][Options][ watchlist ]

/prog/ - Programming

Programming board

Catalog

You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.
Name
Email
Subject
Comment *
File
Select/drop/paste files here
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Expand all images

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

[–]

279851 (1) No.3137>>3138 >>3139 [Watch Thread][Show All Posts]

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();
}
}

1e87a0 (2) No.3138

>>3137 (OP)

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,


1e87a0 (2) No.3139

>>3137 (OP)

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][Screencap][Update] ( Scroll to new posts) ( Auto) 3
2 replies | 0 images | 2 UIDs | Page ???
[Post a Reply]
[ / / / / / / / / / ] [ dir / ask / dcaco / fur / hypno / o / sl / ttgg / u ][ watchlist ]