问题
I'm in the process of recreating the old mastermind video game from the '70s.
The users makes a four digit guess, which is then logged as a vector.
I know there exists a way to accept the input as a string, however I would find it odd if there isn't a way to do this with one single int input -- which I haven't been able to find a method for.
The way I have it currently set up, if the user provides an input of 0003, this will be logged as an insufficient digit error, as the value of the int is interpreted as simply 3. Not totally unexpected, but is there really no way to take all four digits at once using an int method?
Thanks!
O
/e With the game mechanics in mind, an input of 3 (insufficient digit error) should be distinguishable from an input of 0003;
回答1:
"Three". "3". "Threeeeeeeeeeeeeeeeee". "03". "003". "0003". "00000000000000003". "III". "٣". "11". "३". "三". "Γʹ".
What do all the above things have in common? They're all representations of the number three†. Are the representations different? Yes. Are the numbers they represent different? No. There is only one number three.
The number three is indistinguishable from the number three as there is only one number three. However, two representations of the number three may be distinct from each other, since they may use different symbols, or different rules for bringing the symbols together.
int
s in C++ (and in pretty much every language with a similar data type) are used to handle numbers. So, if you assign the number three to a variable of such a type, you get the number three. You don't get any particular representation of it. It's the number three. You get a representation of it when you ask for one, like when you output it.
So, how do you tell the difference between "3" and "0003" if they represent the same number? Easy: you don't use numbers.
The fundamental problem here is that you think you are dealing with numbers, but aren't. You are concerned with the representations, not the numbers themselves. So you should use a data type that can handle representations instead of one that can handle numbers. I believe the simplest solution is to use std::string
.
† In order: English, base ten with Indo-Arabic numerals, English with extraneous "e"s to imply a longer final sound in speech, base eight in C notation, base eight in C# notation with an additional redundant zero, base eighty-seven with Indo-Arabic numerals and three redundant zeros, base thirty with Indo-Arabic numerals and twelve redundant zeros, Roman numerals, Eastern Arabic numerals, base two with Indo-Arabic numerals, Devanagari numerals, Chinese numerals, and Greek numerals.
回答2:
You can certainly use a simple int to get all four digits. If the user enters 0003, the program will store it as just 3, but you can still easily recognize that the thousands, hundreds and tens position are all equal to zero. If you are using a single int for your input, you can access each digit separately by using simple division and mods. For example:
int i=3;
if(i<0 || i>9999) printf("error\n");
else {
int i1=(i/1000);
int i2=(i/100)%10;
int i3=(i/10)%10;
int i4= i%10;
printf("i = %d (%d - %d - %d - %d)\n", i, i1, i2, i3, i4);
}
回答3:
the users makes a four digit guess, which is then logged as a vector.
Why don't you actually store the input as a vector then? Read the four values into an array, and then use the array to determine the outcome. To my knowledge there is no easy way of getting ints to store leading zeros.
回答4:
As many calculators, the 0s at the left of a number will be deleted. So there's no way to input 0003 and be recognized as an int (you can read also documentation to understand data types: http://www.cplusplus.com/doc/tutorial/variables/)
I see many possibilities to fix your problem:
Use FLOAT or DOUBLE data type, so you can enter 0003 and interpret it like decimals (0.0003; now, take only the decimal part)
Enter the numbers 1 by 1
Enter an array of char, and then parse it into integer, counting the number of digits and how many of them are 0s.
来源:https://stackoverflow.com/questions/28006637/is-there-a-way-to-take-a-series-of-zeros-as-an-int-input