How to get integer size/position?

Pretty simple question:

In strings you can do variable.size() or variable.at(number),so how can you do the same for integers or any variable type other than a string?
variable.size() or variable.at(number)

I think you can do this with strings because they're character arrays.

If you want to do it with integers or other variable types, you have to use sizeof

1
2
3
int x;
double y;
cout << sizeof(x) << endl << sizeof(y) << endl;


Output:

4
8


Ok, thx, will try it when I get the chance.
Oh and that reminds me of one more thing, how can I make it so that a variable only accepts integers? Basically a command that checks if the variable is an integer and asks you again to write something if it's not an integer.
kik4444 wrote:
Oh and that reminds me of one more thing, how can I make it so that a variable only accepts integers? Basically a command that checks if the variable is an integer and asks you again to write something if it's not an integer.
http://www.LB-Stuff.com/user-input
An int always holds an integer. It cannot hold anything else ever.

If you do this....
1
2
3
// prompt the user for an integer:
int foo;
cin >> foo;

...and the user does NOT enter an integer, then the stream (in this case cin) will enter a bad state, which you can check for in a simple if statement:

1
2
3
4
if(!cin)
{
    // stream in a bad state.  User probably did not input an integer
}


To put the stream back in a good state, you'll need to call clear(). You'll also want to call ignore to wipe out the data left in the input buffer.

Combine all of that with a loop to ask the user again and you can have something like this:

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

int main()
{
    int x;
    cout << "Input an integer: ";
    cin >> x;

    while(!cin) // bad state?
    {
        cin.ignore( numeric_limits<streamsize>::max() );  // clear input buffer. IE: erase the
                                                          //   non-integer stuff they just input
        cin.clear();        // tell cin to go back in a good state
        
        cout << "That was not an integer.  Try again: ";
        cin >> x;
    }
}



Yes, it's overly complicated for what should be very simple. Welcome to iostream.
Last edited on
Is there a way to make it NOT spam "That was not an integer. Try again: "; forever and ever?
You are probably missing the ignore or clear if it is spamming.

Try this instead actually

1
2
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');


By deafult it is set to EOF so I overwrote it so it ignores until end of the line.


If the snippet I provided wasn't clear enough here is a sample:

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

int main()
{
    int x;
    while(std::cout << "Please enter an integer: " && !(std::cin >> x))
    {
        std::cin.clear(); //clear error flag
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //ignore anything left in buffer
        //cin.ignore(1024, '\n'); would suffice

       std::cerr << "That was not an integer.\n";
    }
}
Please enter an integer: a
That was not an integer.
Please enter an integer: 1
Last edited on
Thx, it works just I wanted now. If you want to, I could paste the entire program's code right here. So far there's protection against everything I could think of, or rather, I think error-handling would be the better word:
- not entering a number between 1 and 100;
- not entering any number at all;
- entering a number so large, it would normally bug out the program.
And I also shortened a few functions where possible as well as a few class functions

Here's the source file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "class.h"
using namespace std;
//Note: the point of this program is to act as an exercise for classes and objects, which is why it's so long, yet it doesn't do much.
int main()
{
	while (1 == 1)
	{
		int tHappy = 0;
		do
		{
			cout << endl << endl << "How happy is Tom on a scale of 1 to 100?: ";
			while (!(cin >> tHappy))
			{
				cin.clear();
				cin.ignore(numeric_limits<streamsize>::max(), '\n');

				cout << endl << "Error! You either didn't enter a number or your number was too long! Retry: ";
			}
			if (tHappy > 100 || tHappy < 0)
			{
				warning("I said, on a scale of 1 to 100! Retry!: ");
			}
		} while (tHappy > 100 || tHappy < 0);

		int tHunger = 0;
		do
		{
			cout << endl << endl << "How hungry is Tom on a scale of 1 to 100?: ";
			while(!(cin >> tHunger))
			{
				cin.clear();
				cin.ignore(numeric_limits<streamsize>::max(), '\n');

				cout << endl << "Error! You either didn't enter a number or your number was too long! Retry: ";
			}
			if (tHunger > 100 || tHunger < 0)
			{
				warning("I said, on a scale of 1 to 100! Retry!: ");
			}
		} while (tHunger > 100 || tHunger < 0);

		vTom obj(tHappy, tHunger);
		string choiceUser = "";

		cout << endl << endl << obj.retStat() << endl << endl;
		do
		{
			cout << "Repeat?: ";
			cin >> choiceUser;

			if (choiceUser == "yes")
			{
				
			}
			else if (choiceUser == "no")
			{
				return 0;
			}

		} while (choiceUser != "yes" && choiceUser != "no");

	}
	return 0;
}


And here's the header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
#include <limits>
#include <string>
using namespace std;

void warning(string lawarning)
{
	cout << endl << lawarning;
}

class vTom
{
private:
	int happiness;
	int hunger;
	string okOrNot;

public:
	vTom(int happy, int hungry)
	{
		happiness = happy;
		hunger = hungry;
		getStat(happiness, hunger);
	}

	void getStat(int, int)
	{
		if (happiness == 0 || hunger == 100)
		{
			string state = "Tom is dead";
			okOrNot = state;
		}
		else if (happiness == 69 && hunger == 69)
		{
			string state = "Tom is aroused";
			okOrNot = state;
		}
		else
		{
			string state = "Tom is fine";
			okOrNot = state;
		}
	}

	string retStat()
	{
		return okOrNot;
	}

};

#endif 

What do you think?
Last edited on
Topic archived. No new replies allowed.