简体   繁体   English

(c++) 循环输出的 cicle

[英](c++) cicle for output looped

Given a base and an exponent, calculate "high exponent base".给定一个底数和一个指数,计算“高指数底数”。 Be the base of that the exponent are integer values.指数是整数值的基础。 Use the "for" Input: base, exponent Output: high exponent base使用“for”输入:基数,指数输出:高指数基数

#include <iostream>
#include <stdlib.h>
#include <cmath> // Libreria necessaria per la funzione pow

using namespace std;

int main(int argc, char *argv[])
{
// Variabili 
int base, esponente, elevazione,n;

cout<<"Inserisci la base:";
cin>>base;
cout<<"Inserisci l'esponente:";
cin>>esponente;

for(int i=0;i<n;i++)
{
elevazione=pow(base,esponente);
cout<<"Il risultato è:"<<elevazione;
}
system("PAUSE");    
return 0;
}

On Linux terminal i keep getting the result like in a loop.在 Linux 终端上,我不断得到结果,就像在循环中一样。 How can i stop the cicle for and give me only one time the result?我怎样才能停止循环并只给我一次结果?

you are not initializing the n variable.您没有初始化n变量。 In this way, it can assume whatever value (say 1254445588).通过这种方式,它可以采用任何值(比如 1254445588)。 So you are printing n times the same "Il risultato è ...".所以你打印了 n 次相同的“Il risultato è ...”。

If you need to use a loop, the right solution is:如果您需要使用循环,正确的解决方案是:

int result = 1;

for (int i = 0; i < esponente; i++)
{
    result *= base;
}

std::cout << "Il risultato è: " << result << std::endl;

Get rid of n摆脱n

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

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