what is wrong with "cout " syntax?? a trial program

B

BobR

Scoots said:
oops, had my own logic flaw up there, sorry.
Program worked as expected with:

// test.cpp : Defines the entry point for the console application.
//
// > #include "stdafx.h" // NOT standard
#include <iostream>
// > #include said:
#include <cstdlib>
> using namespace std;
#define FORWIN
int main(){ #ifdef FORWIN
system("cls");
#else
// GNU/Linux or ?
#endif // FORWIN
cout <<"Enter the base:\n"; double base( 0.0 );
cin >> base;
if( not cin || base == 0.0 ){ /* failure */ /* cin.clear();*/}
cout <<"Enter the exponent:\n"; int exponent( 0 );
cin >> exponent;
if( not cin || exponent == 0 ){ /* failure */ /* cin.clear();*/}
double new_base( base );
for( int i( 0 ); i < exponent-1 ; ++i){ file://changed this!
// > new_base = new_base * base;
new_base *= base;
} // for(i)
cout <<"The final powered output is :\n" << new_base; #ifdef FORWIN
system("PAUSE");
#else
// GNU/Linux or ?
#endif // FORWIN
 
J

James Kanze

mahesh wrote:
BTW you original program looks fine, several people have said that it
works for them. So either the code in your post is not the same as the
code you are running, or you have a seriously broken compiler. My bet
would be the former.

There may also be something in his environment that means that
his input (from cin) isn't working correctly. His original
program is NOT fine; it doesn't verify that the input has
succeeded. Anytime you read a variable, and then use that
variable before having verified that the input has succeeded,
the code is incorrect.
 
J

James Kanze

(though you are right, an single add is faster than a
single mult).

Except when it isn't. On my machine, floating point addition
and multiplication take exactly the same time, and I rather
suspect that this is true for most modern machines.
 
S

Scoots

You should see a difference on a VERY large number of operations,
particularly in say, matrix multiplication. Certainly not on the
level of exponentiation. It also depends on your processor, as
multiplication is probably frequent enough they'd have include a pipe
for it. (and yes, I know that's not the correct term).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top