Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

help with a loop (C++)


ghost's Avatar
0 0

hey guys, i need a little help. Below you can see my code for a program to factor equations but as you can see there is no loop, i need some help with what kind of loop to use and some tips on writing it. If this isnt descriptive enough just tell me and ill try to explain better.

thanks,

#include <iostream.h> main() { int a; int b; int c; int x=1; int y; cout<<"enter value for b"<<endl; cin>>b; cout<<"enter value for c"<<endl; cin>>c; system("pause" ); x++; y=c/x; if(x+y==b ); { cout<<" x equals "<<x<<" "<<" y equals "<<y<<endl; system("pause" ); }

}


ghost's Avatar
0 0

You can find an explanation for loops at practically any site that offers tutorials on C++…but what I used here is called a while loop. WHILE a certain condition is TRUE execute this code.

#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main()
{
int a;
int b;
int c;
int x=1;
int y;
while(1)
{
cout&lt;&lt;&quot;enter value for b&quot;&lt;&lt;endl;
cin&gt;&gt;b;
cout&lt;&lt;&quot;enter value for c&quot;&lt;&lt;endl;
cin&gt;&gt;c;
system(&quot;pause&quot; );
x++;
y=c/x;
if(x+y==b );
{
cout&lt;&lt;&quot; x equals &quot;&lt;&lt;x&lt;&lt;&quot; &quot;&lt;&lt;&quot; y equals &quot;&lt;&lt;y&lt;&lt;endl;
cout &lt;&lt; &quot;Would you like to do another equation? (Y or N)&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Default action is to repeat.&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Request: &quot;;
string z;
cin &gt;&gt; z;
if (z == &quot;Y&quot; || z == &quot;y&quot;)
{
      continue;
      }
else if(z == &quot;N&quot; || z == &quot;n&quot;)
{
      exit(1);
      }
else 
{
     continue;
     }
}
}
}

ghost's Avatar
0 0

Wow, you guys are messy. =P

#include &lt;iostream&gt;

int main(int argc, char **argv)
{
      unsigned int uB, uC, uX = 1, uY;
      char chRestart;
      bool bQuit = false;

      do
      {
           std::cout &lt;&lt; &quot;Enter the value for b: &quot;;
           std::cin &gt;&gt; uB;
           std::cout &lt;&lt; &quot;Enter the value for c: &quot;;
           std::cin &gt;&gt; uC;

                ++uX;
                uY = uC / uX;
                if(uX + uY == uB)
                {
                        std::cout &lt;&lt; &quot;x is equal to: &quot; &lt;&lt; uX 
                                        &lt;&lt; &quot; and y is equal to: &quot; &lt;&lt; uY &lt;&lt; std::endl;
                        std::cout &lt;&lt; &quot;Would you like to perform another operation? &quot;
                                        &lt;&lt; &quot;(Y) or (N) : &quot;;
                        std::cin &gt;&gt; chRestart;
                        bQuit = (chRestart == &#39;Y&#39; || chRestart == &#39;y&#39;) ? false : true;
                }
        } while(!bQuit);

        return 0;
}