为什么我的用户自定义函数在我的主函数中没有被读取?

huangapple go评论74阅读模式
英文:

Why is my user-defined function not being read in my main function?

问题

我遇到了一个问题,我的用户定义函数无法在主函数中读取,有什么建议或提示可以导致这种情况吗?

我尝试了不同的参数,将函数从主函数下面移到上面,但它仍然完全忽略了这段代码

#include <iostream>
using namespace std;
int Fahrenheit, Celsius ;  
char y, Y ;

using namespace std;
char Answer ; 

int FtoCTemp (int argc, char** argv){
    
    cout << "What Fahrenheit  Value Do You Want in Celsius" << endl; 
    cin >> Fahrenheit  ;                                                    
    Celsius = (Fahrenheit -32);             // 使用两个方程式以获得正确的输出                    
    Celsius = (Celsius * 5/9);
    
    cout << Celsius << " Celsius" << endl;   // 在方程式后打印摄氏度
    cout << "Would you like to enter another Fahrenheit" << endl;
    cin >> Answer;   // 捕获答案 
}

int main(int argc, char** argv) {
    do{      
        
        int FtoCTemp (int argc, char** argv);
     
        cout << "Would you like to enter another Fahrenheit" << endl;
        cin >> Answer;   // 捕获答案 
        
    } while (Answer == 'y' || Answer == 'Y');
    
    cout << "Thank you!";
    return 0;
}
英文:

Im having an issue with getting my user-defined function to read in my main function, any advice or tips on what can cause this?

i tried different parameters, moving the function from below the main to above and it still ignores the code all together

#include &lt;iostream&gt;
using namespace std;
int Fahrenheit, Celsius ;  
char y, Y ;
/* run this program using the console pauser or add your own 
getch, system(&quot;pause&quot;) or input loop */

using namespace std;
  char Answer ; 

	int FtoCTemp (int argc, char** argv){
	
		cout &lt;&lt; &quot;What Fahrenheit  Value Do You Want in Celsius&quot; &lt;&lt; endl; 
	cin &gt;&gt; Fahrenheit  ;													
	
	Celsius = (Fahrenheit -32);			 //2 equations used to get correct ouput					
	Celsius = (Celsius * 5/9);
	
	cout &lt;&lt; Celsius &lt;&lt; &quot; Celsius&quot; &lt;&lt; endl;   // prints Celsius after equation
	cout &lt;&lt; &quot;Would you like to enter another Fahrenheit&quot; &lt;&lt; endl;
	cin &gt;&gt; Answer;   // Captures answer 
	}

int main(int argc, char** argv) {
	do{      
	 	
	 	int FtoCTemp (int argc, char** argv);
	 
	cout &lt;&lt; &quot;Would you like to enter another Fahrenheit&quot; &lt;&lt; endl;
	cin &gt;&gt; Answer;   // Captures answer 
	
	} while (Answer == &#39;y&#39; || Answer == &#39;Y&#39;);
	
	cout &lt;&lt; &quot;Thank you!&quot;;
	return 0;
}

答案1

得分: 2

试试只写 FtoCTemp (argc, argv);,而不是在循环中重新声明函数,而不是 int FtoCTemp (int argc, char** argv);

英文:

it seems like you're redeclaring the function in the loop, instead of
int FtoCTemp (int argc, char** argv); , try just writing FtoCTemp (argc, argv);

答案2

得分: 0

以下是您要翻译的代码部分:

主要问题如前一个答案中所指出的那样。还有一些其他问题,包括两次要求“...输入另一个...”和FtoCTemp()的返回值。我在下面放了一个合适的版本并附上一些解释。

#include <iostream>
using namespace std;

// 声明在所有函数体外部的变量是全局变量,可以被任何函数访问。为了代码清晰起见,尽量不要多次使用它们。

// int Fahrenheit, Celsius; // 由于这些变量仅在FtoCTemp()中使用,因此将它们移到FtoCTemp()体内。
// char y, Y; // 这些变量没有被使用。

// using namespace std; // 重复。
// char Answer; // 由于这个变量仅在main()中使用,因此将它移到main()体内。

// 将此函数的返回类型从“int”更改为“void”,因为它没有返回值。
void FtoCTemp(int argc, char **argv)
{
    int Fahrenheit, Celsius;

    cout << "要将华氏温度转换为摄氏温度,请输入华氏温度:" << endl;
    cin >> Fahrenheit;

    // 这两行代码可以合并成一行。
    // Celsius = (Fahrenheit -32);
    // Celsius = (Celsius * 5/9);
    Celsius = (Fahrenheit - 32) * 5 / 9;

    cout << Celsius << " 摄氏度" << endl;

    // 这里的询问“...输入另一个...”已被删除,因为在main()中已经询问过了。
    // cout << "您想输入另一个华氏温度吗?" << endl;
    // cin >> Answer; // 捕获答案

    return; // 为了更好的编码规范,始终在最后一行使用“return”。
}

int main(int argc, char **argv)
{
    char Answer;

    do
    {
        FtoCTemp(argc, argv);

        cout << "您想输入另一个华氏温度吗?" << endl;
        cin >> Answer; // 捕获答案

    } while (Answer == 'y' || Answer == 'Y');

    cout << "谢谢!";
    return 0;
}
英文:

The main problem is just as pointed out in the previous answer. There are also a few other problems, including double asking "...enter another..." and return value of FtoCTemp(). I put a proper version below with some explaination.

#include &lt;iostream&gt;
using namespace std;

// Variables declared outside all function bodies are global variables, which can be accessed by any function. For code clarity, try not to use them many.

//int Fahrenheit, Celsius ; // Since these variables are only used in FtoCTemp(), so move them into FtoCTemp() body.
//char y, Y ; // These variables are not used.

//using namespace std; // Duplicated.
//char Answer ; // Since this variable is only used in main(), so move it into main() body.

// Change return type of this function from &quot;int&quot; to &quot;void&quot; since it has no return value.
void FtoCTemp (int argc, char** argv){
	int Fahrenheit, Celsius;

	cout &lt;&lt; &quot;What Fahrenheit  Value Do You Want in Celsius&quot; &lt;&lt; endl; 
	cin &gt;&gt; Fahrenheit  ;                                                    
    
	// These two line code can be combined into one.
	//Celsius = (Fahrenheit -32);
	//Celsius = (Celsius * 5/9);
	Celsius = (Fahrenheit - 32) * 5 / 9;

	cout &lt;&lt; Celsius &lt;&lt; &quot; Celsius&quot; &lt;&lt; endl;

	// The asking &quot;...enter another...&quot; here is removed because it has been asked in main().
	//cout &lt;&lt; &quot;Would you like to enter another Fahrenheit&quot; &lt;&lt; endl;
	//cin &gt;&gt; Answer;   // Captures answer 

	return; // For better coding custom, always &quot;return&quot; at the last line.
}

int main(int argc, char** argv) {
	char Answer;

	do{      
		FtoCTemp (argc, argv);
     
		cout &lt;&lt; &quot;Would you like to enter another Fahrenheit&quot; &lt;&lt; endl;
		cin &gt;&gt; Answer;   // Captures answer 
    
	} while (Answer == &#39;y&#39; || Answer == &#39;Y&#39;);
    
	cout &lt;&lt; &quot;Thank you!&quot;;
	return 0;
}

huangapple
  • 本文由 发表于 2023年6月5日 10:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定