如何从main中调用函数
How to call a function from within main
我正试图从main
调用函数"isPrime",但一旦输入数字,程序就会退出。我不知道问题出在哪里?如果有人能看到我哪里做错了,我会感激的。谢谢。
代码:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
ofstream myfile;
int num;
int main(){
cout << "Please Enter a number " << endl;
cin >> num;
while (num > 3001){
cout << "Your input integer should be less than 3001. Try again, or -1 to exit" << endl;
cin >> num;
if (num == -1){
break;
}
}
bool isPrime(num);
//cout << "CMSC 140 CRN <your course CRN> Project 5: Prime Numbers Written by a student <YourName> Due Date : DD / MM / YYYY>" <<endl;
}
bool isprime(int a){
myfile.open("primelist.txt");
a = num;
int prime;
if (a <= 1 || (a % 2 == 0)){ // check if the number is even
cout << " that is not a prime number" << endl;
return false;
}
else if (a == 2){
cout << "tht is a prime number" << endl;
return true;
}
else{
int divisor = 3;
int top = a - 1;
while (divisor <= top)
{
if (a % divisor == 0)
return false;
}
divisor += 2; //check the odd divisions
return true;
}
for (int i = 4; i < a; i++){
if (i % 2 != 0 || i % 3 != 0){
myfile << "2, 3, " << i << endl;
}
}
}
bool isPrime(num);
老实说,我认为这不应该编译。不过,从语法上讲,最接近的匹配是函数声明。。。这实际上毫无作用。
因此,正如在评论中所说,省略该声明中的bool
。
我想你错过了这样的东西:1.你更换
布尔是素数(num);
至
bool ret=isPrime(num);
或
(void)isPrime(num);
- 您声明函数布尔是素数(Int a);在主要功能之前
相关文章:
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?