what do i need to do to loop this 10 times
i keep trying to put it in a while loop because i dont know enough about a for loop.
when i put it in a while loop this only runs the first chunk of my code and then hangs...
the code i have provided is only the code that i want looped...
srand(time(0));//Starts random number generator
int number = rand() % 99 + 1;//Using Modulus operator to isolate values 99+1
cout << number;
cin.ignore(100, '\n');
cout << " Is this number a Perfect, Abundant, or Deficient ? " << endl;
cout << " Make sure you use capitol letters when selecting!" << endl;
cout << " Press A if you think it's a PERFECT NUMBER" << endl;
cout << " Press B if you think it's a ABUNDANT NUMBER" << endl;
cout << " Press C if you think it's a DEFICIENT NUMBER" << endl;
char Guess;
cin >> Guess;
int rand,one = 1, sum = 0;
while (one < number)
{
if(number % one == 0)
sum = sum + one;
one++;
}
if(sum == number && Guess == 'A') //added another condition
{
cout << "You are correct! This is a Perfect Number!";
int numRightGuesses;
}
elseif (sum > number && Guess == 'B') //added another condition
{
cout << "You are correct! This is an Abundant Number!";
int numRightGuesses;
}
elseif (sum < number && Guess == 'C') //added another condition
{
cout << "You are correct! This is a Deficient Number!";
int numRightGuesses;
}
else
{
cout << "Sorry, you are incorrect!";
int numWrongGuesses;
}
{
cout << numWrongGuesses << " Wrong" << endl;
cout << numRightGuesses << " Right" << endl;
}
return 0;
}
ive been bashing my head against a wall now for hours, im feeling really stupid
still no luck....
im not sure what i could be doing wrong, I put what you told me right at the top of the code you had provided and it told me one was not declared in the scope. So i then deleted the declared int one = 1; which was beneath the for loop and placed it above. it then compiled ( i got excited) ran thru the program once and it closed , just like every other time ... anyways updated code looks as follows now.
int one = 1;
for(one = 1; one <= 10; one++)
srand(time(0));//Starts random number generator
int number = rand() % 99 + 1;//Using Modulus operator to isolate values 99+1
cout << number;
cin.ignore(100, '\n');
cout << " Is this number a Perfect, Abundant, or Deficient ? " << endl;
cout << " Make sure you use capitol letters when selecting!" << endl;
cout << " Press A if you think it's a PERFECT NUMBER" << endl;
cout << " Press B if you think it's a ABUNDANT NUMBER" << endl;
cout << " Press C if you think it's a DEFICIENT NUMBER" << endl;
char Guess;
cin >> Guess;
int rand, sum = 0;
while (one < number)
{
if(number % one == 0)
sum = sum + one;
one++;
}
if(sum == number && Guess == 'A') //added another condition
{
cout << "You are correct! This is a Perfect Number!";
int numRightGuesses;
}
elseif (sum > number && Guess == 'B') //added another condition
{
cout << "You are correct! This is an Abundant Number!";
int numRightGuesses;
}
elseif (sum < number && Guess == 'C') //added another condition
{
cout << "You are correct! This is a Deficient Number!";
int numRightGuesses;
}
else
{
cout << "Sorry, you are incorrect!";
int numWrongGuesses;
}
{
cout << numWrongGuesses << " Wrong" << endl;
cout << numRightGuesses << " Right" << endl;
}
return 0;
}
}
edit* to my eyes i think i might be using the interger one=1 to do too much , any people out there agree with me ? or am i off base
Instead of using return to stop the cmd from disappearing use the
_getch()
at the end of the program...
And u should put the srand() outside, I mean above the for!