I need to get the "digit" to correspond to the correct number in the digits_seen array and set to "true" so I can then test if the numbers in the array equal to "true" or "false" - Thanks!
#include <iostream>
usingnamespace std;
int main()
{
int i, digit;
intlong num;
bool digits_seen[10];
char redo;
do
{
for(i = 0; i < 10; i++)
digits_seen[i] = false; // goes thru number 0 - 9 and sets to false
cout << "Enter a number: ";
cin >> num ; // person enters a number up to 10 digits long
while (num != 0)
{
digit = num % 10;
num = num / 10;
// I need to get the digit to correspond to the correct digit in the array and set to "true"
//digits_seen[] = true;
}
if (digits_seen[digit] != true)
{
cout <<"\nThere are no repeated digits in the number you have entered." ;
}
else cout << "\nThe number you have entered has a repeated digit." ;
cout << "\n\nDo you want to do this calculation again? (Y/N) " ;
cin >> redo;
cout << "\n\n" ;
}
while ((redo != 'n') && (redo !='N'));
return 0;
}
There's a fun little trick you can do with this problem in that you can convert the number to a string (or just take a string as input, but make sure to validate your input if you do that).
However! What's digit's purpose in this program? Because it seems like that variable's range of possible values (0-9) lines up quite nicely with valid subscripts for your array (0-9).
Also, a check for a repeated digit would have to be done in the loop if you're using bools (lines 28-33). The fact that the program saw a digit doesn't mean that it's a repeated digit.
#include <iostream>
usingnamespace std;
int main()
{
int i, digit;
intlong num;
bool digits_seen[10];
char redo;
do
{
for(i = 0; i < 10; i++)
digits_seen[i] == false; // goes thru number 0 - 9 and sets to false
cout << "Enter a number: ";
cin >> num ; // person enters a number up to 10 digits long
while (num != 0)
{
digit = num % 10;
num = num / 10;
if ((digits_seen[digit] = true))
cout << "\nThe number you have entered has a repeated digit." ;
else (digits_seen[digit] == true);
}
if ((digits_seen[digit] != true))
cout <<"\nThere are no repeated digits in the number you have entered." ;
cout << "\n\nDo you want to do this calculation again? (Y/N) " ;
cin >> redo;
cout << "\n\n" ;
}
while ((redo != 'n') && (redo !='N'));
return 0;
}
Line 24: Uuuum. Look closely at which equals you're using there.
Line 25: That output is wonderful, but after you've found a repeated digit, is there any reason to continue the loop?
Line 26: Why precisely do you have this line? It's not quite syntactically correct (else should not have a condition), besides, not every if needs an else. There's something you could replace it with. Why here? Because it'll interfere with your check on line 24 if it's put any earlier.
You have an array with subscripts 0-9. The range of values for a digit is also 0-9. So why not have each element of an array represent a digit of the same value as the array's subscript? Ex. digits_seen[0] would represent 0, digits_seen[1] would represent 1.
Line 29: This check isn't quite correct, because this line should theoretically never run. You may want to check for a different condition. Maybe some bool variable that was initialized to false and set to true if there WERE repeated digits, or (this one is more complicated) if num isn't equal to zero, if you break out of your loop once it finds that there's a repeated digit and you can guarantee that num isn't zero when the loop is broken out of.