0
/*
  David Ballantyne
  10/03/13
  sum of a series

*/
#include <cstdlib>
#include <iostream>


using namespace std;

//no global constants

//no functioning prototypes

int main( int argc, const char* argv[] ){
    //Declare Variables
    short n,knwn,num,sum=0;

    //prompt user
    cout<<"input a number grater than 0";
    cin>>n;

    //Calculate
    for( int num=1;num<=n;sum+=num++);


    //known
    knwn =(n(n+1))/2;

    //output

    cout<<"the sum of the series is"<<sum<<endl;
    cout<<"the sum of the known series is"<<knwn<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

在第 30 行知道它说“n”不能用作函数

我在介绍 c++ 课程并且遇到了一点困难我确信解决方案很简单,但我似乎无法找到它!

4

1 回答 1

4

您省略了乘法运算符。更改knwn =(n(n+1))/2;knwn =(n * (n+1))/2;

于 2013-10-03T17:10:47.267 回答