[英]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.您替換
bool isPrime(num);
至
bool ret = isPrime(num);
要么
(void)isPrime(num);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.