2014-10-04 22 views
-5

我试图将一个int值传递给一个函数,该函数向用户返回一个char值。将char值和int值传递到一个函数

像这样的事情

// this is the function call 
    option = function1(cus_num) 

    char function1 (int num1){ 
    char test 
    // displays options for user to input char 

    cout << "Customer #" << num1 << "plese enter an option"; 
    cin >> test; 

    return test; 

但每次我这样做的时候,该NUM1显示为#0x47e7484替代的价值。我知道该函数设置为返回一个字符,但我不能传递一个int给它吗?

+2

请示出了[完整最小示例](https://stackoverflow.com/help/mcve)。 – 5gon12eder 2014-10-04 21:33:46

+0

“test”(第一次)后面缺少';'。 – 2014-10-04 21:50:36

回答

1

你可以尝试:

cout << "Customer #" << dec << num1 << "plese enter an option"; 

给定的值的性质,我怀疑别的东西是错误的。

+0

感谢您的回复。这似乎并不奏效。我正在检查我的代码,看看还有什么可能是错的。如果可以的话,我会给你一个+1! – srallen87 2014-10-04 21:39:20

+0

如上所述,我怀疑在你的代码中还有别的东西会导致腐败。如果你之前做过十六进制,dec只能工作。 – user3344003 2014-10-04 21:45:26

+0

由于在制作'cout'之前'dec'从未设置过,您会收到意想不到的行为...... – 2014-10-04 21:51:10

1

此代码相同的,并且是给正确的输出: -

#include <iostream> 

using namespace std; 
char function1 (int num1) 
{ 
    char test ; 
    // displays options for user to input char 
    cout<<"Customer #"<<num1<<" please enter an option: "; 
    cin>> test; 
    cout<<"\n";  //Just to make output cleaner 
    return test; 
} 

int main() 
{ 
    char a; 
    a = function1(3); 
    cout<<a; 
}