在C+;中查找两个项目之间的差异时出现问题+; 让我先说一下我对C++仍然非常陌生,希望尽可能地保持简单。我的数学也很差

在C+;中查找两个项目之间的差异时出现问题+; 让我先说一下我对C++仍然非常陌生,希望尽可能地保持简单。我的数学也很差,c++,modulo,cmath,C++,Modulo,Cmath,大多数情况下,我想看看是否有人可以帮助我的代码,以便它总是给出正确的结果。除了一个场景之外,我几乎可以做我想做的事情 我的代码试图找出有人购买了多少包热狗围脖和多少包热狗包子。然后,它会告诉用户,他们可以从中制作多少热狗,以及他们会有多少剩菜或面包。假设一包Weiner包含12个,一包buns包含8个,这就是我到目前为止的想法: #include <iostream> #include <cmath> using namespace std; void hotdog(

大多数情况下,我想看看是否有人可以帮助我的代码,以便它总是给出正确的结果。除了一个场景之外,我几乎可以做我想做的事情

我的代码试图找出有人购买了多少包热狗围脖和多少包热狗包子。然后,它会告诉用户,他们可以从中制作多少热狗,以及他们会有多少剩菜或面包。假设一包Weiner包含12个,一包buns包含8个,这就是我到目前为止的想法:

#include <iostream>
#include <cmath>

using namespace std;

void hotdog(int a, int b){      //a = weiner packages, b = bun packages
    int weiners = 12 * a;
    int buns = 8 * b;
    int total = (weiners + buns) - (weiners - buns);
    int leftOverWeiners = total % weiners;
    int leftOverBuns = total % buns;
    int totalHotDogs = total / 2;

    cout << "You can make " << totalHotDogs << " hotdogs!" << endl;

    if (leftOverWeiners > 0){
        cout << "You have " << leftOverWeiners << " weiners left over though." << endl;
    }else if (leftOverBuns > 0){
        cout << "You have " << leftOverBuns << " buns left over though." << endl;
    }
}

int main(){
    int a;
    int b;

    cout << "Let's see how many hotdogs you can make!" << endl;
    cout << "How many weiner packages did you purchase?: ";
    cin >> a;
    cout << "How many bun packages did you purchase?: ";
    cin >> b;

    hotdog(a, b);

    return 0;
}
#包括
#包括
使用名称空间std;
void hotdog(inta,intb){//a=weiner包,b=bun包
int-weiners=12*a;
int buns=8*b;
整数总计=(韦恩斯+面包)-(韦恩斯-面包);
int leftOverWeiners=总重量百分比;
int leftOverBuns=总的面包数百分比;
int totalHotDogs=总计/2;
不能包含
void热狗(int-weinerspackages、int-bunspackages){
const int weinersPerPackage=12;
常数int bunsPerPackage=8;
const int totalweiners=weinerspackages*weinersperpackages;
const int totalbuns=bunspackages*bunsPerPackage;
int leftoverweiers=0;
int leftoverbuns=0;
int amountOfHotdogs=0;
如果(TotalWeiner>totalbuns){
leftoverweiners=totalweiners-totalbuns;
amountOfHotdogs=总面包数;
leftoverbuns=0;
}
否则如果(totalbuns>TotalWeiner){
leftoverbuns=totalbuns-TotalWeiner;
amountOfHotdogs=总重量;
LeftOverweiers=0;
}
否则{
amountOfHotdogs=总重量;
LeftOverweiers=0;
leftoverbuns=0;
}

std::cout你把它弄得太复杂了。试试这个:

if(weiners > buns)
{
  cout << "You can make " << buns << " hotdogs!" << endl;
  cout << "with " << weiners-buns << " weiners left over" << endl;
  return;
}
cout << "You can make " << weiners << " hotdogs!" << endl;
if(buns > weiners)
{
  cout << "with " << buns-weiners << " buns left over" << endl;
}
if(维纳斯>包子)
{

cout不是int
total=(维纳斯+包子)-(维纳斯-包子)e是指你受到两个物品数量中较小的一个的限制。例如,类似于“热狗的整数”\u i\u can\u make=std::min(温纳,面包)哈哈哈,这看起来确实简单多了。我最初确实尝试过类似的方法,但我想当它与我所拥有的不起作用时,我改变了我的方法。我必须记住,有时要继续尝试同样的方法。
if(weiners > buns)
{
  cout << "You can make " << buns << " hotdogs!" << endl;
  cout << "with " << weiners-buns << " weiners left over" << endl;
  return;
}
cout << "You can make " << weiners << " hotdogs!" << endl;
if(buns > weiners)
{
  cout << "with " << buns-weiners << " buns left over" << endl;
}