help with a loop (C++)
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" ); }
}
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 <iostream>
#include <string>
using namespace std;
int main()
{
int a;
int b;
int c;
int x=1;
int y;
while(1)
{
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;
cout << "Would you like to do another equation? (Y or N)" << endl;
cout << "Default action is to repeat." << endl;
cout << "Request: ";
string z;
cin >> z;
if (z == "Y" || z == "y")
{
continue;
}
else if(z == "N" || z == "n")
{
exit(1);
}
else
{
continue;
}
}
}
}
Wow, you guys are messy. =P
#include <iostream>
int main(int argc, char **argv)
{
unsigned int uB, uC, uX = 1, uY;
char chRestart;
bool bQuit = false;
do
{
std::cout << "Enter the value for b: ";
std::cin >> uB;
std::cout << "Enter the value for c: ";
std::cin >> uC;
++uX;
uY = uC / uX;
if(uX + uY == uB)
{
std::cout << "x is equal to: " << uX
<< " and y is equal to: " << uY << std::endl;
std::cout << "Would you like to perform another operation? "
<< "(Y) or (N) : ";
std::cin >> chRestart;
bQuit = (chRestart == 'Y' || chRestart == 'y') ? false : true;
}
} while(!bQuit);
return 0;
}