1 Replies - 190 Views - Last Post: 30 April 2019 - 07:44 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 708
  • Joined: 15-December 14

How to break out of nested for loops?.

Posted 30 April 2019 - 07:27 PM

So, I'm stuck on chapter 3. The author wants me to TRY THIS .. Implement square() without using the multiplication operator; that is, do the x*x by repeated addition (start a variable result at 0 and add x to it x times). Then run some version of "the first program" using that square().

I thought instead of trying to over complicate everything I thought I would use simple math to find the square root without using the sqrt() function.I found an article to prove my idea would work. So what I did was use two nested loops and have them multiply till they came with the answer equal to what was inputted by the user, then use some more math to find the answer. I realise I still have a lot to do, but right now I'm trying to get this part of the program working before I work on the rest of the math required to make it work.

This is the article I'm fine tuning my idea.

https://www.quickand...our-head?page=2

So my question is I'm ,currently stuck on trying to find a way to break the nested loops once I find the two integers that equal the number inputted. I've tried everything I could think of and nothing works.

Could I please get a hint or a suggestion?


This is the program


#include <iostream>
#include <math.h>
//  https://www.quickanddirtytips.com/education/math/how-to-calculate-square-roots-in-your-head
int main()
{
    double input;
    double ans;
    std::cout << "Enter number" <<std::endl;
    std::cin >> input;
    for(int x = 0; x < 11; x++)
    {
        for(double z = 0.0; z < 11; z++)
        {
            ans = x * z;
            std::cout << x <<" * "<< z <<" = "<<ans <<std::endl;
            if(ans = x * z)
                break;
        }
        if()
            break;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to break out of nested for loops?.

#2 albert003   User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 708
  • Joined: 15-December 14

Re: How to break out of nested for loops?.

Posted 30 April 2019 - 07:44 PM

Disregard my initial post. I just realized reading the instructions the author again that I was doing the whole program incorrectly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1