Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

Beginner C++ Question: I need to find the location of certain digits from user input

$
0
0
I have been trying to finish this code (function) for a while now, but am stuck on the last part. In this code, I prompt the user to select a number of integers and any number of digits and then find the smallest and largest value within these digits. On the next part, I am supposed to determine which of the given digits the smallest and largest are located such that the output should be:

Digit _ can be found in integer number(s): _, _

I apologize in advance if my code is sloppy; I just started learning C++ and haven't fully grasped the language yet.

Here is what I have tried:

Code:

int digitSizeLoca() {

  int userNumInteger;
  int* iPtr;
  int* iPtr2;
  int* iPtr3;
  int value;
  int value2;
  int value3;

  std::cout << "\nHow many integers? ";
  std::cin >> userNumInteger;

  iPtr = new int[userNumInteger];
  iPtr2 = new int[userNumInteger];
  iPtr3 = new int[userNumInteger];

  for (int i = 0; i < userNumInteger; i++) {

        *(iPtr3 + 1) = *(iPtr2 + 1) = *(iPtr + 1);

        std::cout << "\nEnter digit #" << i + 1 << ": ";
        std::cin >> *(iPtr + 1);
  }

  value = *(iPtr + 1);
  value2 = *(iPtr2 + 1);
  value3 = *(iPtr3 + 1);

  if (value != 0, value2 != 0, value3 != 0) {

    if (value <= 0)
      value = -value;
        if (value2 <= 0)
          value2 = -value2;
        if (value3 <= 0)
          value3 = -value3;

        int lDigit;
    int sDigit;
    int curDigit;
    int pot = 10;

    lDigit = sDigit =  value % pot;

        while (value, value2, value3) {

      if (value / pot == 0, value2 / pot == 0, value3 / pot == 0) break;

      curDigit = (value / pot, value2 / pot, value3 / pot) % 10;
         
      if (curDigit < sDigit)
                sDigit = curDigit;
         
          if (curDigit > lDigit)
            lDigit = curDigit;
   
      pot*=10;
        }

        std::cout << "\nThe smallest digit: " << sDigit << std::endl
          << "\n  Digit " << sDigit
          << " can be found in integer number(s): ";

        for (int i = 0; i < userNumInteger; i++) {

          int temp;

          if (value < 0, value2 < 0, value3 < 0)
                temp = -value, temp = -value2, temp = -value3;
          else
                temp = value, temp = value2, temp = value3;

          do {
                if (temp % 10 == sDigit) {
                  std::cout << " " << i+1;
                  temp = 0;

                  if (i != 0 && i < userNumInteger - 1 || i != 1 && i < userNumInteger - 1)
                        std::cout << ",";
                }
                temp /= 10;
          } while(temp);
        }
       
    std::cout << "\nThe largest digit: " << lDigit << std::endl
          << "\n  Digit " << lDigit
      << " can be found in integer number(s): ";

  }

  return 0;
}

Where:

Code:

for (int i = 0; i < userNumInteger; i++) {

          int temp;

          if (value < 0, value2 < 0, value3 < 0)
                temp = -value, temp = -value2, temp = -value3;
          else
                temp = value, temp = value2, temp = value3;

          do {
                if (temp % 10 == sDigit) {
                  std::cout << " " << i+1;
                  temp = 0;

                  if (i != 0 && i < userNumInteger - 1 || i != 1 && i < userNumInteger - 1)
                        std::cout << ",";
                }
                temp /= 10;
          } while(temp);
        }

Seems to do the job, but it always outputs 1, 2...

Any form of advice is greatly appreciated!

Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images