双方的计票都中断了

Disruption in the count for both sides

本文关键字:中断      更新时间:2023-10-16

使用这个程序,我试图实现如下输出

A + B + C = 7

xMin = 3

3 - 10

4 - 11

5——126——13

7——14

8——15

我通常会得到这样的东西

4 - 0

5 - 0

6 - 0

7 - 0

8 - 0

只有当我硬编码xMin或xMax来显示时,它才会改变,所有介于两者之间的值都不会显示。

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    int X = 0;
    double a, b, c, xMin, xMax;
    double y = 0;
    cout << "#1(A): ";
    cin >> a;
    cout << "n#2(B): ";
    cin >> b;
    cout << "#3(C): ";
    cin >> c;

    cout << "Enter Xmin" << endl;
    cin >> xMin;
    cout << "Enter Xmax" << endl;
    cin >> xMax;

    y = a + b + c + X;
    for (int count = xMin; count <= xMax; count++)
    {
        cout << count << "t" << y << "n";
    }
    return 0;
}

您的for循环是错误的(您没有更新上界),将其更改为:

y = a + b + c;
for (int count = xMin; count <= xMax; count++)
{
    cout << count << "t" << count + y << "n";
}
 #include <iostream>
 #include <cmath>
 using namespace std;
struct updInt{
int xMax;
int xMin;
int inc;
int val;
bool flag;
friend ostream& operator<<(ostream& os, updInt& dt){
    os <<dt.val;
    dt.val+=dt.inc;
    if(dt.val>dt.xMax)
        dt.flag=true;
    if(dt.val<dt.xMin)
        dt.flag=true;
    return os;
}
updInt(int a,updInt X,int inc=1){
    this->val=a+X.val;
    this->xMax = a + X.xMax;
    this->xMin = a + X.xMin;
    this->inc =inc;
    flag=false;
}
updInt(int max,int min,int val,int inc=1){
    this->val= val;
    this->xMax = max;
    this->xMin = min;
    this->inc = inc;
    flag=false;
}};
int main(){
int a, b, c, xMin, xMax;
cout << "#1(A): ";
cin >> a;
cout << "n#2(B): ";
cin >> b;
cout << "#3(C): ";
cin >> c;

cout << "Enter Xmin" << endl;
cin >> xMin;
cout << "Enter Xmax" << endl;
cin >> xMax;
updInt X(xMax,xMin,0);
updInt y(a + b + c , X);
for (int count = xMin; count <= xMax; count++)
{
    cout << count << "t" << y << "n";
    if(y.flag)
        break;
}
return 0;
}

你需要写你的ostream操作符,当你想增加它而不触及循环,但我不知道为什么你不直接增加y。