-3
Я хочу рассчитать сумму каждой цифры в моем почтовом индексе, поэтому я написал такую функцию реализации, но, она показывает «0» в качестве ответов, тогда я понимаю, что это должен быть вызов сначала, я добавляю correctionDigitOf() в конструктору «Zipcode :: Zipcode», то мой почтовый индекс все «0' после того, как я делаю это так, пожалуйста, помогите!рассчитать сумму цифр в моем почтовом индексе
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cmath>
using namespace std;
class Zipcode
{
public:
Zipcode();
void createZipcode();
int getZipcode();
int extract(int location);
int getCoreectionDigit();
void correctionDigitOf();
private:
int zipcode;
int correctionDigit;
};
Zipcode::Zipcode()
{
zipcode = 0;
correctionDigit = 0;
createZipcode();
}
void Zipcode::createZipcode()
{
zipcode = 10000 + rand() % 99999;
}
int Zipcode::getZipcode()
{
return zipcode;
}
int Zipcode::extract(int location)
{
int i = 1;
while (i<location)
{
i++;
zipcode /= 10;
}
return zipcode % 10;
}
void Zipcode::correctionDigitOf()
{
correctionDigit = extract(1) + extract(2) + extract(3) + extract(4) + extract(5);
}
int Zipcode::getCoreectionDigit()
{
return correctionDigit;
}
int main()
{
const int num = 10;
for (int i = 0; i<num; i++)
{
Zipcode zip;
cout << zip.getZipcode()
<< ' '
<< zip.getCoreectionDigit()
<< endl;
}
return 0;
}
Вы создаете почтовые индексы, которые имеют до 6 цифр, а не 5. –
Вы забыли задать вопрос. –