Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > what is wrong with "cout " syntax?? a trial program

Reply
Thread Tools

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

 
 
mahesh
Guest
Posts: n/a
 
      06-20-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-20-2007
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


 
Reply With Quote
 
 
 
 
Scoots
Guest
Posts: n/a
 
      06-20-2007
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 <(E-Mail Removed)> 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



 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      06-20-2007
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
 
Reply With Quote
 
Scoots
Guest
Posts: n/a
 
      06-20-2007
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;



}

 
Reply With Quote
 
mahesh
Guest
Posts: n/a
 
      06-20-2007
On Jun 20, 12:37 pm, Scoots <(E-Mail Removed)> wrote:
> 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 <(E-Mail Removed)> 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 <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.

 
Reply With Quote
 
Scoots
Guest
Posts: n/a
 
      06-20-2007
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;

}

 
Reply With Quote
 
Scoots
Guest
Posts: n/a
 
      06-20-2007
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.


 
Reply With Quote
 
Scoots
Guest
Posts: n/a
 
      06-20-2007
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.



 
Reply With Quote
 
Scoots
Guest
Posts: n/a
 
      06-20-2007
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;
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trial Program TO AVOID ballbrit Computer Support 1 09-12-2007 04:47 PM
veRified: Interesting FACTORISE program, by trial division. + Fermat s method.Casio fx-82 MS schools sci-calc. don.lotto@paradise.net.nz NZ Computing 7 07-26-2007 06:26 AM
How to upgrade trial version to non-trial version w/o formatting =?Utf-8?B?WFB4NjQ=?= Windows 64bit 2 02-22-2007 06:27 AM
Trial key for All windows program. =?Utf-8?B?S292YWxlbmtvIE9sZWc=?= Microsoft Certification 0 11-13-2006 02:24 PM
Add/Remove Program Glitch: Asks If I Want To To Remove Wrong Program ? Robert11 Computer Support 6 08-02-2004 09:02 PM



Advertisments