gpt4 book ai didi

c++ - 为什么我需要在用户为它赋值之前初始化这个变量?

转载 作者:行者123 更新时间:2023-11-27 22:47:22 25 4
gpt4 key购买 nike

<分区>

我希望用户为 int temp1 和 int temp2 分配一个值。但是,编译器说我需要初始化两个变量之一 (temp2)。

为什么它只要求我初始化 temp2 而不是 temp1?当我为 temp2 赋值时,程序会忽略用户输入的任何值。

我的代码是否马虎?如果是,有什么办法可以解决这个问题吗?

(我已经包含了整个程序,以防它是相关的,但是我收到的错误是在 inputDetails() 函数中。)

#include <iostream>
using namespace std;

//Prototype

void inputDetails(int* n1, int* n2);
void outputDetails(int num1, int num2, int* pNum, int* n1, int* n2, int** ppNum);

//Functions

int main()
{
int num1;
//num1 pointer
int* n1 = &num1;

int num2;
//num2 pointer
int* n2 = &num2;

//get pNum to point at num1
int* pNum;
pNum = new int;
*pNum = num1;

//pointer to pNum
int** ppNum = &pNum;

//call functions
inputDetails(n1, n2);
outputDetails(num1, num2, pNum, n1, n2, ppNum);

//change pNum to point at num2
delete pNum;
pNum = new int;
*pNum = num2;

//call function again
outputDetails(num1, num2, pNum, n1, n2, ppNum);
delete pNum;

system("PAUSE");
return 0;
}

void inputDetails(int* n1, int* n2)
{
int temp1, temp2;
cout << "Input two numbers" << endl;
cin >> temp1, temp2;
*n1 = temp1;
*n2 = temp2;
}

void outputDetails(int num1, int num2, int* pNum, int* n1, int* n2, int** ppNum)
{
cout << "The value of num1 is: " << num1 << endl;
cout << "The address of num1 is: " << n1 << endl;
cout << "The value of num2 is: " << num2 << endl;
cout << "The address of num2 is: " << n2 << endl;
cout << "The value of pNum is: " << pNum << endl;
cout << "The dereferenced value of pNum is: " << *pNum << endl;
cout << "The address of pNum is: " << ppNum << endl;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com