Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过void函数检索所有数据_C++_Function_Parameter Passing_Cin - Fatal编程技术网

C++ 通过void函数检索所有数据

C++ 通过void函数检索所有数据,c++,function,parameter-passing,cin,C++,Function,Parameter Passing,Cin,我一直在想办法解决这个问题。我是新的C++,并创建一个简单的程序来获取用户数据,验证和CUT到屏幕。我想做的是让one函数使用指针来获取用户的输入并显示给他们。这个问题以前可能已经得到了回答,但我没有太多的运气找到它 到目前为止,我有以下代码 #include <iostream> using namespace std; void userData(int&); int main(){ int a = 0; int * kmpointer; int * days

我一直在想办法解决这个问题。我是新的C++,并创建一个简单的程序来获取用户数据,验证和CUT到屏幕。我想做的是让one函数使用指针来获取用户的输入并显示给他们。这个问题以前可能已经得到了回答,但我没有太多的运气找到它

到目前为止,我有以下代码

#include <iostream>

using namespace std;

void userData(int&);

int main(){
int a = 0;
int * kmpointer;   
int * dayspointer;

userData();

cout << "You ran " << userData(kmpointer) << endl;    
cout << "in " << userData(dayspointer) << "days!!" <<endl;

}

void userData(int& i){
cout << "Enter how Many Km's you ran:";

while (true)
{
    cin >> kmpointer;

    if ((cin) && (kmpointer >= 0) && (inputYear <= 100))
    break;

cin.clear();
cin.ignore( 100, '\n' );
cout << "That can't be right!\n";
cout << "Enter how Many Km's you ran:";
}

cout << "How many days in a row did you run?";
while (true)
{
    cin >> dayspointer;

    if ((cin) && (dayspointer >= 1) && (dayspointer <= 100))
    break;

cin.clear();
cin.ignore( 1000, '\n' );
cout << "Thats way to much!";
cout << "How many days in a row did you run? ";
}

}
IMO,你应该先读一些关于C++的文章。您缺少一些基本概念,并且尝试的练习过于复杂,与您的水平不符

一, 函数未声明/定义

二, userData声明为接受参数,但使用时不带参数

三, 您所面临的问题可能与我们所称的作用域有关:变量只存在于其作用域内,并且在通常由{和}包围的作用域内可见

在您的情况下,kmpointer和daysInternal仅在main函数中可见,因此不能在userData中使用它们

为了解决这个问题,我建议您将这些变量作为userData的参数传递

四, 指针、引用、值:它们是不同的。您正在将用户输入保存为指针地址,这确实有问题

一般的


一般来说,您的代码充满了错误。尝试一个你好的世界!然后从那里一步一步地继续。

专注于您提出的特定问题,尽管观察到您的代码中还有其他问题,不要使用指针,而是使用引用

在我们讨论这个之前

cout << "You ran " << userData(kmpointer) << endl;

你的问题是什么?你的代码充满了错误,试着逐一解决它们-抱歉-尝试从函数中检索用户输入并使用指针将其显示到屏幕。IM只是新的C++,并慢慢地通过它的方式。USEDATA引用一个int,所以发送一个int,像USEDATAA,EXCDCPSP,我怀疑你想把它全部发送给三个INT。此外,CIN不适用于指针,你希望它工作,甚至不使用C++中的原始指针,它们只在非常特殊的情况下才有用,并且学习参考文献。
#include <iostream>

using namespace std;

void userData(int& i, int& j, int& k);

int main() {
    int a = 0;
    int kmpointer;
    int dayspointer;

    //Here we call our function, ONCE
    userData(a, kmpointer, dayspointer);

    //Here we display what values we now have
    //after calling the function, ONCE
    cout << "You ran " << kmpointer << endl;
    cout << "in " << dayspointer << " days!!" << endl;

}

//simplified to demonstrate changes to the reference parameters
void userData(int& i, int& j, int& k) {
    //Here we have three parameters which we refer to as i, j and k
    // They may have different names ousdie in the calling code
    //  but this function (scope) neither knows nor cares
    j = 42;
    k = 101;
}