mercredi 3 février 2021

C++ Forcing the END of a while-Loop Prematurely ONLY when an (IF) loop is incorporated into the while loop?

The program is supposed to ask the input to input a variable for a vector UNTIL the user picks "-0" to stop the while loop. WITH THIS "IF" nested loop within the While loop, it allows 3 entries before it stops the program! whY? Please find the "THIS RIGHT HERE; THIS is the offending code!" note in this codeblock and try the program with or without said 'if' statement.

   int main(){
    std::vector<float> asdf;
    float g = 0; std::cout << "enter value g;" << endl;
    std::cin >> g; asdf.push_back(g);
    while (g != -0) {
        std::cout << "enter another g value" << endl;
        std::cin >> g;
        asdf.push_back(g);
    /* **THIS RIGHT HERE; THIS is the offending code!** */       if (g = -0) { asdf.pop_back(); }
    }   
    std::cout << "end of vector" << endl;
    for (int i = 0; i < asdf.size(); i++) { std::cout << asdf[i] << ", "; }
    std::cout << "end of while;" << endl;
    system("pause"); return 0;
    
}

Thank you

Aucun commentaire:

Enregistrer un commentaire