最大的5个整数 [英] largest of 5 integers

查看:109
本文介绍了最大的5个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发菜单驱动的程序,这是一个C ++菜单驱动程序,允许用户输入五个数字,然后

在最小,最大,总和或平均值之间进行选择。使用if语句

来确定要采取的操作。

如果允许则没有其他循环且没有数组



当我用整数1,2,3,4,5运行最大的5个选择时,它给出2个答案,有人指出问题是什么?



So I am working on a menu driven program which is

Q)Write a C++ Menu driven program that allows a user to enter five numbers and then
choose between findings the smallest, largest, sum or average. Use else if statement
to determine what action to take.
just else if allowed no other loop and no arrays

and when I run largest of 5 choice with integers 1,2,3,4,5 it gives 2 as answer what's the problem can someone point out??

#include<iostream>
using namespace std;

int main()
{
      int num1=0,num2=0,num3=0,num4=0,num5=0,choice=0;
      int sum=0,avg=0,largest,smallest;

    re_enter_label:cout << "\t\t\t''MENU''\n"<<endl;
    cout <<"1.  Find Smallest of 5"<<endl;
    cout <<"2.  Find Largest of 5"<<endl;
    cout <<"3.  Find Sum of 5"<<endl;
    cout <<"4.  Find Average of 5"<<endl;
    cout <<"Choice: ";
    cin >> choice;
    cout <<"Enter 5 numbers : ";
    cin>>num1>>num2>>num3>>num4>>num5;

            if(choice==1)
{
        smallest=num1;

        if(num2 < smallest )
        { // compare num2 to smallest,
         smallest = num2;
        }
        else if(num3 < smallest )
        {
		smallest = num3;
        }
        else if(num4 < smallest )
        {
		smallest = num4;
        }
        else if(num5 < smallest )
        {
		smallest = num5;
        }

	cout <<"Smallest of all integers: " <<smallest<<endl;

	return 0;
}
    else if(choice==2)
    {
        largest=num1;

        if(num2 > largest )
        {
         largest = num2;
        }
        else if(num3 > largest )
        {
		largest = num3;
        }
        else if(num4 > largest )
        {
		largest = num4;
        }
        else if(num5 > largest )
        {
		largest = num5;
        }

	cout <<"Largest of all integers: " <<largest<<endl;

    }
    else if(choice==3)
    {
         sum = ( num1 + num2 + num3 + num4 + num5 );
         cout <<"Sum of the given numbers: "<<sum<<endl;
    }
    else if(choice==4)
    {
    avg = ( ( num1 + num2 + num3 + num4 + num5 ) / 5 );
    cout <<"Average of the given numbers: "<<avg<<endl;
    }
    else if(choice>5)
    {
            goto re_enter_label;
    }
    return 0;
}

推荐答案

你的逻辑错了。如果2> 1为真,则最大值为= 2,其余为否则将被跳过。
Your logic is wrong. If 2 > 1 is true then the largest = 2, the rest of else if will be skipped.


如果,则需要替换 else if in a:

You need to replace else if with if inside a:
if(choice==1)
{
   // Use only if-s
}
else if(choice==2)
{
   // Use only if-s
}



但是,正如谢尔盖所​​说,这将是一个学习如何学习的好机会调试你的代码,所以把断点放在 else if(choice == 2)并再次重现你的问题。程序执行到达断点后,一步一步地观察会发生什么(你应该注意到如果(num2>最大)将被执行但剩下的 else,如果被跳过。


But as Sergey mentioned this would be a good opportunity for you to learn how to debug your code, so put the break point at else if(choice==2) and reproduce your issue again. After the program's execution reaches the break point go step by step and observe what happens (you should notice that if(num2 > largest ) is going to be executed but the rest of the else if are skipped.


Peter在解决方案1中说的是真的,但并没有真正解决你遇到的问题并且会对你的工作方式有所帮助。



首先不要使用命名变量来保存你从用户那里读取的数据:使用数组代替:

What Peter says in Solution 1 is true, but doesn't really address the problems you are having, and will have with the way you are doing things.

Start by not using "named" variables to hold the data you read from the user: use an array instead:
int data[5];
for (i = 0; i < 5; i++)
   {
   cin>>data[i];
   }

然后找到最大或最小的总和平均值都是非常相似(并且非常简单)的循环:

Then finding the largest, or smallest, sum and average are all just very similar (and very simple) loops:

sum = 0;
for(i = 0; < < 5; i++)
   {
   sum += data[i];
   }

如果您的任务更改为需要十个数字而不是五个,则更改变得微不足道。



尝试一下:它可能看起来更复杂,但它真的,真的不是一旦你了解它!

And if your task changes to want ten numbers instead of five the changes become trivial.

Give it a try: it may seem more complex, but it really, really isn't once you get your head around it!


这篇关于最大的5个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆