473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
23 4822
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ken Varn | last post by:
Sometimes when I try to close my managed C++ application, the following dialog displays in Win 2000 Pro: The title of the dialog is "Server Busy". The message is "This action cannot be completed...
7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
6
by: Juan Romero | last post by:
Guys, Sometimes when you want to open a file, the file is locked for editing for some reason. You are able to see the contents of the file (depending on the program you are using) but you are...
15
by: David White | last post by:
The size of a struct can be affected by compiler packing. Suppose you need it to be a specific value for some reason (e.g., in firmware). How can you get the compiler to generate an error for the...
5
by: coste068 | last post by:
I am using visual studio and can't seem to figure out why I keep getting this error when I compile. Any suggestions? Here is the relevant code: #include <iostream> int main() { ..........
6
by: maya | last post by:
hi, I'm writing about generated code in a blog-sw.. (typepad..) this is from their code: <div class="module-typelist module"> as far as I know this is wrong syntax for a class-name, right...
3
by: Dean Slindee | last post by:
Need to write a standalone application that processes data once per day. Looking for the application program types available that would satisfy these requirements: Requirements: 1. Unattended...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.