简体   繁体   English

使用指针交换整数

[英]Swapping Integers using pointer

1: http://itweb.fvtc.edu/ag/?u=3&f=cpp-assignment3 1: http://itweb.fvtc.edu/ag/?u=3&f=cpp-assignment3

I am to swap the integer using a SwapInteger function outside the main function type using pointer.我将使用指针在主 function 类型之外使用 SwapInteger function 交换 integer 。 The user input a number and then the computer will compile and change the result to the given result that our professor have assigned.用户输入一个数字,然后计算机将编译并将结果更改为我们教授分配的给定结果。

I've tried creating a void swapInteger function and input some code in to see if that swap the code but that does nothing.我尝试创建一个 void swapInteger function 并输入一些代码以查看是否交换了代码,但这无济于事。 So i just added some code into the main function but I don't think that's what our professor wanted us to do.所以我只是在主要的 function 中添加了一些代码,但我认为这不是我们的教授希望我们做的。 He did stated "do not modify the main function"他确实说过“不要修改主要功能”

#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

// TODO: Implement the "SwapIntegers" function

void swapIntegers(int *first, int *second)
{
    int *pSwapIntegers = first;
    first = second;
    second = pSwapIntegers;
}


// Do not modify the main function!
int main()
{
    int first = 0;
    int second = 0;
    int *pFirst = new int (first);
    int *pSecond = new int (second);


    cout << "Enter the first integer: ";
    cin >> first;

    cout << "Enter the second integer: ";
    cin >> second;

    cout << "\nYou entered:\n";
    cout << "first: " << first << "\n";
    cout << "second: " << second << "\n";


    swapIntegers(&first, &second);

    cout << "\nAfter swapping:\n";
    cout << "first: " << *pFirst << "\n";
    cout << "second: " << *pSecond << "\n";

    cout << "\nPress any key to quit.";

    _getch();
    return 0;
}

I expected the computer to compile the two integer that the user input and then show the swapped integer to the user.我希望计算机编译用户输入的两个 integer,然后将交换的 integer 显示给用户。 Please take a look at my code if you have questions如果您有任何问题,请查看我的代码

Inside of your swapIntegers() , you are swapping the pointers themselves, not the values of the variables they are pointing at.在您的swapIntegers()内部,您正在交换指针本身,而不是它们指向的变量的值。 The caller's variables are not being updated.调用者的变量没有被更新。

swapIntegers() needs to look more like this instead: swapIntegers()需要看起来更像这样:

void swapIntegers(int *first, int *second)
{
    int saved = *first;
    *first = *second;
    *second = saved;
}

Also, your main() is buggy .此外,您的main()错误的。 It dynamically allocates 2 int variables that it leaks and never assigns the user's input values to.它动态分配它泄漏的 2 个int变量,并且从不分配用户的输入值。 The final "After swapping" output is printing out values from those pointers, not from the variables that were actually swapped.最终的"After swapping" output 打印出这些指针的值,而不是实际交换的变量。 The code will NOT display the expected output.该代码不会显示预期的 output。 So, despite what the instructions say, main() NEEDS to be modified in order to operate properly, and if your professor has a problem with that, tough.所以,不管说明书怎么说, main()需要修改才能正常运行,如果你的教授对此有疑问,那就很难了。 He made a mistake in the code he gave you.他在给你的代码中犯了一个错误。

main() should look more like this: main()应该看起来更像这样:

int main()
{
    int first = 0;
    int second = 0;

    cout << "Enter the first integer: ";
    cin >> first;

    cout << "Enter the second integer: ";
    cin >> second;

    cout << "\nYou entered:\n";
    cout << "first: " << first << "\n";
    cout << "second: " << second << "\n";

    swapIntegers(&first, &second);

    cout << "\nAfter swapping:\n";
    cout << "first: " << first << "\n";
    cout << "second: " << second << "\n";

    cout << "\nPress any key to quit.";

    _getch();
    return 0;
}

Or, like this:或者,像这样:

// Do not modify the main function!
int main()
{
    int first = 0;
    int second = 0;
    int *pFirst = &first;
    int *pSecond = &second;

    cout << "Enter the first integer: ";
    cin >> first;

    cout << "Enter the second integer: ";
    cin >> second;

    cout << "\nYou entered:\n";
    cout << "first: " << first << "\n";
    cout << "second: " << second << "\n";

    swapIntegers(&first, &second);

    cout << "\nAfter swapping:\n";
    cout << "first: " << *pFirst << "\n";
    cout << "second: " << *pSecond << "\n";

    cout << "\nPress any key to quit.";

    _getch();
    return 0;
}

Or, like this:或者,像这样:

int main()
{
    int *pFirst = new int (0);
    int *pSecond = new int (0);

    cout << "Enter the first integer: ";
    cin >> *pFirst;

    cout << "Enter the second integer: ";
    cin >> *pSecond;

    cout << "\nYou entered:\n";
    cout << "first: " << *pFirst << "\n";
    cout << "second: " << *pSecond << "\n";

    swapIntegers(pFirst, pSecond);

    cout << "\nAfter swapping:\n";
    cout << "first: " << *pFirst << "\n";
    cout << "second: " << *pSecond << "\n";

    delete pFirst;
    delete pSecond;

    cout << "\nPress any key to quit.";

    _getch();
    return 0;
}

UPDATE : oh wait, it is not your professor's fault, it is YOUR fault.更新:哦等等,这不是你教授的错,而是你的错。 The main() you have presented here DOES NOT match the main() given in the actual assignment!您在此处提供的main() () 与实际作业中给出的main()不匹配! . . This is what the original main() looks like:这是原始main()的样子:

// Do not modify the main function!
int main()
{
    int first = 0;
    int second = 0;

    cout << "Enter the first integer: ";
    cin >> first;

    cout << "Enter the second integer: ";
    cin >> second;

    cout << "\nYou entered:\n";
    cout << "first: " << first << "\n";
    cout << "second: " << second << "\n";

    SwapIntegers(&first, &second);

    cout << "\nAfter swapping:\n";
    cout << "first: " << first << "\n";
    cout << "second: " << second << "\n";

    cout << "\nPress any key to quit.";

    _getch();
    return 0;
}

This code is correct.这段代码是正确的。 So YOU are the one who introduced the bad use of pointers in main() .因此,您是在main()中引入了错误使用指针的人。 So just revert back to the original main() code you were given.因此,只需恢复为您提供的原始main()代码。 And then implement swapIntegers() properly.然后正确实施swapIntegers() As the instructions told you to do.正如指示告诉你的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM