Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

trying to get the word '\nNo. \n' in the middle of my output no code. replace the bottom of the output to 7 = 7 8 = 8.

0 votes
asked Oct 17, 2018 by anonymous
#include <iostream>
using namespace std;

const int SEVEN = 7, EIGHT = 8;

void fakeSwap( int x, int y );
void realSwap(int y, int x);

int main()
{
    int eight = SEVEN,
        seven = EIGHT;
    
    fakeSwap( seven, eight );
    cout << seven << " = " << SEVEN << "\nNo. \n"
            << eight  << " = " << EIGHT  << endl;

    realSwap(EIGHT, SEVEN);
    cout << seven << " = " << SEVEN << '\n'
            << eight  << " = " << EIGHT  << endl;

    return 0;
}

void fakeSwap( int x, int y ) {
    int temp = x;
    x = y;
    y = temp;
}

void realSwap( int y, int x) {
    int eight = EIGHT,
        seven = EIGHT;
}

1 Answer

0 votes
answered Jan 23, 2019 by Ryan (1,210 points)

Your issue is that the variables "seven" and "eight" are not declared in main(). They were declared as local variables in a function and cannot be accessed in main(). In order to access them in main(), you can return their values from the fakeSwap() and realSwap() functions in an std::pair. Just use std::pair<int, int> as the return type for the function. This allows a pair holding two integer elements to be returned. You can also store each element separately in its own variable. You can access the elements in the pair using this syntax:  pairName.first  for the first element,  pairName.second  for the second one. Hope this helps!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...