当我 运行 我的代码时,屏幕上出现的唯一数字就是我输入的数字。我如何让它工作?
When I run my code the only number that appears on the screen is the number that I entered. How do I get this to work?
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
void primefunction(int);
int main()
{
char response = 'y';
while (response == 'y')
do
{
int num;
int i;
cout << "Please enter a number that is greater than or equal to 2 \n";
cin >> num;
cout << "The number you entered was " << num << ".\n";
for (i = 2; i < num; i++);
primefunction(i);
cout << "Would you like to run the program again? \n"
<< "Enter y for yes or n for no: ";
cin >> response;
} while (response == 'Y' || response == 'y');
return 0;
}
void primefunction(int j)
{
int divisior = 2;
int stat = 0;
while (divisior < j && stat == 0)
{
if (j%divisior == 0);
stat = 1;
divisior++;
}
if (stat == 0)
cout << j;
return;
}
当我的老师在 class 复习这个例子时,我确保复制我做错的每一部分代码以改正。但是此时它只输出一个数字而不是所有质数直到插入的数字。我该如何解决这个问题?我没有向 void 函数发送正确的数字,还是我错误地调用了 void 函数?我不确定。
main函数中for语句末尾多了一个分号,"if (j%divisior == 0 )"
语句末尾也多了一个分号
删除这些,看看是否有任何改变。
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
void primefunction(int);
int main()
{
char response = 'y';
while (response == 'y')
do
{
int num;
int i;
cout << "Please enter a number that is greater than or equal to 2 \n";
cin >> num;
cout << "The number you entered was " << num << ".\n";
for (i = 2; i < num; i++);
primefunction(i);
cout << "Would you like to run the program again? \n"
<< "Enter y for yes or n for no: ";
cin >> response;
} while (response == 'Y' || response == 'y');
return 0;
}
void primefunction(int j)
{
int divisior = 2;
int stat = 0;
while (divisior < j && stat == 0)
{
if (j%divisior == 0);
stat = 1;
divisior++;
}
if (stat == 0)
cout << j;
return;
}
当我的老师在 class 复习这个例子时,我确保复制我做错的每一部分代码以改正。但是此时它只输出一个数字而不是所有质数直到插入的数字。我该如何解决这个问题?我没有向 void 函数发送正确的数字,还是我错误地调用了 void 函数?我不确定。
main函数中for语句末尾多了一个分号,"if (j%divisior == 0 )"
语句末尾也多了一个分号删除这些,看看是否有任何改变。