// test.cpp - Script 6.7

#include <iostream>

// Function prototype.
// Function takes two arguements.
// Function returns no values.
void changeVar(int *myVar, int newValue);

int main() {
    
    // Create a new variable.
    int myNum = 20;
    
    // Print its current value.
    std::cout << "myNum is "
    << myNum << "\n";
    
    // Call the function.
    changeVar(&myNum, 90);
    
    // Print its value again.
    std::cout << "After calling the function, myNum is now "
    << myNum << "\n";
    
    return 0;

} // End of the main()

// Function definition.
void changeVar(int *myVar, int newValue) {
     
     // Assign the new value to the variable.
     *myVar = newValue;
     
     } // End of changeVar().

I don't understand what is wrong with this code? It's compiling but the console is opening and closeing really quickly without showing the output?

No, I'm saying that the console is the medium within which the code runs. When you hit "Run" or some such from your fancy IDE the console is opened only to run the programme. There's no reason why it would stay open when the programme is finished. If you want it to stay open, do something to make it stay open (such as stop the programme finishing until you're ready). Alternatively, run your executable from a console.

It's all covered in those links. It's nothing to do with your code; it's the environment you're running your executable in.

thanks for your help I got it working with:
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return 0;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.