简体   繁体   English

为什么我的代码无法在屏幕上打印任何内容? (C ++)

[英]Why won't my code print anything from the screen? (C++)

I am working with a file that contains a set of data: 我正在使用包含一组数据的文件:

x values    y values
20.00       0
20.02       15
20.04       27
20.06       39
20.08       54
20.10       65
20.12       75

The program is supposed to calculate the area under curve. 该程序应该计算曲线下的面积。 In this part I am to do so for the first 6 xy sets. 在这部分中,我将针对前6个xy集执行此操作。 My algorithm for finding the area is the sum of each trapezoid , which for these constraints, should only be 5 trapezoids. 我找到面积的算法是每个梯形的总和,对于这些约束,它们应该仅为5个梯形。 The each of each trapezoid is defined by: A = 0.5(base-base_0) * (height + height_0). 每个梯形的每个定义为:A = 0.5(base-base_0)*(height + height_0)。

This is my code: 这是我的代码:

int main()
{
    ifstream infile;
    double AreaCurve = 0.0, NumberTraps = 0.0, height, height_0, base, base_0, AreaTrap;

    infile.open("xydata.dat");

    cin.ignore(20, '2');
    while (NumberTraps < 6)
    {
        infile >> base_0;
        infile >> height_0;
        infile >> base;
        infile >> height;
        AreaTrap = 0.5 * (base - base_0) * (height + height_0);
        AreaCurve += AreaTrap;
        NumberTraps++;
    }

    cout << "The area under the curve is: " << setprecision(4) << AreaCurve << endl;

    infile.close();

    return 0;
}

When I compile the program nothing prints to the screen. 当我编译程序时,屏幕上不会打印任何内容。 I am not sure why this is happening but I believe it may have to do with an error in the line of my code containing the cin.ignore function (I can't edit the data, and I need to skip the line that reads: "x values\\t\\ty values") 我不确定为什么会这样,但是我认为这可能与我的包含cin.ignore函数的代码行中的错误有关(我无法编辑数据,并且我需要跳过以下行: “ x值\\ t \\ ty值”)

Remove the line cin.ignore(20, '2'); 删除行cin.ignore(20, '2'); .

This line will make your program wait for user input until they either enter twenty 2 s, or enter a different character. 这条线将让你的程序等待用户输入,直到他们要么进入21个2 S,或者输入一个不同的角色。

You shouldn't be reading any user input in this program, your input data comes from infile . 您不应该在该程序中读取任何用户输入,您的输入数据来自infile

You will need to add code to ignore the first line of infile . 您将需要添加代码以忽略infile的第一行。 A simple way to do that is string s; getline(infile, s); 一个简单的方法是string s; getline(infile, s); string s; getline(infile, s);

Your program's main loop has a logic bug as well. 您的程序的主循环也有一个逻辑错误。 You are reading in 2 lines at a time and considering that trapezoid, but then you are ignoring the trapezoid just after that one. 您一次要读两行,并考虑该梯形,但随后就忽略了该梯形。 (So you only count about half of the trapezoids). (因此,您只计算大约一半的梯形)。

Finally you should check for input success before processing that trapezoid: 最后,您应该在处理梯形之前检查输入是否成功:

if ( !infile )
    break;

To ignore the first line of the infile stream (regardless of its length) you need: 要忽略的第一线infile流(不管其长度),你需要:

infile.ignore( numeric_limits<streamsize>::max(), '\n' ) ;

Note, this needs the <limits> header. 注意,这需要<limits>标头。

You do not need to ignore to the first '2'. 您无需忽略第一个“ 2”。 For starters the first x value would always have to start with '2', but more usefully std::istream::operator>> (double&); 对于初学者,第一个x值始终必须以“ 2”开头,但更有用的是std::istream::operator>> (double&); skips white-space including line ends in any case. 在任何情况下都跳过空格,包括行尾。

There are other issues with this code, such as attempting to read more values that the file contains, an incorrect integration algorithm, and having no error checking for a valid stream. 此代码还存在其他问题,例如尝试读取文件包含的更多值,错误的集成算法以及没有对有效流进行错误检查。 Consider: 考虑:

infile.ignore( numeric_limits<streamsize>::max(), '\n' ) ;

infile >> base_0;
infile >> height_0;

while( infile.good() )
{
    infile >> base;
    infile >> height;

    if( infile.good() )
    {
        AreaTrap = 0.5 * (base - base_0) * (height + height_0);
        AreaCurve += AreaTrap;

        base_0 = base ;
        height_0 = height ;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM