Cin with int type stalls

closed account (N8RzwA7f)
Hi,
for some reason the most basic code is stalling .
I recently installed netbeans. I'm on my 3rd program.

I tried using this code:
1
2
3
4
5
6
  int aInt;
  cout << "enter a number:";
  cin >> aInt;
  cout << "you entered:" << aInt;

  


it works fine if I try it with a char type instead?
thanks for any help :)
It waits for you to input the integer. Don't forget to press enter.
Last edited on
The reason it does not work with a char is because the char is used to read characters. Take a look at line 1, it is an int (integer) and will take integer numbers not char (characters).
closed account (N8RzwA7f)
Yes I did , I pressed enter. , still it stalls
Just do what chicofeo explained
closed account (N8RzwA7f)
i know I changed the type to char , and then it worked for a while, but now neither type works
Could you post your new code with the type char, as well as tell us what you input using that code and what the output was.
closed account (N8RzwA7f)
<code>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int argc, char** argv) {
char aInt;
cout << "enter a number:";
cin >> aInt;
cout << "you entered:" << aInt;

cin.get();

return 0;
}
</code>

I entered a character or integer with char first, but now that doesn't work either :
output: enter a number:Press any key to continue....

same with int except i used integers. I think char worked first , but i'm not sure anymore now.
thanks!
I mean. I'm not sure what your problem is here, it works perfectly fine. char is short for character, meaning one character. If you enter the number 55. It will output only 5, because char only holds one character. If you input "My name is" it will output only "M".

What exactly is it that you think is not working correctly?
@ oOoOo , when you have time, take a look at http://www.cplusplus.com/doc/tutorial/variables/ it explains better the different variables types.

Last edited on
closed account (N8RzwA7f)
i'm not seeing :
enter a number: 4 you entered 4 (or such)

and a green 'task running' indicator in the bottom right just keeps going.

Thanks again !
Here is what I get using your program.

https://gyazo.com/867068d0e3827a53fb7a13d09ae125a9
https://gyazo.com/cf684a5b43994742d1e5b4e4d316270e

Could you post a screenshot of what you get when you run the program?
closed account (N8RzwA7f)
how would I do that ?
closed account (N8RzwA7f)
are you using a command prompt? Maybe it's a netbeans problem ?
closed account (N8RzwA7f)
and I don't know how to take a screenshot ?
You can simply google how to take a screenshot and you'll get a billion answers.

Can you tell us what the output is when you enter "Mom"?
Topic archived. No new replies allowed.