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

M

mahesh

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
 
V

Victor Bazarov

mahesh said:
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
 
S

Scoots

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?
 
Z

Zeppe

mahesh said:
Hi all,
I have following code that is supposed to increase the power by
specified value.

#include <iostream>
#include <fstream>
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
 
S

Scoots

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;



}
 
M

mahesh

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?





- Show quoted text -

I included #include <cstdio> as 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.
 
S

Scoots

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;

}
 
S

Scoots

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.
 
S

Scoots

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.
 
S

Scoots

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;
}
 
M

mahesh

I included #include <cstdio> as 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
 
O

Obnoxious User

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());
 
S

Scoots

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?
 
M

mahesh

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
 
V

Victor Bazarov

mahesh said:
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
 
J

John Harrison

mahesh said:
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
 
S

Scoots

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);
 
S

Scoots

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

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I included #include <cstdio> as 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.
 
S

shadowman

mahesh said:
I included #include <cstdio> as 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?
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top