注册 登录
编程论坛 C++教室

高手帮我看看错在哪了,谢谢了!!!

flysky102 发布于 2008-03-05 13:32, 530 次点击
我是学c++新手,今天做了道题,不知道错哪了,各位大侠帮我改下,谢谢了。
题目是:编写一个要求用户输入两个整数的程序,该程序将计算并输出这两个整之间所有整数的和,例如,输入2和9,则程序将指出2—9之间所有整数的和为44
我的程序如下:
# include <iostream>
int main()
{using namespace std;
 int a,b;
 cout<<"Enter the first:";
 cin>>a;
 cout<<"Enter the last:";
 cin>>b;
 int c=a;
 for(a;a<=b;a++)
  {c=c+a;
   a++;
  }
  cout<<c<<endl;
  cin.get();
  return 0;
}
 谢谢啦!!!
8 回复
#2
flysky1022008-03-05 13:33
顺便说一下,我用的是dev-c++编译器
#3
woami662008-03-05 13:41
c的初值应该是0
#4
flysky1022008-03-05 13:50
我把初值改成0了,可是我在要求输入完2个数字后,一按回车,结果执行窗口就一下子关了,我也没看到显示的结果,是怎么回事啊
#5
hylhp2008-03-05 14:24
a++;出现了两次。
for(a;a<=b;a++)
  {c=c+a;
  }
c的初值就是0.
#6
flysky1022008-03-05 14:28
我把程序改成这样了:
# include <iostream>
int main()
{using namespace std;
 int a,b;
 cout<<"Enter the first:";
 cin>>a;
 cout<<"Enter the last:";
 cin>>b;
 int c=0;
 for(a;a<=b;a++)
  {c=c+a;

  }
  cout<<c<<endl;
  cin.get();
  return 0;
}
 
但是当我输入完第2个数字后按回车,结果执行界面一下子就没了,我还没看到结果呢,这是什么原因呢,我用的是dev-c++编译器
#7
hylhp2008-03-05 15:04
# include <iostream.h>
# include<cstdlib>
int main()
{using namespace std;
int a,b;
cout<<"Enter the first:";
cin>>a;
cout<<"Enter the last:";
cin>>b;
int c=0;
for(a;a<=b;a++)
  {c=c+a;

  }
  cout<<c<<endl;
  system("pause");
  return 0;
}
#8
hylhp2008-03-05 15:05
system("pause");//暂停
#9
flysky1022008-03-05 15:08
哦,知道了,谢谢,刚学,不知道还能这样。实在是太感谢了!!!
1