int get()

closed account (jLNv0pDG)
int get();
Extracts a character from the stream and returns its value (casted to an integer).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
	std::cout << "Enter a two digit number from 10-99: ";

	// How do I just put numbers in the input stream here?
        // "cin" goes here?

	int a, b;

	a = int get();
	std::cout << "The first number is: " << a << std::endl;

	b = int get();
	std::cout << "The second number is: " << b << std::endl;

	return 0;
}


I want to be able to input a 2 digit integer and return each digit separately. (There are probably other/easier ways to do this but I'd like to try it like this as a learning exercise.)

Am I even using int get() correctly?
Last edited on
Not quite. Remove the "int" and just type "get()" like you would for any other function call.

I'm not sure though... are you using std::cin.get()?
By the way, when you want to read a few characters from a stream, std::cin is fine. Most people don't recommend it though - it does no typechecking and can easily mess up if you don't use it wisely. For std::strings, use getline(stream, std::string), for single characters, I think you can use std::cin.get(); or this "int get()" (but remove the int when calling it).
By the way, when you want to read a few characters from a stream, std::cin is fine. Most people don't recommend it though
Is not std::cin itself which isn't recommended, is the >> operator having cin as left hand operand to not be recommended: http://www.cplusplus.com/forum/articles/6046/
closed account (jLNv0pDG)
@chrisname: "Remove the "int" and just type "get()" -- Yes, in hindsight that seems obvious. ^.^'
Although, I think you almost need to be a c++ expert to understand some of the c++ help documentation!

"are you using std::cin.get()?" -- I was wondering if there was just a plain cin() function that read console input (cin = ConsoleINput?) and just left that input in some sort of buffer/stream/RAM thing which plain get() could then access later on. It appears there is not.

int get();
Extracts a character from the stream and returns its value (casted to an integer).


I made a mistake here. I thought that "returns its value (casted to an integer)" meant, if I input '9' it would return the value 9. Instead it returns the ASCII value of '9' which is 57. So, int a = 57, while I should have been doing char a = 9.


@Bazzy: Thanks for the link.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 // How to get a number.
 int myNumber = 0;

 while (true) {
   cout << "Please enter a valid number: ";
   getline(cin, input);

   // This code converts from string to number safely.
   stringstream myStream(input);
   if (myStream >> myNumber)
     break;
   cout << "Invalid number, please try again" << endl;
 }
 cout << "You entered: " << myNumber << endl << endl;


That seems like a *lot* of effort just to input a number... Do you make a function or something out of it?

1
2
3
4
5
6
7
8
9
10
11
12
13
int cin_int(void)
{
    std::string input = "";
    int myNumber = 0;

    while (true) {
    getline(std::cin, input);
    std::stringstream myStream(input);
    if (myStream >> myNumber)
        break;
    }
    return myNumber;    
 }

Topic archived. No new replies allowed.