[英]Hundreds of lines of garbage code from else if statement
I'm a first semester C++ student trying to make a program where, given 3 int values it finds the sum.我是第一学期的 C++ 学生,试图制作一个程序,在给定 3 个 int 值的情况下,它可以找到总和。 However, if one of the values is the same as another of the values, it does not count towards the sum The problem comes in the if / else if part of my code.
但是,如果其中一个值与另一个值相同,则不计入总和问题出在我代码的 if / else if 部分。
When I run it an enter something like 1, 1, 2 it should only add 1 and two but instead outputs hundreds of lines of garbage code.当我运行它并输入诸如 1, 1, 2 之类的东西时,它应该只添加 1 和 2,而是输出数百行垃圾代码。 I don't know what I did or what I should be doing as there aren't any errors.
我不知道我做了什么或我应该做什么,因为没有任何错误。 Entering 1, 1, 1 should and does output 1, but anything outside of that use case fails.
输入 1, 1, 1 应该并且确实输出 1,但该用例之外的任何内容都会失败。
My code is:我的代码是:
#include <iostream>
#include <stdlib.h>
using namespace std;
// Declare Function:
string loneSum(int num1, int num2, int num3);
int main()
{
//Declaring vars
int num1;
int num2;
int num3;
//Obtaining user input
cout << "Please enter number one: ";
cin >> num1;
cout << "Please enter number two: ";
cin >> num2;
cout << "Please enter number two: ";
cin >> num3;
cout << loneSum(num1, num2, num3);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sends user input to function
}
string loneSum(int num1, int num2, int num3) {
if(num1 != num2 || num3) {
cout << num1 + num2 + num3;
}
else if ((num1 == num2) && (num2 != num3)) {
cout << num2 + num3;
}
else if ((num2 == num3) && (num3 != num1)) {
cout << num1 + num3;
}
else if (num1 == num2 && num3) {
cout << "None";
}
}
The problem complained about in the title, massive “garbage” output, is caused by the fact that loneSum
is declared to return a string
, but it does not contain any return
statement.标题中抱怨的问题,大量的“垃圾”输出,是由于
loneSum
被声明为返回一个string
,但它不包含任何return
语句导致的。 So it is not returning what it promised, and then the behavior is not defined by the C++ standard.所以它没有返回它承诺的东西,然后行为不是由 C++ 标准定义的。 What happens in practice, at least in your case, is that, when
cout << loneSum(num1, num2, num3);
在实践中会发生什么,至少在你的情况下,当
cout << loneSum(num1, num2, num3);
is executed, it receives some sort of uncontrolled data back in place of a string, and it attempts to process it as a string and write it to standard output.执行时,它接收某种不受控制的数据代替字符串,并尝试将其作为字符串处理并将其写入标准输出。
To correct this, put a return
statement in loneSum
that returns a value.要纠正此问题,请在
loneSum
中放置一个return
值的return
语句。 Since you are still writing simple programs, instead of returning a string
, let's just return an int
.由于您仍在编写简单的程序,因此我们不返回
string
,而是返回int
。 Change the declaration of loneSum
to:将
loneSum
的声明改为:
int loneSum(int num1, int num2, int num3);
Change the definition to:将定义更改为:
int loneSum(int num1, int num2, int num3) {
And change the use of it in main
to:并将其在
main
的使用更改为:
cout << "The sum is " << loneSum(num1, num2, num3) << ".\n";
Now, inside loneSum
, we need to calculate the correct sum, which your code was not doing.现在,在
loneSum
,我们需要计算正确的总和,而您的代码没有这样做。 This is actually fairly easy:这实际上相当简单:
num1
is always included in the sum, because it is not a duplicate of any earlier number. num1
总是包含在总和中,因为它不是任何先前数字的重复。num2
is included if it is not a duplicate of num1
.num2
不是num1
的副本,则包括在内。num3
is included if it is not a duplicate of num1
and is not a duplicate of num2
.num3
不是num1
的副本并且不是num2
的副本,则包括它。 Code for this is:代码是:
int sum = num1;
if (num2 != num1)
sum += num2;
if (num3 != num1 && num3 != num2)
sum += num3;
Finally, we include the return
statement your program was missing:最后,我们包含您的程序缺少的
return
语句:
return sum;
Fixed the problem, I rewrote the code and it works fine.解决了这个问题,我重写了代码,它工作正常。 Here is my solution:
这是我的解决方案:
int sum = num1 + num2 + num3; // This starts by adding all three vars together
if (num1 == num2) { // checks if num1 and num2 are equal and subtracts num2
sum = sum - num2; //from the total if they are.
if (num1 == num3) { // checks if num1 and num3 are equal and
sum = sum - num3; //subtracts num3 if they are
cout << sum; // Outputs the new total
} else { // If num1 and num3 weren't equal it outputs the
cout << sum; //previous sum
}}
else if (num1 != num2) { //If num1 doesn't equal num2
if (num2 != num3) // and num2 doesn't equal num3
cout << endl << sum; // Outputs the original total
else { // If num2 does equal num3
sum = sum - num3; // Subtracts num3
cout << sum; // Outputs new total
}
}
Please take a look at this line of code from your program:请看一下您程序中的这行代码:
if(num1 != num2 || num3) {
You say you're new to C++, but I imagine you must have some familiarity with C, so I'd encourage you to briefly review two areas:你说你是 C++ 的新手,但我想你一定对 C 有点熟悉,所以我鼓励你简要回顾一下两个方面:
My guess is you'll find the error in a matter of minutes, but if you get stuck, let us know, and I'll give you some more hints.我的猜测是您会在几分钟内找到错误,但如果您遇到困难,请告诉我们,我会给您更多提示。
First of all Why are you making the function code so complex.首先你为什么要把函数代码弄得这么复杂。 The basic rule of programming is KISS(Keep it simple and straight).
编程的基本规则是KISS(Keep it simple and direct)。 like declare the function as int or float.
比如将函数声明为 int 或 float。 and instead of using three different values take an array.
而不是使用三个不同的值,而是使用一个数组。 //This is the code.//Sorry am new to stackoverflow so dont know how to post it.
//这是代码。//对不起,我是stackoverflow的新手,所以不知道如何发布它。
#include<iostream>
using namespace std;
int function(int arr[ ], int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if(arr[j]==arr[k])
{ i++; }
else
{ sum=sum+arr[i];
i++;
}
}
}
}
return sum;
}
int main()
{
int arr[]={1,1,2};
for(int i=0;i<3;i++)
{ cout<<"no "<<i+1<<"="<<arr[i]<<"\t";}//displaying numbers
cout<<"\nAnswer="<<function(arr,3);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.