C++ 为什么我不能给出我的函数参数?
我只是不知道我做错了什么,但是在我的尝试函数中加入任何参数都会给我一个错误C++ 为什么我不能给出我的函数参数?,c++,C++,我只是不知道我做错了什么,但是在我的尝试函数中加入任何参数都会给我一个错误 #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int guesses = 0; int attempt(int guess) { if(guesses > 0) { cout << "Take a guess!";
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int guesses = 0;
int attempt(int guess) {
if(guesses > 0) {
cout << "Take a guess!";
cout << endl;
cin >> guess;
}
}
int main() {
int answer = (rand() % 10) + 1;
int guess;
srand(time(0));
cout << "I am thinking of a number between 1 and 10. Try to guess it in 3 attempts!";
cout << endl;
attempt();
cin.get();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
整数猜测=0;
整数尝试(整数猜测){
如果(猜测>0){
猜不到;
}
}
int main(){
int answer=(rand()%10)+1;
智力猜测;
srand(时间(0));
库特
你的功能
int attempt(int guess)
^^^
需要返回一个int
值
在主体中调用此函数时,请使用输入参数正确地调用它,因此
attempt();
到
你最好做些如下的事情:
int attempt() {
int guess;
cout << "Take a guess!\n";
cin >> guess;
return guess;
}
将当前的guess
传递到尝试中是没有意义的,只有当它是一个引用参数时才会反射回调用方,而不是
我也不完全确定,当已经进行了猜测时,仅仅通过询问猜测,您希望获得什么,但这是您的旧代码的影响
您可能遇到的其他问题:
您在srand
之前调用rand
。这意味着您在每次运行时都会得到相同的随机数,因为没有设置种子的rand
的行为就好像您调用了srand(1)
guesss
变量被设置为零并且从未更改。当您着手实现“仅限三次猜测”功能时(假定在main
中有一个循环),您的函数将声明为
int attempt(int guess);
也就是说,它必须1)接受int类型的参数,2)返回int类型的对象
1)它的调用没有参数
attempt();
2) 并且在使用值参数(例如)声明函数时,不会返回int.类型的任何对象
void func(int i)
您正在声明输入
我们告诉编译器“尝试”不带任何参数,并将返回一个int值。然后在函数末尾,我们使用return
来准确地执行此操作
return
只能传回一个值-同样,稍后您将了解指针和引用,这将允许您返回大于单个值的值
要将此值接收到主服务器,请执行以下操作:
int main() {
int answer = (rand() % 10) + 1;
srand(time(0));
std::cout << "I am thinking of a number between 1 and 10. Try to guess it in 3 attempts!";
std::cout << endl;
int guess = attempt();
std::cout << "You guessed: " << guess << '\n';
cin.get();
return 0;
}
从这一切中可以看出,在不同的“作用域”中可以有相同名称的不同变量:
int i = 1; // global, visible to all functions in this compilation unit.
int main()
{
srand(time());
int i = 2;
if ((rand() % 4) == 0) // 1 in four chance
{
// this is essentially a new scope.
int i = 10;
}
// here, i = 2.
}
void foo()
{
// here, i = 1 - we see the global since we didn't declare our own.
}
void foo2(int i)
{
// here, i is whatever value we were called with.
if (i == 1)
{
int i = 99;
}
// we're back to the i we were called with.
}
一个非常基本的5YO类型问题:你得到什么错误?这与EL5无关,是不是?<代码> int(INTION)应该是“代码> int尝试(int和猜测)< /C>”,在主,<代码> TimeTo()中,应该通过<代码> int和<代码>。在编写代码之前最好先学习C++基础知识。“函数的参数太少”。int guesses=0;
…如果(guesses>0){…
和guesses
未在任何地方修改。将永远不会执行trunt()
函数中的任何内容。这当然是一个很好的观点,但不一定是一个错误。这通常是一个警告。@chris Updated。谢谢。
#include <iostream>
void func(int i)
{
// the 'i' you see here is a local variable.
i = 10;
// we changed the local thing called 'i', when
// we exit in a moment that 'i' will go away.
}
int main()
{
int i = 1;
func(i);
// if we print 'i' here, it will be our version of i.
std::cout << "i = " << i << '\n';
return 0;
}
int attempt()
{
std::cout << "Take a guess!\n";
int guess;
std::cin >> guess;
return guess;
}
int main() {
int answer = (rand() % 10) + 1;
srand(time(0));
std::cout << "I am thinking of a number between 1 and 10. Try to guess it in 3 attempts!";
std::cout << endl;
int guess = attempt();
std::cout << "You guessed: " << guess << '\n';
cin.get();
return 0;
}
int guess;
...
guess = attempt;
int i = 1; // global, visible to all functions in this compilation unit.
int main()
{
srand(time());
int i = 2;
if ((rand() % 4) == 0) // 1 in four chance
{
// this is essentially a new scope.
int i = 10;
}
// here, i = 2.
}
void foo()
{
// here, i = 1 - we see the global since we didn't declare our own.
}
void foo2(int i)
{
// here, i is whatever value we were called with.
if (i == 1)
{
int i = 99;
}
// we're back to the i we were called with.
}