Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (1)

Powered by Vanilla. Made with Bootstrap.
Very Simple Calculator
  • #include <iostream>

    using namespace std;

    int main()
    {
    double iFirstNumber;
    double iSecondNumber;

    std::cout << \"This is very basic simple calculater \" << std::endl;

    //Prompt for first number
    std::cout << \"Enter First Number\"<< std::endl;std::cin >> iFirstNumber;

    //Prompt for Second number
    std::cout << \"Enter second number\" << std::endl;std::cin >> iSecondNumber;

    std::cout << \"PLus = \" << iFirstNumber + iSecondNumber << std::endl;
    std::cout << \"Subtract = \" << iFirstNumber - iSecondNumber << std::endl;
    std::cout << \"Divid = \" << iFirstNumber / iSecondNumber << std::endl;
    std::cout << \"Multiply = \" << iFirstNumber * iSecondNumber << std::endl;

    return 0;
    }


    Very Simple Calculator Written by me :)
  • Sh3llc0d3
    Posts: 1,910
    Nice job, Is this your first c++ program? I see a few slight error's well they're not even errors to be honest. It looks like you have used to different types of namespace conventions, globally declaring it (using namespace std) and individual declarations (std::) when only one is necessary. For a simple program such as this using a global declaration of which namespace you are using is perfectly fine. Below is an example:
    #include <iostream>

    using namespace std;

    int main()
    {
    double iFirstNumber;
    double iSecondNumber;

    cout << \"This is very basic simple calculater \" << endl;

    //Prompt for first number
    cout << \"Enter First Number\"<< endl;
    cin >> iFirstNumber;

    //Prompt for Second number
    cout << \"Enter second number\" << endl;
    cin >> iSecondNumber;

    cout << \"PLus = \" << iFirstNumber + iSecondNumber << endl;
    cout << \"Subtract = \" << iFirstNumber - iSecondNumber << endl;
    cout << \"Divid = \" << iFirstNumber / iSecondNumber << endl;
    cout << \"Multiply = \" << iFirstNumber * iSecondNumber << endl;

    return 0;
    }

    When writing more advanced programs with multiple namespaces using global namespace declarations can cause problems. For this reason you can either using explicit declarations (std::) or local declaration of namespaces in each block of code. Examples are below:
    #include <iostream>

    int main()
    {
    using namespace std;

    double iFirstNumber;
    double iSecondNumber;

    cout << \"This is very basic simple calculater \" << endl;

    //Prompt for first number
    cout << \"Enter First Number\"<< endl;
    cin >> iFirstNumber;

    /*
    snip
    */
    }

    The longest way of declaring namespaces when needed.
    #include <iostream>

    int main()
    {
    double iFirstNumber;
    double iSecondNumber;

    std::cout << \"This is very basic simple calculater \" << std::endl;

    //Prompt for first number
    std::cout << \"Enter First Number\"<< std::endl;std::cin >> iFirstNumber;

    /*
    snip
    */
    }

    Another which saves writing it out.
    #include <iostream>

    int main()
    {
    using std::cout;
    using std::cin;
    using std::endl;

    double iFirstNumber;
    double iSecondNumber;

    cout << \"This is very basic simple calculater \" << endl;

    //Prompt for first number
    cout << \"Enter First Number\"<< endl;
    cin >> iFirstNumber;

    /*
    snip
    */
    }


    Hope that hasn't confused you too much, using namespace std is fine for simple program's though, however including the std:: is not needed if you include the global namespace declaration. I'm afraid there is no short way of demonstrating it lol.
  • yes this is my first program. and from this namespace demonstration.i learnet alot Thanks :)
  • Sh3llc0d3
    Posts: 1,910
    No problem, very good effort for a first attempt :)
  • hexon
    Posts: 9


    std::cout << "Enter First Number"<< std::endl;std::cin >> iFirstNumber;



    Not a good programming style either.
  • Sh3llc0d3
    Posts: 1,910
    said:



    std::cout << "Enter First Number"<< std::endl;std::cin >> iFirstNumber;



    Not a good programming style either.



    std::cout << "Enter First Number" << std::endl;
    std::cin >> iFirstNumber;

    Correct style ;)