#cin vs cin.get()
26 messages · Page 1 of 1 (latest)
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.
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
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
https://en.cppreference.com/w/cpp/io/basic_istream/get here is the reference if you want to read in detail
so how come when I type: a b, in the console. It outputs just a. If it ignores whitespaces why isn't it outpuing ab or a b.
cpp```
#include <iostream>
using namespace std;
int main() {
string name{ "" };
cin >> name;
cout << name;
}
@wanton obsidian
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
```cpp
int main() {}
```
int main() {}
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
cin gets the input up untill first whitespace
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;
}
did you read how to use it ?
you cant just randomly throw some code together and get confused when it doesnt work
read how to use it
cin reads from "standard input"
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
if your goal is to just get strings line by line , there is also getline
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;
123
a
b
c
what is ss
i dont understand what the ss>>x; line does
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 ☠️
TL;DR same way std::cin does. They are both input streams :)
you can use std::getline() it dosent skip whitespaces
yep , i realized that xd
btw they made a new thread here #1435543653190864928 message
if you had time , you can look over it and give me feedback if my understanding of what i said is correct or not