简体   繁体   中英

Swapping Integers using pointer

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. 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. So i just added some code into the main function but I don't think that's what our professor wanted us to do. 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. 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. The caller's variables are not being updated.

swapIntegers() needs to look more like this instead:

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

Also, your main() is buggy . It dynamically allocates 2 int variables that it leaks and never assigns the user's input values to. The final "After swapping" output is printing out values from those pointers, not from the variables that were actually swapped. The code will NOT display the expected 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. He made a mistake in the code he gave you.

main() should look more like this:

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! . This is what the original main() looks like:

// 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() . So just revert back to the original main() code you were given. And then implement swapIntegers() properly. As the instructions told you to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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