Problem with math program

So I have this program that allows one to find the square root of a number except it does not do quite that. When I run it, the square root it gets is always 0.

Here is the program so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;

int main()
{
    string shallwestart;
    cout << "Welcome to this program of finding square roots of numbers.\nShall we start?(Y/N)";
    cin >> shallwestart;
    if (shallwestart == "y")
    {
        
        iff:
        cout << "Ok! Please enter the number you want square-rooted: ";
        long start1;
        long start2;
        cin >> start1;
        start2 = start1;
        long halfstart;
        halfstart = start1/2;
        for (long x=halfstart;x--;start1=x*x)
        {
            halfstart--;
        }
        cout << "The square root of " << start2 << " = " << halfstart << "!";
    }
    else if (shallwestart == "Y")
    {
        goto iff;
    }
    else if (shallwestart == "n")
    {
        elsee:
        cout << "That's to bad. See you later.";
    }
    else if(shallwestart == "N")
    {
        goto elsee;
    }
    else
    {
       cout<< "You have entered something that was not Y or N. Since I will not bother to code something that allows you to type in your answer again this program will end and you will just have to restart it.";
    }
    return 0;
}


Output:
Welcome to the program of finding square roots of numbers
Shall we begin?(Y/N)Y
Ok! Please enter a number to be square-rooted: 4
The square root of 4 = 0!


Any help would be much appreciated.
So starting from line 17 cin >> start1; you begin by assigning to start1 to start2
Then intializing halfstart to start1 / 2 Okay so far I don't follow why you did these assignments...
Then the for loop is where your problems start. I'm not sure what you are trying to do here. Are you trying to loop until x*x == start1???? Help us understand what you want the code to do here so we can help.

In the meantime, you can find more information on for loops here: http://cplusplus.com/doc/tutorial/control/
I feel you should also read information on while loops and do while loops on the for loop page, because they could help you get rid of the goto commands.

I hope this reading helps your coding! :)
Last edited on
As Kevin said, your for loop is all kinds of messed up.
Not the solution to this problem, but just a suggestion.. Why do you have this unnecessary long if-else-if ladder? Why don't you use logical operators? Will cut down on your code length. Also, goto, bad habit. Get rid of it.. Also wonder why shallwestart is a string?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

int main()
{
    char shallwestart;
    cout << "Welcome to this program of finding square roots of numbers.\nShall we start?(Y/N)";
    cin >> shallwestart;
    if (shallwestart == 'y'||shallwestart=='Y')
    {
        cout << "Ok! Please enter the number you want square-rooted: ";
        long start1, start2, halfstart;
        cin >> start1;
        start2 = start1;
        halfstart = (start1)/2;

        for (long x=halfstart;x--;start1=x*x)          //What kind of an algorithm is this anyways?
        {
            halfstart--;
        }
        cout << "The square root of " << start2 << " = " << halfstart << "!";
    }
 
    else if (shallwestart == 'n'||shallwestart=='N')
    {
          cout << "That's too bad. See you later.";
    }

    else
    {
       cout<< "You have entered something that was not Y or N. Please restart the program.";
    }
    return 0;
}


Ok I have figured out something to bypass all of this trouble with the nifty math.h library. The reason I assigned start1 to start 2 is that somehow start1 got changed to 0 and I wanted to output the original number. I know this was not very good programming... but it's pretty much fixed now.
Topic archived. No new replies allowed.