I need help on "for" statement .Im doing exponents. I did a while statement already in which i checked if base is in range. help please.

for(int count = 0; count <=Expo; count++)
              Total1 = (dont know what this should be?) 
              cout << setw(45) << "Total  : " << setw(2) << Total1     << endl;

Edited 6 Years Ago by nickx522: n/a

ok in my code i need to do exponents...get first number as base then i need to get the second number as exponent and multiply the base as many times as the exponent.

//local constants
    const int SENTINEL = -1;
    
    //local variables
    int Base;                                //Base
    int Expo;                                //Exponent 
    int Total;                               //Total
    int Total1;                              //Calculations
    
    /**************************start main program*********************/
   
   //Get total of numbers to be inputted
   
   cout << "\n\n";
   cout << setw(50) << "--------------" << endl;
   cout << setw(50) << " Calculations " << endl;
   cout << setw(50) << "--------------" << endl;
   cout << "\n\n";
   cout << setw(55) << " Input Base (or -1 to Quit):  ";
   cin  >> Base;
   
   while( Base != SENTINEL)
   {
          if( Base <=10 && Base >=1)
          {
              
              cout << setw(55) << " Input Exponent:  ";
              cin  >> Expo;
              }
          else
              cout << " Base Error";
              
              
              if ( Expo <=10 && Expo >=0 )
              
                   {
              Total1 = 1;
              for(int count = 0; count < Expo; count++)
              Total1 = Total1 * Base;
              cout << setw(45) << "Total  : " << setw(2) << Total1     << endl;
              }
              else 
                  cout << " Exponent Error";

}
    

}   //End main program

thats what i got so far but its not working. it keeps on asking for another exponent when it should just display the total. and when the numbers are out of range the program goes crazy and writes out error error error on the whole screen. help please.

umm can you just help me find the problem..i am going to space everything out later...that doesnt effect the program. i need this as soon as possible. please

Reformat... That's one of the most important parts of programming.

Rather than waiting 14 hours you could have done it in 5 minutes,

Edited 6 Years Ago by WaltP: n/a

The point about reformatting is that you can spot mistakes in your code much more easily when the indentation is done properly. As a result, most programmers that have more than a few months of experience in coding are already fanatical about respecting format. It certainly won't magically fix any problems because the compiler ignores it, but it helps you help yourself.

As for your code (properly formatted):

int main() {

  //local constants
  const int SENTINEL = -1;
    
  //local variables
  int Base;                                //Base
  int Expo;                                //Exponent 
  int Total;                               //Total
  int Total1;                              //Calculations
    
  /**************************start main program*********************/
   
  //Get total of numbers to be inputted
   
  cout << "\n\n";
  cout << setw(50) << "--------------" << endl;
  cout << setw(50) << " Calculations " << endl;
  cout << setw(50) << "--------------" << endl;
  cout << "\n\n";
  cout << setw(55) << " Input Base (or -1 to Quit):  ";
  cin  >> Base; //notice that you set Base here, and ONLY HERE.
   
  while( Base != SENTINEL) //if you enter this loop, you will never get out because Base never changes in the loop.
  {
    if( Base <=10 && Base >=1)
    {
      cout << setw(55) << " Input Exponent:  ";
      cin  >> Expo;
    }
    else
      cout << " Base Error";
    //Isn't it strange that even with an invalid base you keep on going to the exponent?
 
    if ( Expo <=10 && Expo >=0 ) //What is the value of Expo if you entered an invalid Base?
    {
      Total1 = 1;
      for(int count = 0; count < Expo; count++)
        Total1 = Total1 * Base;
      cout << setw(45) << "Total  : " << setw(2) << Total1 << endl; //width of only 2 for Total1?
    }
    else 
      cout << " Exponent Error";

  } //this loop is infinite!

  return 0;
}   //End main program

Reformatting helps you see things like logic errors with conditionals and loops because you _see_ what the program executes.

I see. You don't understand the internet and how links work...

Try reformatting your code correctly and you will probably notice a problem. See this

See that underlined text? That is a link. You are able to click on it and a new page will be displayed that describes formatting techniques. Apply those techniques to your source code.

This article has been dead for over six months. Start a new discussion instead.