Thread: Square Root Problem.

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    21

    Question Square Root Problem.

    Hello,
    I have written a program that allows the user to choose a calculator function, then enter numbers and then it gives the answer.
    All is working, except for the square roots function.
    Below is my program code, and then below that, there is my errors list.
    I am stuck, so I was wondering if anyone cold help me out here.
    Thank you:

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    using namespace std;
    
    int main ()
    {
    string response ="n";
    
    int function;
    double x;
    double y;
    int i;
    i = 0;
    
    do
    {
    cout << "Welcome" << endl;
    cout << "Choose one of the following functions:" << endl;
    cout << "1. Addition" << endl;
    cout << "2. Subtraction" << endl;
    cout << "3. Multiplication" << endl;
    cout << "4. Division" << endl;
    cout << "5. Powers" << endl;
    cout << "6. Square Roots" << endl;
    cout << "7. Sine" << endl;
    cout << "8. Cosine" << endl;
    cout << "9. Tangent" << endl;
    cout << "Enter number to choose function:" << endl;
    cin >> function;
    
            if (function == 1)
            {
            cout << "Addition Function." << endl;
            cout << "Enter two numbers to find their sum." << endl;
            cout << "Enter First Number:" << endl;
            cin >> x;
            cout << "Enter Second Number:" << endl;
            cin >> y;
                    for (int i = 0; i < 1; i++)
                    {
                    cout << x << "+" << y << "=" << x + y << endl;
                    }
            }
    
    
            if (function == 2)
            {
            cout << "Subtraction Function." << endl;
            cout << "Enter two numbers to find their difference." << endl;
            cout << "Enter First Number:" << endl;
            cin >> x;
            cout << "Enter Second Number:" << endl;
            cin >> y;
                    for (int i = 0; i < 1; i++)
                    {
                    cout << x << "-" << y << "=" << x - y << endl;
                    }
            }
    
    
            if (function == 3)
            {
            cout << "Multiplication Function." << endl;
            cout << "Enter two numbers to find their product." << endl;
            cout << "Enter First Number:" << endl;
            cin >> x;
            cout << "Enter Second Number:" << endl;
            cin >> y;
                    for (int i = 0; i < 1; i++)
                    {
                    cout << x << "*" << y << "=" << x * y << endl;
                    }
            }
    
            if (function == 4)
            {
            cout << "Division Function." << endl;
            cout << "Enter two numbers to find their Quotient." << endl;
    
                    do
                    {
                    cout << "Enter First Number:" << endl;
                    cin >> x;
                    cout << "Enter Second Number:" << endl;
                    cin >> y;
    
                            if (x != 0 && y != 0)
                            {
                                    for (int i = 0; i < 1; i++)
                                    {
                                    cout << x << "/" << y << "=" << x / y << endl;
                                    }
                            }
    
                            if (x == 0 || y == 0)
                            {
                            cout << " " << endl;
                            cout << "Numbers can not be 0" << endl;
                            cout << " " << endl;
                            }
    
                    }
                    while (x == 0 || y == 0);
            }
    
            if (function == 5)
            {
            cout << "Powers Function." << endl;
            cout << "Enter First Number:" << endl;
            cin >> x;
            cout << "Enter Second Number:" << endl;
            cin >> y;
                    for (int i = 0; i < 1; i++)
                    {
                    cout << x << "^" << y << "=" << std::pow(x,(float)y) << endl;
                    }
            }
    
            if (function == 6)
            {
            cout << "Square Root Function." << endl;
    
                    do
                    {
                    cout << "Enter Number:" << endl;
                    cin >> x;
    
                            if (x > 0)
                            {
                                    for (int i = 0; i < 1; i++)
                                    {
                                    cout << "Square Root of " << x << " = " << std::sqrt(x) << endl;
                                    }
                            }
    
                            if (x < 0)
                            {
                                    cout << " " << endl;
                                    cout << "Number can not be negative" << endl;
                                    cout << " " < endl;
                            }
                    }
                    while (x < 0);
            }
    
            if (function == 7)
            {
            cout << "Sine Function." << endl;
            cout << "Enter Angle Measurement:" << endl;
            cin >> x;
                    for (int i = 0; i < 1; i++)
                    {
                    cout << "Sine of " << x << " = " << std::sin(x) << endl;
                    }
            }
    
            if (function == 8)
            {
            cout << "Cosine Function." << endl;
            cout << "Enter Angle Measurement:" << endl;
            cin >> x;
                    for (int i = 0; i < 1; i++)
                    {
                    cout << "Cosine of " << x << " = " << std::cos(x) << endl;
                    }
            }
    
            if (function == 9)
            {
            cout << "Tangent Function." << endl;
            cout << "Enter Angle Measurement:" << endl;
            cin >> x;
                    for (int i = 0; i < 1; i++)
                    {
                    cout << "Tangent of " << x << " = " << std::tan(x) << endl;
                    }
            }
    cout << " " << endl;
    cout << "Do you want to do this again? [enter y for yes]" << endl;
    cin >> response;
    }
    
    while (response == "Y" || response == "Yes" || response == "y" || response == "yes");
    
    }
    Here is the Errors that I receive:
    Code:
    newCalc.cpp: In function `int main()':
    newCalc.cpp:140: no match for `ostream & < ostream & (&)(ostream &)'
    newCalc.cpp:140: candidates are: operator <(ostream & (*)(ostream &), ostream & (*)(ostream &)) <builtin>
    newCalc.cpp:140:                 operator <(void *, void *) <builtin>
    I am not sure what to do now, this is the only problem and then the program is done.
    If anyone thinks they could help me out, it would greatly appreciated!
    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where is line 140?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    21

    To Clear it up a bit.

    I should be a bit more clear, I am trying to make the square root function not allow negative numbers, for if the user enters a negative number, then they are told not to do so, and they are allowed to try again.
    Hopefully that will help!
    Thank you so much!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Line 140:
    Code:
    cout << " " < endl;
    Why do you think it could be complaining about <, and why do you think this has anything to do with sqrt?

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    21

    Where line 140 is.

    Oops, sorry.
    Line 140 is somewhere in this area. I have a couple lines because I may have counted one or two off

    Code:
                  if (x < 0)
                            {
                                    cout << " " << endl;
                                    cout << "Number can not be negative" << endl;
                                    cout << " " < endl;
                            }
                    }
                    while (x < 0);
    I counted to here, but I dont see anything wrong.
    There is, of course, so maybe someone else can see it!
    Thanks

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    21

    Oh I see haha

    Wow, that slipped my eye, thanks a lot
    I know that that doesnt have anything to do with square roots, but it was in the area, so, i dont know!
    Haha thanks a lot.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by thekautz View Post
    Oops, sorry.
    Line 140 is somewhere in this area. I have a couple lines because I may have counted one or two off

    Code:
                  if (x < 0)
                            {
                                    cout << " " << endl;
                                    cout << "Number can not be negative" << endl;
                                    cout << " " < endl;
                            }
                    }
                    while (x < 0);
    I counted to here, but I dont see anything wrong.
    There is, of course, so maybe someone else can see it!
    Thanks
    If you're not using a tool that will tell you where line 140 is, you're not doing it right. Every editor I know of can count lines, and it usually says in the bottom-right hand corner of the window.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I had never met someone before who was a fan of writing loops that dont loop. But there you go...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    I had never met someone before who was a fan of writing loops that dont loop. But there you go...
    Let me introduce you to matsp
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Making my own square root function
    By Alexthunder in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2006, 07:36 PM
  4. square root alternative
    By FH|RogueHunter in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2005, 10:17 PM
  5. Advanced: Calculate square root with templates
    By Sang-drax in forum Contests Board
    Replies: 32
    Last Post: 09-29-2004, 04:37 PM