C++ 对双方都有影响

C++ 对双方都有影响,c++,count,counter,cmath,C++,Count,Counter,Cmath,通过这个程序,我正在尝试实现类似这样的输出 A+B+C=7 xMin=3 xMax=8 3-10 4-11 5-12 6-13 7-14 8-15 相反,我通常会得到这样的东西 4-0 5-0 6-0 7-0 8-0 只有当我硬编码xMin或xMax来显示时,它才会改变,所有的in-bewteens都不会显示 #include "stdafx.h" #include <iostream> #include <cmath> using namespace std; in

通过这个程序,我正在尝试实现类似这样的输出

A+B+C=7

xMin=3

xMax=8

3-10

4-11

5-12

6-13

7-14

8-15

相反,我通常会得到这样的东西

4-0

5-0

6-0

7-0

8-0

只有当我硬编码xMin或xMax来显示时,它才会改变,所有的in-bewteens都不会显示

#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";
}

当你想在不接触循环的情况下增加它时,你需要编写你的ostream操作符,但我不知道你为什么不简单地增加y

为什么您希望y在for循环中的任何地方都会发生变化?你的代码对我来说似乎没有多大意义。它不会让我对两个答案都进行绿色检查,但从长远来看,每个答案都有帮助。[问题已解决]
 #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;
}