2014-11-06 79 views
-2

我一直在玩我的学位,并在一段时间内我的学位,我已经嵌套了另一个do()循环中的一些代码,但我遇到的问题是,代码不断,直到即使包裹的数量已满足,最后一辆面包车已满...(C++)代码似乎运行在错误的循环...?

代码如下。如果有人可以看看它,并告诉我我做错了什么,那很棒。裸记住,我才真正被用C++编码约一个月所以我还有很多海拉学习..

#include <iostream> 
using namespace std; 

char cBeltFull; 
int iVanCount, iParcelCount, iParcelLoaded; 
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity; 
char cExit = 'N'; 

int main() { 

    iVanCount = 1; 
    iParcelLoaded = 1; 
    fHeaviestWeight = 0; 
    fVanCapacity = 410; 


    do { 
     //Get the number of parcels to dispatch 
     cout << "How many parcels need sending?" << endl; 
     cin >> iParcelCount; 

     do { 
      fCurrentPayload = 0; 

      do { 

       //Get parcel weight 
       cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?"; 
       cin >> fParcelWeight; 

       //'Load' the parcel 
       cout << endl << endl << "Parcel loaded"; 
       iParcelLoaded ++; 

       //Update the payload 
       fCurrentPayload = fCurrentPayload + fParcelWeight; 

      } while ((fCurrentPayload + fParcelWeight)) < fVanCapacity) 

      //Dispatch the van 
      cout << endl << endl << "Van dispatched."; 

      //Update the van count 
      iVanCount ++; 

      if (fCurrentPayload > fHeaviestVanWeight) { 

       //Update the heaviest weight 
       fHeaviestVanWeight = fCurrentPayload; 

      } 

     } while (iParcelLoaded <= iParcelCount); 

     cout << endl << endl << endl << "Vans dispatched: " << iVanCout; 
     cout << endl << endl << "Weight of heaviest van: " << fHeaviestWeight; 

     cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl; 
     cin >> cExit; 

    } while (cExit == 'N'); 

} 
+2

你想实现什么? :)另外,“fHeaviestWeight”声明在哪里? – gsamaras 2014-11-06 18:41:40

+4

嵌套的do-while循环,这是我很久没见过的东西。 – Borgleader 2014-11-06 18:42:17

+1

@Borgleader有很好的理由:D – deW1 2014-11-06 18:42:41

回答

3

更改此

} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity); 

这个

} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity 
      && iParcelLoaded < iParcelCount); 

这样你将加载用户输入的项目数量。你的代码包含很多语法错误。

我纠正了它们,但请您在下次发布时更加小心。

#include <iostream> 
using namespace std; 

char cBeltFull; 
int iVanCount, iParcelCount, iParcelLoaded; 
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity; 
char cExit = 'N'; 

int main() { 

    iVanCount = 1; 
    iParcelLoaded = 1; 
    fHeaviestVanWeight = 0; 
    fVanCapacity = 410; 


    do { 
     //Get the number of parcels to dispatch 
     cout << "How many parcels need sending?" << endl; 
     cin >> iParcelCount; 

     do { 
      fCurrentPayload = 0; 

      do { 

       //Get parcel weight 
       cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?"; 
       cin >> fParcelWeight; 

       //'Load' the parcel 
       cout << endl << endl << "Parcel loaded"; 
       iParcelLoaded ++; 

       //Update the payload 
       fCurrentPayload = fCurrentPayload + fParcelWeight; 

      } while (((fCurrentPayload + fParcelWeight)) < fVanCapacity && iParcelLoaded < iParcelCount); 

      //Dispatch the van 
      cout << endl << endl << "Van dispatched."; 

      //Update the van count 
      iVanCount ++; 

      if (fCurrentPayload > fHeaviestVanWeight) { 

       //Update the heaviest weight 
       fHeaviestVanWeight = fCurrentPayload; 

      } 

     } while (iParcelLoaded <= iParcelCount); 

     cout << endl << endl << endl << "Vans dispatched: " << iVanCount; 
     cout << endl << endl << "Weight of heaviest van: " << fHeaviestVanWeight; 

     cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl; 
     cin >> cExit; 

    } while (cExit == 'N'); 

} 
+0

感谢您的回复和更正。我将来一定会更加小心。 :) – 2014-11-06 20:14:37

+0

不客气@Olyly约翰。接受对您的问题足够好的答案是一个很好的做法,以便问题在新闻订阅源中显示为已回答。我发现在你的问题中你得到-2,这是因为你的代码有错别字,缺少分号和括号,但主要是因为它不清楚你想达到什么(我不得不在评论中问你)。只是一些提示,很高兴我帮助解决了这个问题! :) – gsamaras 2014-11-06 20:42:07