#cin vs cin.get()

26 messages · Page 1 of 1 (latest)

haughty condorBOT
#

Please don't cross-post the same message across multiple channels (#cpp-help-text, #c-cpp-discussion, #1435499212388434021). Pick the most appropriate channel for your question and ask there.

#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

haughty condorBOT
# haughty condor

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

forest walrus
#

std::cin ignores any kind of whitespace and delimit characters when taking input

so lets say you have a code like


#include<iostream>

int main(){

  char inp1, inp2;
  std::cin >> inp1;
  std::cin >> inp2;
  
  std::cout << inp1 << " " << inp2 ;

  return 0;
}

and you enter in a string a b , you would expect inp1 to get a and inp2 to get (whitespace)

but that doesnt happen , inp1 does get the a but inp2 gets b because the std::cin ignores whitespace

#

but if you use std::cin.get() instead , you will get whitespace in inp2

wanton obsidian
haughty condorBOT
#

@wanton obsidian

It looks like you may have code formatting errors in your message

Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks

Markup

```cpp
int main() {}
```

Result
int main() {}
unique prism
#

std::cin by itself is just an instance of* a std::istream (short of input stream), used from reading data from a stream of characters.

There are many overloads of the >> operator which are used to read specific types of data. Like
operator>>(std::istream&, int&) for reading ints from an input stream.

The istream class also has its own methods, of which istream::get returns the next character in the stream

forest walrus
wanton obsidian
#

I think i just need to do more research on how all it works like input stream and characters and stuff. I'm just jumping the gun to quick

#

@unique prism @forest walrus
Dang it doesnt like this? I get a red error line under the word get.
cpp```
#include <iostream>
using namespace std;
int main() {

string name{ "" };
cin.get(name);
cout << name;

}

forest walrus
#

you cant just randomly throw some code together and get confused when it doesnt work

read how to use it

unique prism
#

cin reads from "standard input"

wanton obsidian
#

thats my plan to read how to use it. I just figured i'd throw a hail mary and try that code to see if it works lol

forest walrus
#

if your goal is to just get strings line by line , there is also getline

unique prism
#

So there are different types of istreams. fstreams reads data from files. stringstream reads data from a string. And std::cin reads from the standard input.

Whereever a given istream reads from, .get() returns the next byte in there

#

;compile

std::stringstream ss("123abc");
int x;

// This reads the number 123
ss >> x;
std::cout << x << std::endl;

// These 3 gets read the next characters 3 characters
std::cout << (char) ss.get() << std::endl;
std::cout << (char) ss.get() << std::endl;
std::cout << (char) ss.get() << std::endl;
small oasisBOT
#
Program Output
123
a
b
c
forest walrus
#

like , how can a string stream , insert its characters into an int (and even know how to stop at 3 , like how does it a know 123 is an int) ?

#

nvm , im dumb
thats just how the >> operator works ☠️

unique prism
fervent tartan
#

you can use std::getline() it dosent skip whitespaces

forest walrus