简体   繁体   English

控制台有输入提示,然后在一个非常简单的程序上没有 output 关闭

[英]Console has input prompt then closes without output on a very simple program

I have a very simple program that won't give any console output.我有一个非常简单的程序,它不会提供任何控制台 output。

I've tried getting input at the end using cin.get() and holding with system("pause").我尝试在最后使用 cin.get() 获取输入并使用 system("pause") 保持。 I've also tried getting input at the start of the program then outputting at the end.我还尝试在程序开始时获取输入,然后在结束时输出。

#include <iostream>
using namespace std;

int main(){
    int bulb, bulbOpen=0, multiple;

    for ( bulb=1; bulb<101 ; bulb=bulb+1 ){
        for ( multiple=1; 100; multiple++){
            if (bulb/multiple==0){
                bulb = bulb * (-1);
            }
        }
        if ( bulb<<0 ){
            bulbOpen = bulbOpen + 1;
        }
    }

    cout << "The remaining open light bulbs are " << bulbOpen << "." << endl;

    return 0;
}

I'm a beginner programmer so any help, recommendations and explanations are very welcome.我是一名初学者程序员,因此非常欢迎任何帮助、建议和解释。

The Main-Problem why you get no output is, that the code is causing an infinity-loop (The loop cant escape and will run forever) and you never reach the std::cout part of the code为什么你没有得到 output 的主要问题是,代码导致了一个无限循环(循环不能逃脱并且将永远运行)并且你永远不会到达代码的std::cout部分

Ok there's a lot going on and the first thing is (You probably will hear this a lot on this platform) don't use using namespace std;好的,发生了很多事情,第一件事是(你可能会在这个平台上听到很多)不要使用using namespace std; instead use the std:: -prefix for c++-Standard Things.而是将std:: -prefix 用于 c++-Standard Things。 I think its ok to use if you start out, but its a really bad Practice.我认为如果你开始使用它是可以的,但它是一个非常糟糕的做法。

Then another thing is, cin.get() already 'pauses' or interrupting the program until you entered an input so system("pause") really isn't needed here.然后另一件事是, cin.get()已经“暂停”或中断程序,直到您输入输入,因此这里确实不需要system("pause")

To get input simply do it like that:要获得输入,只需这样做:

int input;
std::cin >> input;
std::cout << "My output was: " << input;

Then another thing is, i dont really know what you try to do with the nested for-loops but in the second for-loop you have a conditions that doesnt really make sense然后另一件事是,我真的不知道您尝试使用嵌套的 for 循环做什么,但在第二个 for 循环中,您的条件并不真正有意义

for(multiple=1; 100; multiple++)
                ^^^

What you probably want is something like你可能想要的是类似的东西

for(multiple=1; multiple<100; multiple++)

And then saying bulb/multiple==0 doesn't really make sense either, because its only true if bulb is 0, maybe you mean bulb%multiple==0 (modulo).然后说bulb/multiple==0也没有真正的意义,因为只有当bulb 为0 时它才是正确的,也许你的意思是bulb%multiple==0 (模数)。

And there's probably a typo in one condition where you wrote bulb<<0 where you probably want to write bulb<0并且在您写bulb<<0的情况下可能有一个错字,您可能想写bulb<0

But no matter what you do, it still runs into a infinite loop, because the conditions are weird.但是无论你做什么,它仍然会陷入无限循环,因为条件很奇怪。 And in normal cases you really shouldn't change the iteration-variable of your loop inside your loop (only if you know thats exactly what you want) but in most cases that just breaks your program, especially if youre starting to learn the language.在正常情况下,您确实不应该在循环中更改循环的迭代变量(仅当您知道这正是您想要的)但在大多数情况下,这只会破坏您的程序,尤其是当您开始学习该语言时。

Maybe if you say exactly what you want, we can help you more.也许如果您确切地说出您想要什么,我们可以为您提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM