By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hi all,
I have following code that is supposed to increase the power by
specified value.
int main()
{

system("cls");
int i, exponent;
double base;
double new_base=0.0;
ofstream powerfile("power.txt",ios::app);
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
for (i=0; i< exponent ; i++)
{
new_base = new_base + base;

}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
return 0;

}
but the output that shows up in the screen is:
The final powered output is :
3.32222e-31204 (some weird value).
for every input.
What could be the reason for that??
Thanks in advance

Jun 20 '07 #1
Share this Question
Share on Google+
23 Replies


P: n/a
mahesh wrote:
Hi all,
I have following code that is supposed to increase the power by
specified value.
Seems some includes are missing here....
int main()
{

system("cls");
int i, exponent;
double base;
You better initialise the variables you attempt to read. If you
don't, their value will stay indeterminate if reading fails.
double new_base=0.0;
ofstream powerfile("power.txt",ios::app);
What's that for? You don't seem to be using it...
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
Are you sure the values are what you enter?
for (i=0; i< exponent ; i++)
{
new_base = new_base + base;

}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
return 0;

}
but the output that shows up in the screen is:
The final powered output is :
3.32222e-31204 (some weird value).
for every input.
What could be the reason for that??
The program apparently fails to read your value, in which case
'base' variable stays UNinitialised. Please initialise it to
something and verify that the value you read is different from
what you initialise it to (and valid for your calculations).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 20 '07 #2

P: n/a
A couple points.

Shouldn't:
new_base = new_base + base be new_base = new_base * base

(just a suggestion)
Also, what inputs have you tried it with? A large negative exponent
like that is often a sign of what is known as floating point
imprecision. Generally, you'll only notice this around 0, and it
comes from the ability of the floating point to show very large
numbers or very very small. Think scientific numbers. An operation
that it expects to be a zero doesn't necessarily actually end up at
zero.

Final question though, you DO have stdio included, right?


On Jun 20, 1:20 pm, mahesh <maheshr...@gmail.comwrote:
Hi all,
I have following code that is supposed to increase the power by
specified value.
int main()
{

system("cls");
int i, exponent;
double base;
double new_base=0.0;
ofstream powerfile("power.txt",ios::app);
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
for (i=0; i< exponent ; i++)
{
new_base = new_base + base;

}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
return 0;

}

but the output that shows up in the screen is:
The final powered output is :
3.32222e-31204 (some weird value).
for every input.
What could be the reason for that??
Thanks in advance

Jun 20 '07 #3

P: n/a
mahesh wrote:
Hi all,
I have following code that is supposed to increase the power by
specified value.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{

system("cls");
this is windows only.
int i, exponent;
double base;
double new_base=0.0;
ofstream powerfile("power.txt",ios::app);
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
for (i=0; i< exponent ; i++)
ni c++, it's better to declare
for(int i = 0; ...
and remove the int i at the beginning of the main body.
{
new_base = new_base + base;
It should be a multiplication (with new_base initialized to 1.0),
shouldn't it?
>
}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
again, it's only windows
return 0;

}
but the output that shows up in the screen is:
The final powered output is :
3.32222e-31204 (some weird value).
for every input.
In my system it works fine. Maybe some iostream issue. You can try to
add cin.sync() after each cin >>, in order to flush the garbage (\n)
from the buffer.
What could be the reason for that??
Thanks in advance
Regards,

Zeppe
Jun 20 '07 #4

P: n/a
you got it zeppe.

He's not initializing new_base, so he's seeing a garbage value
exponentiated. (or in this case, added to).
Try this:

int main()
{

system("cls");
int i, exponent;
double base;
double new_base=0.0;
ofstream powerfile("power.txt",ios::app);
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
new_base=base; //added
for (i=0; i< exponent ; i++)
{
new_base = new_base * base;
}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
return 0;

}

Jun 20 '07 #5

P: n/a
On Jun 20, 12:37 pm, Scoots <bssalm...@traxcorp.comwrote:
A couple points.

Shouldn't:
new_base = new_base + base be new_base = new_base * base

(just a suggestion)
Also, what inputs have you tried it with? A large negative exponent
like that is often a sign of what is known as floating point
imprecision. Generally, you'll only notice this around 0, and it
comes from the ability of the floating point to show very large
numbers or very very small. Think scientific numbers. An operation
that it expects to be a zero doesn't necessarily actually end up at
zero.

Final question though, you DO have stdio included, right?

On Jun 20, 1:20 pm, mahesh <maheshr...@gmail.comwrote:
Hi all,
I have following code that is supposed to increase the power by
specified value.
int main()
{
system("cls");
int i, exponent;
double base;
double new_base=0.0;
ofstream powerfile("power.txt",ios::app);
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
for (i=0; i< exponent ; i++)
{
new_base = new_base + base;
}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
return 0;
}
but the output that shows up in the screen is:
The final powered output is :
3.32222e-31204 (some weird value).
for every input.
What could be the reason for that??
Thanks in advance- Hide quoted text -

- Show quoted text -
I included #include <cstdioas a header and all the required header
file.
I wanted to use addition rather than multiplication because addition
takes less time then multiplication.
I tried with 2 as base and 2 as exponent but output was like i
mentioned above rather than 4.
any more guidence on this.

Jun 20 '07 #6

P: n/a
you got it zeppe.

He's not initializing new_base to base, so he's working with zero
anyway.
Try this:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdio>
using namespace std;
int main()
{
system("cls");
int i, exponent;
double base;
double new_base=0.0;
ofstream powerfile("power.txt",ios::app);
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
new_base=base; //added this!
for (i=0; i< exponent ; i++)
{
new_base = new_base * base;
}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
return 0;

}

Jun 20 '07 #7

P: n/a
Addition may be faster than multiplication, but your logic isn't
exponentiating. It's multiplying.
for (i=0; i< exponent ; i++)
{
for (int j=0;j<base;j++){
new_base = new_base * base;
}

}
would be exponentiation. And it is MUCH slower to do that many
iterations (though you are right, an single add is faster than a
single mult). Why don't you check your values with math.pow(double,
double).

Still, try my above suggestion along with initializing the values, and
it might help.
Jun 20 '07 #8

P: n/a
Addition may be faster than multiplication, but your logic isn't
exponentiating. It's multiplying.

for (i=0; i< exponent ; i++)
{
for (int j=0;j<base;j++){
new_base = new_base + base;
}
}
would be exponentiation. And it is MUCH slower to do that many
iterations (though you are right, an single add is faster than a
single mult). Why don't you check your values with math.pow(double,
double).
Still, try my above suggestion along with initializing the values,
and
it might help.

Jun 20 '07 #9

P: n/a
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"
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
system("cls");
int i, exponent;
double base;
double new_base=0.0;
// ofstream powerfile("power.txt",ios::app);
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
new_base=base; //added this!
for (i=0; i< exponent-1 ; i++) //changed this!
{
new_base = new_base * base;
}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
return 0;
}
Jun 20 '07 #10

P: n/a
On Jun 20, 12:43 pm, mahesh <maheshr...@gmail.comwrote:
On Jun 20, 12:37 pm, Scoots <bssalm...@traxcorp.comwrote:


A couple points.
Shouldn't:
new_base = new_base + base be new_base = new_base * base
(just a suggestion)
Also, what inputs have you tried it with? A large negative exponent
like that is often a sign of what is known as floating point
imprecision. Generally, you'll only notice this around 0, and it
comes from the ability of the floating point to show very large
numbers or very very small. Think scientific numbers. An operation
that it expects to be a zero doesn't necessarily actually end up at
zero.
Final question though, you DO have stdio included, right?
On Jun 20, 1:20 pm, mahesh <maheshr...@gmail.comwrote:
Hi all,
I have following code that is supposed to increase the power by
specified value.
int main()
{
system("cls");
int i, exponent;
double base;
double new_base=0.0;
ofstream powerfile("power.txt",ios::app);
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
for (i=0; i< exponent ; i++)
{
new_base = new_base + base;
}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
return 0;
}
but the output that shows up in the screen is:
The final powered output is :
3.32222e-31204 (some weird value).
for every input.
What could be the reason for that??
Thanks in advance- Hide quoted text -
- Show quoted text -

I included #include <cstdioas a header and all the required header
file.
I wanted to use addition rather than multiplication because addition
takes less time then multiplication.
I tried with 2 as base and 2 as exponent but output was like i
mentioned above rather than 4.
any more guidence on this.- Hide quoted text -

- Show quoted text -
After the suggested changes here is how my program looks:
int main(int argc, char *argv[])
{
// clrscr();
//system("cls");
int exponent;
double base;
double new_base=1.0;
ofstream powerfile("power.txt",ios::app);
cout<<"Enter the base:\n";

cin >base;
cin.sync();
powerfile <<"the base u entered is "<< base<<endl;
cout <<"Enter the exponent:\n";
cin >exponent;
cin.sync();
powerfile<<"the exponent u entered is "<< exponent<<endl;

for (int i=0; i< exponent ; i++)
{
new_base = new_base* base;
}
powerfile <<"The final powered output is :\n" <<
new_base<<endl;
//powerfile << new_exponent <<endl;
//cin.get();
system("PAUSE");

return 0;

}


the out put still I have is :

the base u entered is 3.60739e-313
the exponent u entered is 2
The final powered output is :
3.60739e-313

Jun 20 '07 #11

P: n/a
my output was:

the base u entered is 2
the exponent u entered is 2
The final powered output is :
4
It's definately an input fault. Do you get the same fault if you
bring it in as a string and atof it?

Jun 20 '07 #12

P: n/a
On Jun 20, 1:09 pm, Scoots <bssalm...@traxcorp.comwrote:
my output was:

the base u entered is 2
the exponent u entered is 2
The final powered output is :
4

It's definately an input fault. Do you get the same fault if you
bring it in as a string and atof it?
I tried to accept the base as string and change it using atof(base)
but it is giving me error..as
" cannot convert `std::string' to `const char*' for argument `1' to
`double atof(const char*)'"
How can I resolve it?
regards
Mahesh

Jun 20 '07 #13

P: n/a
mahesh wrote:
On Jun 20, 1:09 pm, Scoots <bssalm...@traxcorp.comwrote:
>my output was:

the base u entered is 2
the exponent u entered is 2
The final powered output is :
4

It's definately an input fault. Do you get the same fault if you
bring it in as a string and atof it?

I tried to accept the base as string and change it using atof(base)
but it is giving me error..as
" cannot convert `std::string' to `const char*' for argument `1' to
`double atof(const char*)'"
How can I resolve it?
regards
Mahesh
atof(base.c_str());

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.

john
Jun 20 '07 #14

P: n/a
On Wed, 20 Jun 2007 18:17:28 +0000, mahesh wrote:
On Jun 20, 1:09 pm, Scoots <bssalm...@traxcorp.comwrote:
>my output was:

the base u entered is 2
the exponent u entered is 2
The final powered output is :
4

It's definately an input fault. Do you get the same fault if you
bring it in as a string and atof it?

I tried to accept the base as string and change it using atof(base)
but it is giving me error..as
" cannot convert `std::string' to `const char*' for argument `1' to
`double atof(const char*)'"
How can I resolve it?
atof(base.c_str());

--
Obnoxious User
Jun 20 '07 #15

P: n/a
mahesh wrote:
On Jun 20, 1:09 pm, Scoots <bssalm...@traxcorp.comwrote:
>my output was:

the base u entered is 2
the exponent u entered is 2
The final powered output is :
4

It's definately an input fault. Do you get the same fault if you
bring it in as a string and atof it?

I tried to accept the base as string and change it using atof(base)
but it is giving me error..as
" cannot convert `std::string' to `const char*' for argument `1' to
`double atof(const char*)'"
If you *must* use atof, pass 'base.c_str()' to it, not 'base'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 20 '07 #16

P: n/a
use a char* instead of a string, sorry. Just make a char base[100] or
something for the test.

It should crash horribly on the conversion from char* to double, but I
want to see what it's getting INSTEAD of the number.

from the msdn:
char *s;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
so you could probably do
char s[100]
cin>>s
base=atof(s);

Jun 20 '07 #17

P: n/a
agreed, atof isn't the best solution, but I would like to see what
he's getting from his cin, if anything.

Jun 20 '07 #18

P: n/a
On 2007-06-20 19:58, mahesh wrote:
On Jun 20, 12:43 pm, mahesh <maheshr...@gmail.comwrote:
>On Jun 20, 12:37 pm, Scoots <bssalm...@traxcorp.comwrote:


A couple points.
Shouldn't:
new_base = new_base + base be new_base = new_base * base
(just a suggestion)
Also, what inputs have you tried it with? A large negative exponent
like that is often a sign of what is known as floating point
imprecision. Generally, you'll only notice this around 0, and it
comes from the ability of the floating point to show very large
numbers or very very small. Think scientific numbers. An operation
that it expects to be a zero doesn't necessarily actually end up at
zero.
Final question though, you DO have stdio included, right?
On Jun 20, 1:20 pm, mahesh <maheshr...@gmail.comwrote:
Hi all,
I have following code that is supposed to increase the power by
specified value.
int main()
{
system("cls");
int i, exponent;
double base;
double new_base=0.0;
ofstream powerfile("power.txt",ios::app);
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
for (i=0; i< exponent ; i++)
{
new_base = new_base + base;
}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
return 0;
}
but the output that shows up in the screen is:
The final powered output is :
3.32222e-31204 (some weird value).
for every input.
What could be the reason for that??
Thanks in advance- Hide quoted text -
- Show quoted text -

I included #include <cstdioas a header and all the required header
file.
I wanted to use addition rather than multiplication because addition
takes less time then multiplication.
I tried with 2 as base and 2 as exponent but output was like i
mentioned above rather than 4.
any more guidence on this.- Hide quoted text -

- Show quoted text -

After the suggested changes here is how my program looks:
int main(int argc, char *argv[])
{
// clrscr();
//system("cls");
int exponent;
double base;
double new_base=1.0;
ofstream powerfile("power.txt",ios::app);
cout<<"Enter the base:\n";

cin >base;
cin.sync();
powerfile <<"the base u entered is "<< base<<endl;
cout <<"Enter the exponent:\n";
cin >exponent;
cin.sync();
powerfile<<"the exponent u entered is "<< exponent<<endl;

for (int i=0; i< exponent ; i++)
{
new_base = new_base* base;
}
powerfile <<"The final powered output is :\n" <<
new_base<<endl;
//powerfile << new_exponent <<endl;
//cin.get();
system("PAUSE");

return 0;

}
#include <iostream>

int main()
{
int exponent;
double base;
double value = 1.0;
std::cout<< "Enter the base: ";

std::cin >base;
std::cout << "The base you entered is: "
<< base << std::endl;
std::cout << "Enter the exponent: ";
std::cin >exponent;
std::cout<< "The exponent you entered is: "
<< exponent << std::endl;

for (int i=0; i< exponent ; i++)
{
value = value * base;
}
std::cout << "\nThe value is: " << value
<< std::endl;
return 0;
}

This code works for me, if you can not copy-paste that, compile, and run
correctly you have a problem with your compiler.

--
Erik Wikström
Jun 20 '07 #19

P: n/a
mahesh wrote:
>>
>>Hi all,
I have following code that is supposed to increase the power by
specified value.
int main()
{
system("cls");
int i, exponent;
double base;
double new_base=0.0;
ofstream powerfile("power.txt",ios::app);
cout <<"Enter the base:\n";
cin >base;
cout <<"Enter the exponent:\n";
cin >exponent;
//cout << base;
for (i=0; i< exponent ; i++)
{
new_base = new_base + base;
}
cout <<"The final powered output is :\n" << new_base;
system("PAUSE");
return 0;
}
but the output that shows up in the screen is:
The final powered output is :
3.32222e-31204 (some weird value).
for every input.
What could be the reason for that??
Thanks in advance- Hide quoted text -
- Show quoted text -

I included #include <cstdioas a header and all the required header
file.
I wanted to use addition rather than multiplication because addition
takes less time then multiplication.
WTF? How does that matter if it yields an incorrect result? Are you
telling me you have a machine that runs so slowly that you need to
change multiplication to addition for debugging purposes?

I tried with 2 as base and 2 as exponent but output was like i
mentioned above rather than 4.
any more guidence on this.

Jun 20 '07 #20

P: n/a

Scoots <bs*******@traxcorp.comwrote in message...
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 <fstream// unused
#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
return 0;
} // main()
--
Bob <GR
POVrookie
Jun 20 '07 #21

P: n/a
On Jun 20, 8:22 pm, John Harrison <john_androni...@hotmail.comwrote:
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.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 21 '07 #22

P: n/a
On Jun 20, 7:50 pm, Scoots <bssalm...@traxcorp.comwrote:
(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.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 21 '07 #23

P: n/a
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).

Jun 21 '07 #24

This discussion thread is closed

Replies have been disabled for this discussion.