2013-04-05 84 views
0

试图使用适当的货币面额为我的游戏。货币存储为一个字符串(即不能由于我的教授而改变),并且按照白金,金,银和铜的顺序存储。例如,如果我将我的货币初始化为“0.1.23.15”,这意味着我有0白金,1黄金,23白银和15铜。货币面额

但是,我需要能够转换到更高的面额。那是什么意思?一个例子是如果我有105个银片(即0.0.105.0),它应该显示为1个金和5个银(即0.1.5.0)。

我在我的setCost方法中加粗了我的问题。我正在检查一个大于100的数字,如果是 - 我使该列为0,返回到前一个元素,并向ASCII值加1,以提供适当的进位。不幸的是,调试器显示“/ x4”正被转储到元素中,而不是“4”。有谁知道这是为什么,我怎么能改变它?

编辑:编辑的代码,只要你不输入一个数字它的工作原理上面100有一个关于如何使它超过100

较大的数字工作的大脑经过这一番我曾经写过的最不经意的代码。请温柔。 :(

void Potion::setCost(std::string cost) 
{ 
    char buffer[256]; 
    std::string currencyBuffer [4]; 
    int integerBuffer[4]; 
    int * integerPointer = nullptr; 
    int temp = 0; 
    int i = 0; 
    char * tokenPtr; 
    //Convert string to cString 
    strcpy(buffer, cost.c_str()); 

    //Tokenize cString 
    tokenPtr = strtok(buffer, "."); 

    while(tokenPtr != nullptr) 
    { 
     //Convert ASCII to integer 
     temp = atoi(tokenPtr); 

     //Store temp into currency buffer 
     integerBuffer[i] = temp; 

     //Make pointer point to integer buffer 
     integerPointer = &integerBuffer[i]; 

     if(*integerPointer < 100) 
      currencyBuffer[i] = tokenPtr; 
     else 
     { 
      //Store zero in column if number is 
      //greater than 100 
      temp2 = temp % 100; 
      itoa(temp2, temp3, 10); 
      currencyBuffer[i] = temp3; 

      //Go back and add one to currency buffer 
      temp = atoi(currencyBuffer[i-1].c_str()); 
      temp += 1; 
      itoa(temp, temp3, 10); 
      currencyBuffer[i - 1] = temp3; 
     } 

     i++; 

     //Get next token 
     tokenPtr = strtok(nullptr, "."); 
    } 
    NewLine(); 

    std::string tempBuffer; 

    //Store entire worth of potions 
    tempBuffer = "Platinum: "; 
    tempBuffer += currencyBuffer[0]; 
    tempBuffer += "\nGold: "; 
    tempBuffer += currencyBuffer[1]; 
    tempBuffer += "\nSilver: "; 
    tempBuffer += currencyBuffer[2]; 
    tempBuffer += "\nCopper: "; 
    tempBuffer += currencyBuffer[3]; 

    mCost = tempBuffer; 
} 
+0

uhm ...不知道这里,但是,为什么0?如果是150S,那应该是1G50S,不是吗?你需要看看分裂和余下......这应该有所帮助:http://www.daniweb.com/software-development/cpp/threads/9349/c-division-remainder-help注意最后一个帖子,这是非常重要的... – MaxOvrdrv 2013-04-05 03:13:35

+0

这绝对是我的错。它应该是模数算子得到余数(即如果它是105,其余的将是5,其中一个作为进位)。 – MrPickle5 2013-04-05 03:17:18

回答

1

我认为这个问题是在这一行:

currencyBuffer[i - 1] = temp; 

你为一个字符串(currencyBuffer[i-1]),这将导致垃圾字符写入此指派一个int(temp)。是允许的,显然是: (Why does C++ allow an integer to be assigned to a string?) 因为整数可以隐式转换为字符,并字符可以被分配到字符串,.

你想临时转换为使用012一个char或类似的函数(当你从字符串中获得int时,你已经做了相反的处理,atoi)。

既然你在C++的时候,一个简单的方法来做到这一点是:

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

非常感谢。我用itoa,现在它完美地工作。不幸的是,我无法弄清楚如何解决“temp2 = temp%100”的问题线。如果用户输入200,那么应该有两个进位,但是现在我只需要设置一个进位而不管(即如果数字大于100)。 – MrPickle5 2013-04-05 03:35:26

+0

是的,因为你有命令“返回并添加一个货币缓冲区”。 :)你想把'temp + = 1;'换成'temp + = atoi(tokenPtr)/ 100;' – maditya 2013-04-05 03:39:05

+0

'%'得到余数。你应该有另一个变量,我们称之为'quotient',得到商:'quotient = temp/100;'。用一个整数除以另一个将得到商,因此将例如205乘100会给你2.然后你说'temp + =商数''。在前面的评论中,我没有把事物放在一个名为'quotient'的单独变量中,而是因为我很懒,我只是用'atoi'重新获得了这个数字。 – maditya 2013-04-05 03:42:36

1

不知道这只是我在这里(我的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 
} 

从那里,你有一个数组,用正确的文本/字,进入一个数组,你可以用它来做你的计算...虽然...不要引用我;

1

这是我创建的函数来解析你的号码。它没有问题的数字大于...使用它,如果你愿意=)

unsigned long int str2cur(std::string cost) 
{ 
    unsigned long int money = 0; 
    //given that there are always 4 segments 
    int end; 
    do 
    { 
     end = cost.find('.', 0); 
     money = money * 100 + atoi(cost.substr(0, end).c_str()); 
     cost.erase(0, end + 1);   
    } 
    while (end != std::string::npos); 
    return money; 
} 
+0

此功能的输出是最低面额的总金额 – Kupto 2013-04-05 03:51:45