简体   繁体   中英

(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

#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. How can i stop the cicle for and give me only one time the result?

you are not initializing the n variable. In this way, it can assume whatever value (say 1254445588). So you are printing n times the same "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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