为什么我的代码组合而不是添加数字?

时间:2017-09-19 20:41:52

标签: c++

我不确定为什么会这样,但是当我运行它时,需要数字并将它们组合起来而不是添加它们。

例如,如果我为每个输入键入1,2和3,它将输出$ 123而不是$ 6。

该计划计算花园中心花卉展示需要花多少钱。

#include <iostream>
using namespace std;

int main()
{ 
    // Get the cost of soil.
    std::string Soil;
    std::cout << "What does the soil cost? " <<std::endl;
    std::cin >> Soil;

    // Get the cost of flower seeds.
    std::string Flower;
    std::cout << "What do the flower seeds cost? " <<std::endl;
    std::cin >> Flower;

    // Get the cost of the fence.
    std::string Fence;
    std::cout << "What does the fence cost? " <<std::endl;
    std::cin >> Fence;

    // Calculate the total cost.
    std::string total;
    std::cout total = Soil + Flower + Fence;

    // Display the total cost
    std::cout << "The total cost is $" << total;
    std::cout << "." << std::endl;

    return 0;
}

6 个答案:

答案 0 :(得分:1)

使用int而不是string。

#include <iostream>
using namespace std;

int main()
{ 
int soil, flower, fence, total;

// Get the cost of soil.
std::cout << "What does the soil cost? " <<std::endl;
std::cin >> soil;

// Get the cost of flower seeds.
std::cout << "What do the flower seeds cost? " <<std::endl;
std::cin >> flower;

// Get the cost of the fence.
std::cout << "What does the fence cost? " <<std::endl;
std::cin >> fence;

// Calculate the total cost.
total = Soil + Flower + Fence;

// Display the total cost
std::cout << "The total cost is $" << total;
std::cout << "." << std::endl;

return 0;

答案 1 :(得分:1)

使用数据类型字符串,字符串的运算符+执行字符串连接。对于算术计算,请使用数字数据类型,例如double

int main()
{
    // Get the cost of soil.
    double Soil;
    std::cout << "What does the soil cost? " <<std::endl;
    std::cin >> Soil;

    // Get the cost of flower seeds.
    double Flower;
    std::cout << "What do the flower seeds cost? " <<std::endl;
    std::cin >> Flower;

    // Get the cost of the fence.
    double Fence;
    std::cout << "What does the fence cost? " <<std::endl;
    std::cin >> Fence;

    // Calculate the total cost.
    double total =Soil + Flower + Fence;

    // Display the total cost
    std::cout << "The total cost is $" << total;
    std::cout << "." << std::endl;

    return 0;
}

答案 2 :(得分:0)

您的三个变量是string+是字符串的连接运算符。

std::string Soil;替换为int Soil;,将FlowerFence替换为V [libjvm.so+0x643ee4] InstanceKlass::find_method_index(Array<Method*>*, Symbol*, Symbol*, bool, bool)+0x14 ,然后从那里开始。

答案 3 :(得分:0)

好吧,看看你正在使用的类型:

std::string Soil;
[...]
std::string Flower;
[...]
std::string Fence;

您使用的是文本类型而不是数字,默认情况下,标准求和运算符+会重载以连接字符串。只需将其更改为int或尝试转换。

答案 4 :(得分:0)

  

为什么我的代码组合而不是添加数字?

字符串连接。

另一种需要考虑的c ++方法。

使用std :: stoi()将字符串转换为int并在header&lt;中提供字符串&gt;

int main(int, char**)
{
   // Get the cost of soil.
   std::string Soil;
   std::cout << "What does the soil cost? " <<std::endl;
   std::cin >> Soil;

   // Get the cost of flower seeds.
   std::string Flower;
   std::cout << "What do the flower seeds cost? " <<std::endl;
   std::cin >> Flower;

   // Get the cost of the fence.
   std::string Fence;
   std::cout << "What does the fence cost? " <<std::endl;
   std::cin >> Fence;

   // Calculate the total cost.
   int  total = (std::stoi(Soil) +
                 std::stoi(Flower) +
                 std::stoi(Fence));

   // Display the total cost
   std::cout << "The total cost is $"
             << total
             << "." << std::endl;

   return(0);
}

答案 5 :(得分:-2)

C ++是类型安全的,这意味着当您将Soil,Flower和Fence声明为类型字符串时,标准输入必须将它们保存为字符串。因此,“+”被解释为连接,而不是添加。

我会将它们声明为整数或无符号整数,并且与total相同。

或者您可以将值转换为整数或无符号整数。我离C和C ++太多年了,无法获得语法。

我认为可能是:

std::string total = NumberToString((int)Soil + (int)Flower + (int)Fence);