0

How do I compare an int variable and a string variable in if statement

how to do if array[j] == blank or char/string then break?

Can somebody explain? I have no idea how to do this, I'm a beginner

#include <algorithm>
#include <iostream> 
#include <algorithm>
#include <vector>
#include <string>

int main()
{

    std::string aa = " ";
    std::vector<int> array(5);
    for (int j = 0; 0 < 5; j += 1) {
        array.push_back(1);
        std::cin >> array[j];
        if (array[j] < aa) {
            break;
        }
    }




    for (int startIndex = 0; startIndex < array.size() - 1; ++startIndex)
    {

        int smallestIndex = startIndex;


        for (int currentIndex = startIndex + 1; currentIndex < array.size(); ++currentIndex)
        {

            if (array[currentIndex] < array[smallestIndex])
    

                smallestIndex = currentIndex;

            std::cout << currentIndex << "  ";
        }

        std::swap(array[startIndex], array[smallestIndex]);


    
}

    for (int index = 0; index < array.size(); ++index)
        std::cout << array[index] << ' ';
}
  • 1
    `if( std::to_string(array[i]) == "" )` - but note that no integer will ever be equal to `""`. – Ted Lyngmo Nov 29 '20 at 14:58
  • I tried it but it doesn't really solve the problem, I just stops working when I print something other than a number but the program simply doesn't end – thePinkAndRed Nov 29 '20 at 15:03
  • 1
    Your condition is `0 < 5` which is always true. Replace this with `j < 5`. – HolyHoratio Nov 29 '20 at 15:10
  • `if (array[j] < aa) {` maybe if you explained what this loop was supposed to do. I have no idea why you want to make this comparison between a number and a string containing a single space. It seems like a very unusual comparison. – drescherjm Nov 29 '20 at 15:25
  • ***how to do if array[j] == blank or char/string then break?*** `array[i]` for all valid indices will never ever be a blank string. – drescherjm Nov 29 '20 at 15:27
  • I just want to type values that are necessary are not anything other than integer otherwise, stop the program for loop – thePinkAndRed Nov 29 '20 at 15:29
  • You probably want something like: [https://stackoverflow.com/a/8377715/487892](https://stackoverflow.com/a/8377715/487892) remember to press ctrl-z or ctrl-d to end the stream depending on your OS. Related to that: [https://stackoverflow.com/questions/46114870/when-the-input-will-stop](https://stackoverflow.com/questions/46114870/when-the-input-will-stop) – drescherjm Nov 29 '20 at 15:33
  • This doesn't address the question, but `std::array array(5);` initializes `array` with 5 values. When the code later does `array.push_back(1)`; it's adding a 6th value, a 7th value, etc. So inside that loop, with `j` running from 0 through 4, all of the values of `array[j]` will be 0, and when the loop exits, the array will hold 10 integer values. That seems wrong; you might want to change the initialization to `std::vector array;`, which will initialize `array` with no values. The 5 push_backs will fill in 5 values, and afterwards the array will hold 5 integer values. – Pete Becker Nov 29 '20 at 17:28

0 Answers0