我的代码的某些部分未运行

时间:2013-11-21 13:14:40

标签: c++ runtime-error

我的一个朋友要我制作一个程序,找出两个用户输入数字中哪一个是最大的,以及数字的总和。我有所有的代码,但是,我很确定它的格式是不正确的。如果我做了一些愚蠢的事情,请原谅我,因为我对C ++没有太多经验。以下是我的代码。

#include <iostream>
#include <windows.h>
using namespace std;

int a, b;
int stats();
int sum();
int fin();

/*This handles user input for a and b*/
int main()
{
cout<<"Enter value one: "<<endl;
cin>>a;
cout<<"Enter value two: "<<endl;
cin>>b;
stats();
sum();
fin();
}
/*This finds out which  number is greater or less than*/
int stats()
{
if (a>b)
cout<< a << " Is greater than " << b <<endl;
else if (a<b)
cout<< a << " Is less than " << b <<endl;
else
cout<< a << " Is equal to " << b <<endl <<endl;
}

/*This finds the sum of a and b*/
int sum()
{
cout<<"The sum of a and b is " << a + b <<endl<<endl;
}

/*This should print which number is greater or
less than, and the sum of the numbers*/
int fin()
{
cout<<stats<<endl;
cout<<sum<<endl;
system ("pause");
return(0);
}

当我编译代码并运行它时,我可以输入a和b的值,但是,之后,程序结束。如果有人能告诉我这是为什么,或提供修复,那么我真的很感激。如果您有任何其他问题,请随时提出。

5 个答案:

答案 0 :(得分:5)

您未在stats()

中致电sum()main()

答案 1 :(得分:4)

我认为很明显,你的主要内容是这个

int main()
{
    cout<<"Enter value one: "<<endl;
    cin>>a;
    cout<<"Enter value two: "<<endl;
    cin>>b;
    stats(); // call the stats function
    sum();   // call the sun function
    fin();   // call the fin function
}

main的最后三行是调用您编写的其他函数的地方,这不会自动发生。

现在上面的代码将无法编译,因为在使用它们之前必须声明这三个函数。所以在main;

之前添加这三行
    // declare the three functions
    int stats();
    int sum();
    int fin();

对于您编写的每个函数,您都有以下三个方面:声明它,定义它(即写入它),调用它(即使用它)。每种语法都不同,你必须学习这三种语法。这与C ++基本相同,所以我非常担心你从哪里学习C ++。你真的应该读一本好的教科书。

答案 2 :(得分:1)

首先,你不是从主调用方法,其次我建议你制作方法 stats()或sum()以便接收两个参数(来自用户的两个输入)比声明两个int全局变量。像这样:

int stats(int a, int b)
{

}

int sum(int a, int b)
{

}

答案 3 :(得分:0)

main()函数中,您所做的只是读取用户输入,然后没有其他内容。别忘了打电话给其他功能..

答案 4 :(得分:0)

您好:在您的代码中存在一些问题(没有什么重要的,所以不要担心):为了保持简短,我已经更改了代码并添加了一些注释(我以前的答案将是执行它的两倍)用纯文本英文):

#include <iostream>
#include <windows.h>

using namespace std;

//  Re-ordered the functions so that no forward declaration is not needed, 
//  and to make it compliant with more compilers.

/*
    Removed the return types from the functions:
    You only need a return type if the function is actually returning a value.
    As the functions were there was no return value necessary as the calculation
    and output were all handled internally to the function

    Also removed the global variables a and b and scoped them to main
    making them passed into functions as parameters.
*/

/*This finds out which  number is greater or less than*/
void stats(int a, int b)
{
    /*
        changed the 3 if statements to one with multiple branches:
        as there are only 3 possible outcomes this seemed more elegant and readable
        Normally put your most common clause at the top-most check if you want that
        evaluation to be the fastest during runtime.
    */
    if (a>b)
        cout<< a << " Is greater than " << b <<endl;
    else if (a<b)
        cout<< a << " Is less than " << b <<endl;
    else
        cout<< a << " Is equal to " << b <<endl <<endl;
}

/*This finds the sum of a and b*/
void sum(int a, int b)
{
    //  Added a bit more voluminous output to make the output easier to track and debug
    //  Also removed the extra variable, as you can do the calculation inline to save 
    //  RAM and processor instructions
    //  Negligible benefits for this program, but becomes more important as your code evolves
    cout<<"The sum of the values = "<< (a+b) <<endl;
}

/*This should print which number is greater or
less than, and the sum of the numbers*/
void fin(int a, int b)
{
    /*
        in here you only need to call the functions, if you do a cout on a function 
        what you will get is effectively garbage (has more meaning but that's for
        later learning:)
    */
    stats(a, b);
    sum(a, b);
}
/*This handles user input for a and b*/
int main()
{
    int a,b = 0;
    cout<<"Enter value one: "<<endl;
    cin>>a;
    cout<<"Enter value two: "<<endl;
    cin>>b;
    fin(a, b);
    //  have added the system pause here so that it happens whether you call fin() or not
    system ("pause");
    return 0;
}

如果我遗漏了任何内容,请告诉我,我会修改答案以适应。 :)