Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 货币面额_C++_String_Parsing - Fatal编程技术网

C++ 货币面额

C++ 货币面额,c++,string,parsing,C++,String,Parsing,尝试为我的游戏使用正确的货币面额。货币以字符串形式存储(即,由于我的教授,无法更改该字符串),其顺序为白金、黄金、白银和铜。例如,如果我将我的货币初始化为“0.1.23.15”,这意味着我有0白金、1黄金、23银和15铜 然而,我需要能够隐藏到更高的面额。这是什么意思?例如,如果我有105枚银币(即0.0.105.0),它应该显示为1枚黄金和5枚白银(即0.1.5.0) 我用setCost方法解决了我的问题。我正在检查一个数字是否大于100,如果大于100,我将该列设为0,返回到上一个元素,并将

尝试为我的游戏使用正确的货币面额。货币以字符串形式存储(即,由于我的教授,无法更改该字符串),其顺序为白金、黄金、白银和铜。例如,如果我将我的货币初始化为“0.1.23.15”,这意味着我有0白金、1黄金、23银和15铜

然而,我需要能够隐藏到更高的面额。这是什么意思?例如,如果我有105枚银币(即0.0.105.0),它应该显示为1枚黄金和5枚白银(即0.1.5.0)

我用setCost方法解决了我的问题。我正在检查一个数字是否大于100,如果大于100,我将该列设为0,返回到上一个元素,并将一个添加到ASCII值中,给出正确的进位。不幸的是,调试器显示“/x4”被转储到元素中,而不仅仅是“4”。有人知道这是为什么吗?我怎样才能改变它

编辑:编辑代码,只要输入的数字不超过100,代码就可以工作。在如何使其适用于大于100的数字方面,大脑出现了问题。

这是我写过的最草率的代码。请温柔一点(

无效药剂::设置成本(标准::字符串成本)
{
字符缓冲区[256];
std::字符串currencyBuffer[4];
整数缓冲[4];
int*integerPointer=nullptr;
内部温度=0;
int i=0;
字符*ptr;
//将字符串转换为cString
strcpy(buffer,cost.c_str());
//标记化cString
tokenPtr=strtok(缓冲区“.”);
while(tokenPtr!=nullptr)
{
//将ASCII转换为整数
温度=atoi(令牌PTR);
//将临时数据存储到货币缓冲区中
整数缓冲[i]=温度;
//使指针指向整数缓冲区
整数点=&integerBuffer[i];
if(*整数点<100)
currencyBuffer[i]=tokenPtr;
其他的
{
//如果数字为,则在列中存储零
//大于100
温度2=温度%100;
itoa(temp2、temp3、10);
currencyBuffer[i]=temp3;
//返回并向货币缓冲区添加一个
temp=atoi(currencyBuffer[i-1].c_str());
温度+=1;
itoa(临时、临时3、10);
currencyBuffer[i-1]=temp3;
}
i++;
//获取下一个令牌
tokenPtr=strtok(nullptr,“.”);
}
换行符();
std::字符串临时缓冲区;
//储存全部价值的药剂
tempBuffer=“白金:”;
tempBuffer+=currencyBuffer[0];
tempBuffer+=“\n黄金:”;
tempBuffer+=currencyBuffer[1];
tempBuffer+=“\n服务器:”;
tempBuffer+=currencyBuffer[2];
tempBuffer+=“\n端口:”;
tempBuffer+=currencyBuffer[3];
mCost=临时缓冲区;
}

我认为问题出在这一行:

currencyBuffer[i - 1] = temp;
您正在将int(
temp
)分配给字符串(
currencyBuffer[i-1]
),这会导致写入垃圾字符。显然,这是允许的: () 因为整数可以隐式转换为字符,而字符可以分配给字符串

您希望使用
itoa
或类似函数将temp转换为char(从字符串中获取int时,您已经正确地执行了相反的操作,atoi)

<>因为你在C++中,一个简单的方法是:

std::stringstream itos;
itos << temp;
currencyBuffer[i-1] = itos.c_str();
std::stringstream itos;

itos我认为问题出在这方面:

currencyBuffer[i - 1] = temp;
您正在将int(
temp
)分配给字符串(
currencyBuffer[i-1]
),这会导致写入垃圾字符。显然,这是允许的: () 因为整数可以隐式转换为字符,而字符可以分配给字符串

您希望使用
itoa
或类似函数将temp转换为char(从字符串中获取int时,您已经正确地执行了相反的操作,atoi)

<>因为你在C++中,一个简单的方法是:

std::stringstream itos;
itos << temp;
currencyBuffer[i-1] = itos.c_str();
std::stringstream itos;

ITOS < P>不确定这是否只是我在这里(我的C++天大约回到13年),您的老师最适合回答您这个问题,但感觉您正在做的事情/如何做是非常需要处理器的。从技术上讲,您最好将整个字符串拆分为一个字符串数组,然后使用这些字符串来确定您的最终计数:

std::string str = "I.have.a.dog";
//replace all DOTS with SPACES for next use
for (int i = 0; i < str.length(); ++i) {
    if (str[i] == '.')
      str[i] = ' ';
}
std::istringstream stm(str) ;
string word ;
while( stm >> word ) // read white-space delimited tokens one by one 
{
   // put word into array
}
std::string str=“I.have.a.dog”;
//将所有点替换为空格,以便下次使用
对于(int i=0;i>word)//逐个读取空格分隔的标记
{
//将字放入数组
}

从那里,你有一个数组,用适当的文本/单词组成一个数组,你可以用来做你的Calcon……只是一个想法……不要引用我的话;

< P>不确定这是否只是我在这里(我的C++天大约回到13年),你的老师将最适合回答你这个问题,但感觉你在做什么/怎么做是非常需要处理器的。从技术上讲,您最好将整个字符串拆分为一个字符串数组,然后使用这些字符串来确定最终计数:

std::string str = "I.have.a.dog";
//replace all DOTS with SPACES for next use
for (int i = 0; i < str.length(); ++i) {
    if (str[i] == '.')
      str[i] = ' ';
}
std::istringstream stm(str) ;
string word ;
while( stm >> word ) // read white-space delimited tokens one by one 
{
   // put word into array
}
std::string str=“I.have.a.dog”;
//将所有点替换为空格,以便下次使用
对于(int i=0;i>word)//逐个读取空格分隔的标记
{
//将字放入数组
}

从那里,你有一个数组,带有适当的文本/单词,进入一个数组,你可以用它来计算。。。只是一个想法。。。不要引用我的话;)

这是我创建的用于解析您的号码的函数。它对大于…的数字没有问题。。。如果愿意,可以使用它=)


这是我创建的用于解析您的号码的函数。它对大于…的数字没有问题。。。如果愿意,可以使用它=)


嗯。。。这里不确定,但为什么是0?如果是150秒,那就是