0
آرایه ها در c++
چطور در c++ با استفاده از آرایه ها و متغیر ها یک از کاربر یک عدد ۱۶ رقمی بگیریم( int a[16] ) و تک تک رقم های آن را جداگانه چاپ کنیم ؟ How to get a 16-digit number (int a[16]) from the user using arrays and variables in C++ and print each digit separately?
2 Réponses
+ 4
Black Knight
1. Read the console input as 16 individual characters.
2. Convert each character digit to its equivalent int value by subtracting char constant '0' (ASCII int value 48) from the input character. Store in the a[16] array.
3. Loop through the a[16] array and print each digit.
It is not necessary to store the digits in an array. You could do the same thing minimally with only a single char variable in one loop that performs the input, conversion, and output.
+ 2
string text;
//read user input as string
cin >> text;
//optional: check for possible errors
if (text.size() != 16 || text.find_first_not_of("0123456789") != string::npos) return "error";
//convert every chacter to integer and print it
for (size_t x = 0; x != 16; x++)
{
cout << (int)(text[x] - '0');
}