计算我的邮政编码中的数字总和
calculate sum of digit in my zip code
我想计算邮政编码中每个数字的总和,所以我写了这样的实现函数,但是,它显示"0"作为答案,然后我意识到它应该首先调用,我将correctionDigitOf()添加到构造函数"邮政编码::邮政编码"中,然后我的邮政编码在我这样做后都是"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;
}
你的Zipcode::extract
会改变zipcode
的值,这不可能是你想要的。
相关文章:
- 如何修复我的最大公约数代码?它适用于除零和零以外的所有数字
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 我的问题是关于C++中数字和序列的重复以及如何打印它们?
- 为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 为什么我的位移给出不正确的数字
- 为什么我的 while 循环永远不会在 C++ 中的数字猜测游戏中结束?
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53
- 我可以从具有不同数字分隔符的istream中读取双值吗
- 我需要静态转换我的数字模板值吗
- 我必须更改我的数字最后一个数字和第一个数字,但不要使用仅带有整数或循环的函数.例如从 12345 到 52341
- 如何让我的代码显示文件名不正确或文件中数字的平均值?
- 我的代码应该接受一个数字,并返回字母等级或"Grade is not valid"但 else 语句不起作用
- 为什么当我输入大量数字时,我的计算器程序开始闪烁和滚动
- 我的代码似乎在查找最大数字时存在语法错误
- 有效创建数字签名的正确方法是什么?我可以使用DSA_sign_setup()吗?
- 为什么我的C++程序输出不同的数字来将米转换为英尺?
- 为什么我的阶乘数查找器返回以C++输入的数字?(已编辑)
- 我可以在不指定数字分布的情况下使用随机生成器吗?
- 如何允许我的程序成功读取数字包含的文件