简体   繁体   English

虽然循环逻辑不适用于我的代码,但无法弄清楚

[英]While loop logic not working on my code, cant figure it out

Im having issues on how to make the while loop work on my code (C++), can you please help?我有关于如何使 while 循环在我的代码(C++)上工作的问题,你能帮忙吗? I want the user to be asked a question and if the answer is "si" then to execute what I have for the if statements.我希望用户被问一个问题,如果答案是“si”,那么执行我对 if 语句的处理。 If answer is no, then skip the while loop and execute the last code.如果答案是否定的,则跳过 while 循环并执行最后的代码。 Im getting an error after all the input, just an endless loop of letters.在所有输入之后我得到一个错误,只是一个无穷无尽的字母循环。 Any hints on how I can fix it will be very appreciated.任何关于我如何修复它的提示将不胜感激。 Thank you.谢谢你。

int main()
{   
    int a, b, c, d, total, promedio;
    
    string siNo;

    
    cout << "Ingresar nota 1: ";
    cin >> a;
    cout <<"Ingrese segunda nota: ";
    cin >> b;
    cout << "Ingrese tercera nota: ";
    cin >> c;
    cout << "Ingresar nota 4: ";
    cin >> d;

    cout<<endl;

    cout << "Desea eximirse? ";
    cin >> siNo;

    total = (a+b+c+d)/4;
    promedio = (a+b+c)/3;

    while (siNo != "no"){

    if(promedio >= 85){
        cout << "Si está eximido, su promedio es: " + promedio;
    }
    if(promedio < 85){
        cout << "No está eximido, su promedio de los 3 parciales es:" + promedio;
        }

    }
    
    cout << "Su nota final es " + total;
    
     return 0;
}

You haven't given any exit loop statement inside your while loop that is why it is running in infinite loop您没有在 while 循环中给出任何退出循环语句,这就是它在无限循环中运行的原因

while (siNo != "no"){
    if(promedio >= 85){
        cout << "Si está eximido, su promedio es: " + promedio;
        break; // just add this line
    }
    if(promedio < 85){
        cout << "No está eximido, su promedio de los 3 parciales es:" + promedio;
        break;
    }
}

This is an infinite loop:这是一个无限循环:

while (siNo != "no") {

    if(promedio >= 85){
        cout << "Si está eximido, su promedio es: " + promedio;
    }
    if(promedio < 85) {
        cout << "No está eximido, su promedio de los 3 parciales es:" + promedio;
    }
}

Since nothing changes siNo after the cout statements, it's going to keep evaluating (siNo != "no") to be true.由于在 cout 语句之后没有任何改变siNo ,因此它将继续评估(siNo != "no")为真。

I don't think a loop is necessary here我认为这里不需要循环

// if answer is equal to "si"
if (siNo == "si") { 
   if(promedio >= 85){ 
   } // continue here
} 

// else is not required 
// if you don't want to do anything when answer is no

return 0;

A simple if statement will do the trick for ya.一个简单的 if 语句就可以解决问题。

Thank you all for your suggestions and comments, it was definitely easier to use ifs, no need for a while loop.谢谢大家的建议和意见,使用 if 肯定更容易,不需要 while 循环。 Here is how I solved it:这是我解决它的方法:

 if (siNo == "si")
{
    if(promedio >= 85)
    {
    cout << "Si está eximido, su promedio es: " << promedio << endl;
    cout << "Su nota para el cuarto parcial es: " << promedio << endl;
    }   
    else { 
     cout << "No está eximido, su promedio de los 3 parciales es:"<< promedio << endl;
    }
}

if (siNo == "no")
{    
    cout << "Ingresar nota 4: ";
    cin >> d;
        cout << "Su nota final es " << total;
}

 return 0;

} }

Hey thank you all for your responses.嘿,谢谢大家的回复。 I finally managed to make it work with your tips and using for loops.我终于设法使它与您的提示一起使用并使用 for 循环。 Here is the code, hope it helps others with same questions:这是代码,希望它可以帮助其他人有同样的问题:

int main() {

double precios[5];
int cantidades[5];

double precioMasAlto = 0;
int cantidadMinima;
double total = 0;
int codigosProducto[5];
int codigoProducto = 0;
int codCantidadMasBaja = 0;

// 1.  ingreso de datos
for (int i = 0; i < 5 ; i++) {
    cout << "Ingrese codigo de producto " << i << endl;
    cin >> codigosProducto[i];
    cout << "Ingresar precio de producto " << i << endl;
    cin >> precios[i];
    cout << "Ingresar cantidad del producto " << i << endl;
    cin>> cantidades[i];
}

cantidadMinima = cantidades[0];
    

// 2. cual de los productos tiene el precio mas alto

  for(int posicion = 0; posicion < 5; posicion++){
      if(precios[posicion] > precioMasAlto){
          precioMasAlto = precios[posicion];
          codigoProducto = codigosProducto[posicion];
      }
  }
  
cout << "El producto con mayor precio es el Producto " << codigoProducto << ", cuyo precio es " << precioMasAlto <<endl;
// 3. cual de los productos tiene menor existencias

for(int posicion = 0; posicion < 5; posicion++){
      if(cantidades[posicion] < cantidadMinima ){
          cantidadMinima = cantidades[posicion];
          codigoProducto = codigosProducto[posicion];
        }
    }
    
cout << "El producto con menor existencia es el Producto " << codigoProducto << ", con una existencia actual de " << cantidadMinima  << endl; 

// 4. total de productos
for (int i = 0; i < 5; i++){
     total += cantidades[i];
}

cout << "El total de productos x cantidad es " << total;



return 0;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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