小编Xia*_*ang的帖子

这个输入中float和double有什么区别?

我有两个输入,唯一的区别是我在第二个输入中用"float"替换"double".但是,第一个可以按预期运行,但不能运行第二个.第二个不以0.1的输入结束.有谁对此有一些想法?非常感谢!

第一输入:

#include <iostream>

using namespace std;

int main()
{
    double input;
    input = 0;
    double sum = 0;
    cout << "Please enter a series numbers and end with 0.1: ";
    cin >> input;
    while (input != 0.1)
    {
        sum += input;
        cout << "The cumulative sum is: " << sum << endl;
        cin >> input;
    }
    return 0;
}


Please enter a series numbers and end with 0.1: 1 2 3 0.1
The cumulative sum is: 1
The cumulative …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point double

1
推荐指数
1
解决办法
304
查看次数

意外的cin行为

我正在编写一个函数int count_words(string str),它返回字符串中所有单词的计数.

问题是无论输入如何,结果都是1.任何人都可以帮忙吗?

#include <iostream>
#include <string>

using namespace std;

int count_words(string str)
{
    int i,j;
    j = 1;
    int l = str.length();
    for(i = 0; i < l; i++)
    {
        string c = str.substr(i,1);
        if(c == " ")
        {
            j++;
        }
    }
    cout << "The total word in the string: " << j << endl;
    return j;
}


int main()
{
    string str;
    cout << "Please enter a string: ";
    cin >> str;
    int result = …
Run Code Online (Sandbox Code Playgroud)

c++ string

1
推荐指数
1
解决办法
264
查看次数

标签 统计

c++ ×2

double ×1

floating-point ×1

string ×1