0

I have an assignment where we have to write code that finds a number that fits four parameters. My idea was to split up that number into its individual digits, then have an if statement that checks if the number fits the parameters. If it doesn't I wanted it to increase by one and loop until it finds the number. My loop only runs once however, and doesn't even return all four digits. Any direction on what I should do would be appreciated.

#include <iostream>
using namespace std;
int digit_breakdown(int& number);
int main()
{

    int number = 1000; //This is the variable I will change to find the         secret number
    int last_dig, third_dig, sec_dig, first_dig; // These are variables   that represent each of the four digits
    int secret_number = 0; //This variable holds the secret number

    do{
        number++;
        last_dig = number%10; //This series of code breaks up the number into     digits
        number /= 10;
        third_dig = number%10;
        number /= 10;
        sec_dig = number%10;
        number /= 10;
        first_dig = number%10;
        number /=10;

        if((first_dig != sec_dig) && (first_dig != third_dig) && (first_dig    != last_dig) && (sec_dig != third_dig) && (sec_dig != last$
            number = secret_number;
    }while((secret_number = 0));

    cout << "You found the secret number! That number is " << secret_number;
    cout << last_dig << endl;
    cout << third_dig << endl;
    cout << sec_dig << endl;
    cout << first_dig << endl;

}
4
  • 2
    while((secret_number = 0)) -- See anything wrong with that line? Commented Feb 20, 2017 at 1:21
  • while(secret_number==0) its == not = Commented Feb 20, 2017 at 1:21
  • Please make some indents so the code will be more readable... Also what's with the dollar sign in sec_dig != last$? Commented Feb 20, 2017 at 1:22
  • @DannyuNDos i guess it is a copy-paste error from an editor where that line exceeded the screen width Commented Feb 20, 2017 at 2:44

2 Answers 2

1

I have changed the logic and few statements as per the problematic statement given. Even though you will change the while loop condition as suggested, you will not get exact output.

So the changes are.

A) secret_number assigned at the beginning of the loop and holds the incremented number

B) do the modification operations on secret_number variable

C) use "break" once the condition is met.(because you got your secret number)

E) Because your condition is endless till you find a required number changed the loop to while(1) [This is optional you can set it to some upper limit]

F) Display the number itself.

Below is the code details.

#include <iostream>
using namespace std;
int digit_breakdown(int& number);
int main()
{

    int number = 1000; //This is the variable I will change to find the         secret number
    int last_dig, third_dig, sec_dig, first_dig; // These are variables   that represent each of the four digits
    int secret_number = 0; //This variable holds the secret number

    do{
        secret_number = ++number;
        last_dig = secret_number%10; //This series of code breaks up the number into     digits
        secret_number /= 10;
        third_dig = secret_number%10;
        secret_number /= 10;
        sec_dig = secret_number%10;
        secret_number /= 10;
        first_dig = secret_number%10;
        secret_number /=10;

        if((first_dig != sec_dig) && (first_dig != third_dig) && (first_dig    != last_dig) && (sec_dig != third_dig) && (sec_dig != last_dig)){
            break;
        }
    }while(1);  //To avoid endless loop you can set some upper_limit

    cout << "You found the secret number! That number is " << number<<endl;
    cout << last_dig << endl;
    cout << third_dig << endl;
    cout << sec_dig << endl;
    cout << first_dig << endl;

    return 0;
}

Output is as follows:

/WorkDir/checks/stackover> vi loop_digit.cpp
/WorkDir/checks/stackover> g++ -Wall -Wextra -O2 -o loop_digit loop_digit.cpp 
/WorkDir/checks/stackover> ./loop_digit 
You found the secret number! That number is 1022
2
2
0
1
Sign up to request clarification or add additional context in comments.

Comments

1

Change

while((secret_number = 0));

to

while(secret_number == 0);

Single = is an assignment operator, for comparison you need ==

There's also a $ sign in one of the conditions of if-statement inside while loop that you might want to rectify.

7 Comments

When comparing against constants, I've found it helpful to state the constant first, such as while(( 0 == secret_number)), so in case of a mistype, the compiler will choke on while(( 0 = secret_number )).
Indeed it's a better way to avoid this kind of mistake.
Oh geeze, thank you! I made this change but now the loop doesn't seem to be finishing. I've been waiting around ten minutes and its not giving me any kind of response. Does it typically take a long time for a program to run through hundreds of numbers?
It now appears you will start at 1001, continue until digits are unique, say, 1023, set number to 0, add 1, and continue until all digits are unique, set number to 0, add 1... without an exit condition, like breaking or changing secret_number... Although this introduces a new question...
This answer does however satisfy the initial question of why your loop won't loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.