在C+中获取控制台窗口的输出+; 所以我对C++很陌生,希望社区能帮我做作业。现在我不是要求别人帮我做这件事,因为我很有能力自己做这件事,我只是在某个特定的方面寻求帮助。所以,我的任务是制作一个程序,可以找到并打印2到100之间的所有素数。我必须使用一个双循环(这是我教授说的),所以我设置了一个if语句来遍历从2到100的所有数字,并在第一个循环中有第二个循环来确定当前数字是否为素数,然后打印它。这里是我的问题发挥作用的地方,当我运行它时,它会打开控制台窗口并快速关闭,我看不到是否有任何东西打印到它。所以我添加了一个断点,看看它是否有。当我按F5键进入下一步时,它会在循环中运行一次,然后开始跳转到不同的窗口,读取不同源文件的行(我认为它们是源文件)。最终控制台窗口关闭,没有打印任何内容,并且不会像应该的那样再次启动循环。我的问题是,像VisualBasic中你可以放控制台。RealLoad(),所以必须从键盘按住一个按钮,以便继续,C++中你怎么做同样的操作,这样在循环之后查看数字是不是原色Run并打印了这个数字,程序会等待一个键在打印之后被按下?

在C+中获取控制台窗口的输出+; 所以我对C++很陌生,希望社区能帮我做作业。现在我不是要求别人帮我做这件事,因为我很有能力自己做这件事,我只是在某个特定的方面寻求帮助。所以,我的任务是制作一个程序,可以找到并打印2到100之间的所有素数。我必须使用一个双循环(这是我教授说的),所以我设置了一个if语句来遍历从2到100的所有数字,并在第一个循环中有第二个循环来确定当前数字是否为素数,然后打印它。这里是我的问题发挥作用的地方,当我运行它时,它会打开控制台窗口并快速关闭,我看不到是否有任何东西打印到它。所以我添加了一个断点,看看它是否有。当我按F5键进入下一步时,它会在循环中运行一次,然后开始跳转到不同的窗口,读取不同源文件的行(我认为它们是源文件)。最终控制台窗口关闭,没有打印任何内容,并且不会像应该的那样再次启动循环。我的问题是,像VisualBasic中你可以放控制台。RealLoad(),所以必须从键盘按住一个按钮,以便继续,C++中你怎么做同样的操作,这样在循环之后查看数字是不是原色Run并打印了这个数字,程序会等待一个键在打印之后被按下?,c++,loops,if-statement,file-io,nested-loops,C++,Loops,If Statement,File Io,Nested Loops,下面是我当前的代码,再次感谢您的帮助,我非常感谢 #include "stdafx.h" #include<iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int counter=2; int primeCounter=counter; //two variables, one to keep track of the number we are one, the other to che

下面是我当前的代码,再次感谢您的帮助,我非常感谢

#include "stdafx.h"
#include<iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int counter=2;
int primeCounter=counter;
//two variables, one to keep track of the number we are one, the other to check to see if its prime.
if(counter<101)
{//I want the counter to increment by 1 everytime the loops runs until it gets to 100.
    if(!(counter%(primeCounter-1)==0))//if the counter has a remainer, then it is prime
    {//each time the main loop run i want this loop to run too so it can check if it is a prime number and then print it.

        cout<<counter<<endl;
    //if this was VB, here is where i would want to have my console.Readline()
    }
    else
    {

    }
    counter+=1;
}
else
{

}   
#包括“stdafx.h”
#包括
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
int计数器=2;
int primeCounter=计数器;
//两个变量,一个用来跟踪我们是一的数字,另一个用来检查它的素数。

如果(counter由于您使用的是Visual Studio,您可以只使用Ctrl+F5运行程序而不进行调试。这样,在程序完成后,控制台窗口将保持不变

或者,您可以从命令行运行该程序

或者,您可以在
main
的最后一个
}
上设置断点,并在调试器中运行它

在结尾加上“到此为止”不是一个好主意


如果希望在生成输出的每一行时都看到它,只需在output语句后放置一个断点,然后在Visual Studio按键F5中的调试器中运行该程序


在传递过程中,<代码>不是标准的头文件。它支持VisualC++预编译头,这是一个产生非标准预处理器行为的特性。最好在项目设置中关闭它,并删除<代码> STDAFX。


是一个愚蠢的非标准微软ism,一度支持Windows9x,但除了供应商锁定之外,不再有任何其他用途

只需编写标准

int main()
或使用尾部返回类型语法

auto main() -> int

最后,不是

!(counter%(primeCounter-1)==0)
只要写

counter%(primeCounter-1) != 0

在Visual Studio中,您实际上可以在需要挂起应用程序的位置调用
系统(“暂停”);

cin.get()将执行您想要的操作。

因此我解决了我的问题。首先,循环没有运行,因为第一个if语句没有循环。我将此更改为一段时间,现在输出工作起来就像一个符咒。请看

#include "stdafx.h"
#include<iostream>

using namespace std;

int main()
{
int counter=2;
int primeCounter=counter;
//two variables, one to keep track of the number we are one, the other to check to see if its prime.
while(counter<101)
{//I want the counter to increment by 1 everytime the loops runs until it gets to 100.
    if((counter%(primeCounter-1)==0))//if the counter has a remainer, then it is prime
    {//each time the main loop run i want this loop to run too so it can check if it is a prime number and then print it.



    }
    else
    {
        cout<<counter<<endl;
    }
    counter+=1;
    primeCounter=counter;
}


}
#包括“stdafx.h”
#包括
使用名称空间std;
int main()
{
int计数器=2;
int primeCounter=计数器;
//两个变量,一个用来跟踪我们是一的数字,另一个用来检查它的素数。

虽然(这里是我的问题发挥作用的地方,当我运行它时,它会打开控制台窗口并快速关闭,我看不到是否有任何东西打印到它。我可以告诉你,关于这个部分,这里有100多个重复的问题。@drescherjm。我问这个问题显然是因为它们对我有用。我已经讨论这个问题2个半小时了It’It’It’It’’It’’It’’It’’It’’It’’It’’It’’It’’It’’It’’It’It’’It’’It’It’It’’It’It’It’It’It@Cheers and Hth.-Alf topicstarter真的需要这个例子是可移植的吗?我不这么认为。这只是实现结果的最简单的方法之一,我认为没有理由避免在特定任务中使用它。让我们尽量简单。不,这不是最简单的方法之一。简单的方法是Ctrl+F5。是的,OP需要学习在默认情况下编写便携代码。可移植的代码更短和简单。这个答案是一种反模式,一种已知的不好的做事方式。@阿尔夫。默认情况下,OP应该考虑自己在目标任务中更需要哪些工具。模式是很好的,但不一定是好的。我不知道。萨格利,但这个问题确实特别问了如何让他的程序等待按键,我相信我已经回答了。回答一个关于如何使用焊工手电筒点燃蜡烛的问题是可以的,答案还包括关于使用打火机或火柴来完成工作的建议,以及关于使用手电筒而不是火柴的建议蜡烛(鉴于它显然不是出于浪漫的原因而使用)。当我使用CTRL+F5组合键时,窗口会打开,说“按任意键继续”,然后在我按了一个键后它会关闭,但屏幕上没有显示任何内容。虽然main最后一个括号处的断点是个好主意。我现在可以看到控制台窗口中没有打印任何内容。您知道“CoutAny数模1为0。因此,对于当前代码,不会输出任何内容。;-)
#include "stdafx.h"
#include<iostream>

using namespace std;

int main()
{
int counter=2;
int primeCounter=counter;
//two variables, one to keep track of the number we are one, the other to check to see if its prime.
while(counter<101)
{//I want the counter to increment by 1 everytime the loops runs until it gets to 100.
    if((counter%(primeCounter-1)==0))//if the counter has a remainer, then it is prime
    {//each time the main loop run i want this loop to run too so it can check if it is a prime number and then print it.



    }
    else
    {
        cout<<counter<<endl;
    }
    counter+=1;
    primeCounter=counter;
}


}