通过这个程序,我试图实现一个看起来像这样的输出
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;
}
答案 0 :(得分:0)
您的for
循环错误(您没有更新上限),请将其更改为:
y = a + b + c;
for (int count = xMin; count <= xMax; count++)
{
cout << count << "\t" << count + y << "\n";
}
答案 1 :(得分:0)
#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。