Questions tagged [cin]

std::cin is the global stream object provided by the C++ standard library for reading from the standard input stream.

For more general questions about std::istream use the or tags.

1859 questions
11
votes
6 answers

Problem of using cin twice

Here is the code: string str; cin>>str; cout<<"first input:"<
gc .
  • 415
  • 4
  • 9
  • 18
11
votes
6 answers

Using cin in QtCreator

For school, we use C++ as the language of choice. I am currently using QtCreator as an IDE, and for its GUI library, it is wonderful. The school is using Visual Studio. However, most of the programs we are writing make use of cin and cout for…
Austin Hyde
  • 26,347
  • 28
  • 96
  • 129
10
votes
3 answers

C++ - pointer being freed was not allocated error

malloc: *** error for object 0x10ee008c0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Abort trap: 6 Or I get this when I try and print everything Segmentation fault: 11 I'm doing some homework for an…
jordaninternets
  • 233
  • 2
  • 6
  • 15
10
votes
3 answers

Trying to push an unknown number of strings into a vector, but my cin loop doesn't terminate

I am trying to take strings as input from cin, and then push the string into a vector each time. However, my loop doesn't terminate even when I put a '\' at the end of all my input. int main(void) { string row; vector log; while…
cluelessguy
  • 101
  • 3
10
votes
2 answers

Does "cin" reset variable to some default value if input type differs from destination type?

I have an issue with behavior of "cin" (I do not understand). My IDE is Netbeans under Windows OS (with Cygwin). Here's a code example: int main() { int temp = -1; std::cin >> temp; // here user enters string of characters (string) or a…
RickSanch3z
  • 129
  • 7
10
votes
3 answers

std::cin skips white spaces

So I am trying to write a function to check whether a word is in a sentence, by looping through a char array and checking for the same string of char's. The program works as long as the Sentence doesn't have any spaces. I googled around and they are…
Pejman Poh
  • 493
  • 2
  • 8
  • 20
10
votes
2 answers

Pipe an input to C++ cin from Bash

I'm trying to write a simple Bash script to compile my C++ code, in this case it's a very simple program that just reads input into a vector and then prints the content of the vector. C++ code: #include #include
Tyler
  • 509
  • 4
  • 11
  • 23
10
votes
4 answers

How Can I avoid char input for an int variable?

The program below shows a 'int' value being entered and being output at the same time. However, when I entered a character, it goes into an infinite loop displaying the previous 'int' value entered. How can I avoid a character being…
Scoop
  • 121
  • 1
  • 2
  • 7
9
votes
2 answers

Why doesn't std::noskipws work, or what is it supposed to do?

First off my understanding is that cin >> std::noskipws >> str; should stick a whole line from cin like "i have spaces" into str. However this only puts "i" into str. This could be a false assumption in which case what does std::noskipws do? I…
Digital Powers
  • 460
  • 6
  • 23
9
votes
1 answer

Basic String input

I've just came across this bit of code that allows users to input strings in the command prompt. I'm aware of what they do and it's all great. But I have a question in regards to the cin and getline() functions. string name ; cout << "Please enter…
user2779581
  • 169
  • 1
  • 1
  • 8
9
votes
3 answers

What does cin do when there is an error

#include; int main() { int a = 1; int b = 2; std::cin >> a >> b; std::cout << a << "+" << b << "=" << a+b << std::endl; return 0; } when I enter 3 4 as input,the output will be 3+4=7,well,it's strange; But when I…
user1668903
  • 407
  • 5
  • 11
8
votes
3 answers

std::getline on std::cin

Is there any good reason why: std::string input; std::getline(std::cin, input); the getline call won't wait for user input? Is the state of cin messed up somehow?
ForeverLearning
  • 6,352
  • 3
  • 27
  • 33
8
votes
1 answer

C++: using cin on a child process

I'm writing a C++ program that forks early on and where I use std::cout and std::cin in both the child and parent processes. For some reason, on Linux, cin doesn't seem to be working in the child process; it never prompts for for any input. The…
metalhead696
  • 195
  • 2
  • 11
8
votes
1 answer

How to properly use cin.peek()

This function is supposed to read a fraction and place it in an array. If the user enters '0' the function is supposed to exit. I am trying to do this using the cin.peek() function but execution always goes into the if statement and doesn't allow…
Zzz
  • 2,927
  • 5
  • 36
  • 58
8
votes
2 answers

C++ how does While( cin >> x) work?

My question is how does, while(cin>>x) { //code } work. Or to be more specific, what about that code halts the loop? From the documentation here, it looks like the >> operator returns a &istream. Does that mean if the read fails or hits the…
Dan
  • 2,625
  • 7
  • 39
  • 52