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

Discussion in 'C++' started by mahesh, Jun 20, 2007.

  1. mahesh

    mahesh Guest

    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
    mahesh, Jun 20, 2007
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jun 20, 2007
    #2
    1. Advertising

  3. mahesh

    Scoots Guest

    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 <> 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
    Scoots, Jun 20, 2007
    #3
  4. mahesh

    Zeppe Guest

    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
    Zeppe, Jun 20, 2007
    #4
  5. mahesh

    Scoots Guest

    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;



    }
    Scoots, Jun 20, 2007
    #5
  6. mahesh

    mahesh Guest

    On Jun 20, 12:37 pm, Scoots <> 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 <> 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.
    mahesh, Jun 20, 2007
    #6
  7. mahesh

    Scoots Guest

    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;

    }
    Scoots, Jun 20, 2007
    #7
  8. mahesh

    Scoots Guest

    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.
    Scoots, Jun 20, 2007
    #8
  9. mahesh

    Scoots Guest

    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.
    Scoots, Jun 20, 2007
    #9
  10. mahesh

    Scoots Guest

    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;
    }
    Scoots, Jun 20, 2007
    #10
  11. mahesh

    mahesh Guest

    On Jun 20, 12:43 pm, mahesh <> wrote:
    > On Jun 20, 12:37 pm, Scoots <> 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 <> 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.- 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
    mahesh, Jun 20, 2007
    #11
  12. On Wed, 20 Jun 2007 18:17:28 +0000, mahesh wrote:

    > On Jun 20, 1:09 pm, Scoots <> wrote:
    >> 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
    Obnoxious User, Jun 20, 2007
    #12
  13. mahesh

    Scoots Guest

    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?
    Scoots, Jun 20, 2007
    #13
  14. mahesh

    mahesh Guest

    On Jun 20, 1:09 pm, Scoots <> wrote:
    > 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
    mahesh, Jun 20, 2007
    #14
  15. mahesh wrote:
    > On Jun 20, 1:09 pm, Scoots <> wrote:
    >> 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
    Victor Bazarov, Jun 20, 2007
    #15
  16. mahesh wrote:
    > On Jun 20, 1:09 pm, Scoots <> wrote:
    >> 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
    John Harrison, Jun 20, 2007
    #16
  17. mahesh

    Scoots Guest

    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);
    Scoots, Jun 20, 2007
    #17
  18. mahesh

    Scoots Guest

    agreed, atof isn't the best solution, but I would like to see what
    he's getting from his cin, if anything.
    Scoots, Jun 20, 2007
    #18
  19. On 2007-06-20 19:58, mahesh wrote:
    > On Jun 20, 12:43 pm, mahesh <> wrote:
    >> On Jun 20, 12:37 pm, Scoots <> 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 <> 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.- 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
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jun 20, 2007
    #19
  20. mahesh

    shadowman Guest

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


    > 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.
    >
    shadowman, Jun 20, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,650
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,406
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    317
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    479
  5. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    442
    Jerry Coffin
    Oct 7, 2008
Loading...

Share This Page