在 C++ 中获取输出到控制台 window
Get output to the Console window in C++
所以我是 C++ 的新手,我希望社区可以帮助我完成作业。现在我不要求别人为我做这件事,因为我非常有能力自己做,我只是在特定部分寻求帮助。因此,我的作业涉及编写一个程序,该程序能够找到并打印 2 到 100 之间的所有质数。我必须使用双循环(这是我的教授所说的),所以我设置了一个 if 语句来运行 遍历从 2 到 100 的所有数字,并在第一个循环中进行第二个循环以确定当前数字是否为质数,然后打印它。这是我的问题发挥作用的地方,当我 运行 它时,它打开控制台 window 并很快关闭它,我看不到是否有任何东西打印到它。所以我添加了一个断点,看看它是否做到了。当我按 F5 进入每个下一步时,它 运行 循环一次然后它开始跳到不同的 windows 读取不同源文件的行(我认为它们是源文件) .最终,控制台 window 关闭时没有打印任何内容,并且它没有像应该的那样重新开始循环。我的问题是,就像在 Visual Basic 中你可以放置 console.readline() 这样必须从键盘上按下一个按钮才能继续,你如何在 C++ 中做同样的事情以便在循环后看到如果数字是素数 运行 并打印数字,程序将在打印后立即等待按下某个键?
这是我目前的代码如下,再次感谢您的帮助,我真的很感激。
#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
{
}
因为你正在使用 Visual Studio 你可以只使用 Ctrl+F5 到 运行 你的程序无需调试。这样控制台 window 在程序完成后仍然存在。
或者您可以从命令行运行程序。
或者您可以在调试器中 main
和 运行 的最后 }
处放置一个断点。
在末尾添加“停在这里”不是一个好主意。
如果你想看到输出的每一行,只需在输出语句和 运行 调试器中的程序之后放置一个断点,在 Visual Studio 按键 F5 .
顺便说一下,<stdafx.h>
不是标准的 header。它支持 Visual C++ 预编译 headers,这是一个产生相当 non-standard 预处理器行为的特性。最好在项目设置中将其关闭,并删除 >stdafx.h>
include.
还有
int _tmain(int argc, _TCHAR* argv[])
是一个愚蠢的 non-standard Microsoft-ism,曾经支持 Windows 9x,但不再用于供应商 lock-in.[=19 以外的任何目的=]
就写标准
int main()
或使用尾随 return 类型语法,
auto main() -> int
最后,而不是
!(counter%(primeCounter-1)==0)
随便写
counter%(primeCounter-1) != 0
在Visual Studio中,您实际上可以在需要暂停应用程序的地方调用system("pause");
。
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;
}
}
现在我只需要完善条件即可真正计算出素数。再次感谢您的帮助!!!!
所以我是 C++ 的新手,我希望社区可以帮助我完成作业。现在我不要求别人为我做这件事,因为我非常有能力自己做,我只是在特定部分寻求帮助。因此,我的作业涉及编写一个程序,该程序能够找到并打印 2 到 100 之间的所有质数。我必须使用双循环(这是我的教授所说的),所以我设置了一个 if 语句来运行 遍历从 2 到 100 的所有数字,并在第一个循环中进行第二个循环以确定当前数字是否为质数,然后打印它。这是我的问题发挥作用的地方,当我 运行 它时,它打开控制台 window 并很快关闭它,我看不到是否有任何东西打印到它。所以我添加了一个断点,看看它是否做到了。当我按 F5 进入每个下一步时,它 运行 循环一次然后它开始跳到不同的 windows 读取不同源文件的行(我认为它们是源文件) .最终,控制台 window 关闭时没有打印任何内容,并且它没有像应该的那样重新开始循环。我的问题是,就像在 Visual Basic 中你可以放置 console.readline() 这样必须从键盘上按下一个按钮才能继续,你如何在 C++ 中做同样的事情以便在循环后看到如果数字是素数 运行 并打印数字,程序将在打印后立即等待按下某个键?
这是我目前的代码如下,再次感谢您的帮助,我真的很感激。
#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
{
}
因为你正在使用 Visual Studio 你可以只使用 Ctrl+F5 到 运行 你的程序无需调试。这样控制台 window 在程序完成后仍然存在。
或者您可以从命令行运行程序。
或者您可以在调试器中 main
和 运行 的最后 }
处放置一个断点。
在末尾添加“停在这里”不是一个好主意。
如果你想看到输出的每一行,只需在输出语句和 运行 调试器中的程序之后放置一个断点,在 Visual Studio 按键 F5 .
顺便说一下,<stdafx.h>
不是标准的 header。它支持 Visual C++ 预编译 headers,这是一个产生相当 non-standard 预处理器行为的特性。最好在项目设置中将其关闭,并删除 >stdafx.h>
include.
还有
int _tmain(int argc, _TCHAR* argv[])
是一个愚蠢的 non-standard Microsoft-ism,曾经支持 Windows 9x,但不再用于供应商 lock-in.[=19 以外的任何目的=]
就写标准
int main()
或使用尾随 return 类型语法,
auto main() -> int
最后,而不是
!(counter%(primeCounter-1)==0)
随便写
counter%(primeCounter-1) != 0
在Visual Studio中,您实际上可以在需要暂停应用程序的地方调用system("pause");
。
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;
}
}
现在我只需要完善条件即可真正计算出素数。再次感谢您的帮助!!!!