简体   繁体   中英

My loop won't loop

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;

}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM