+ 1

Why wont this work?

I literally just started C++ and this will only print "Hello world" so why wont it print 10, I cant redo int main() so I have no idea why the test function with work it's the exact same code from the lesson with a different name #include <iostream> using namespace std; //standard namespace int main() { cout << "Hello world!" << endl; //endl means new line along with \b return 0; } /*Long comment */ /*Integer, a built-in type, represents a whole number value. Define integer using the keyword int. C++ requires that you specify the type and the identifier for each variable defined. An identifier is a name for a variable, function, class, module, or any other user-defined item. An identifier starts with a letter (A-Z or a-z) or an underscore (_), followed by additional letters, underscores, and digits (0 to 9). For example, define a variable called myVariable that can hold integer values as follows: */ int test() { int myVariable = 10; cout << myVariable; return 0; } // Outputs 10

11th May 2018, 11:43 PM
Steven4547466
Steven4547466 - avatar
4 Réponses
+ 5
You have a few options to achieve what you are trying to do. Get the function to echo the number directly, or return the number from the function to main and print it there. int test1() { return 10; } void test2() { std::cout << 10; } int main() { std::cout << test1() << "\n"; test2(); }
12th May 2018, 2:05 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
You can post a code directly in your post. There is a +on the bottom of the post editor.
12th May 2018, 2:02 AM
syul
syul - avatar
+ 1
I see... I know what I did wrong disregard this lol
11th May 2018, 11:46 PM
Steven4547466
Steven4547466 - avatar
0
It does not print 10 because test1() function is not called from main() function. Also, add a test1() function prototype before the main() function.
12th May 2018, 6:03 PM
Blade Wolfmoon
Blade Wolfmoon - avatar