[英]How to call a function from within main
I am trying to call the function "isPrime" from main
, but as soon as I enter a number the program exits. 我试图从
main
调用函数“ isPrime”,但是一旦输入数字,程序就会退出。 I don't know what the problem is? 我不知道是什么问题? If anyone can see where am I doing wrong, I will appreciate it.
如果有人看到我在哪里做错了,我将不胜感激。 Thanks.
谢谢。
Code: 码:
#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);
Honestly, I don't think this should compile. 老实说,我不认为这应该编译。 The nearest match, syntactically, though would be a function declaration...which pragmatically does nothing.
从语法上讲,最接近的匹配是函数声明...在实用上什么都不做。
So as was said in comments, omit the bool
in that statement. 因此,正如评论中所说,在该声明中省略“
bool
。
I think you are miss something like this: 1. You replace 我认为您很想念这样的事情:1.您替换
bool isPrime(num);
bool isPrime(num);
To 至
bool ret = isPrime(num);
bool ret = isPrime(num);
Or 要么
(void) isPrime(num);
(void)isPrime(num);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.