Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a problem. Here is the simplified version:
#include <iostream>
using namespace std;
int main() {
cout << "Hello!";
return 0;
}
string name = "My name is ______";
int main2() {
cout << name;
return 0;
}
I have tried to remove the first return 0; in the main(), but it did nothing. I want to somehow start main2() from main(). Is it possible?
I tried running the code(I use repl.it) and it didn't return any error messages. I also tried running it from the Terminal and it just prints Hello!.
Yes. Try something like this:
#include <iostream>
using namespace std;
// Forward declare |main2|.
int main2();
int main() {
cout << "Hello!";
main2();
return 0;
}
string name = "My name is ______";
int main2() {
cout << name;
return 0;
}
To elaborate on the differences between C++ and Python here: In C++ main is the entry point for your program. So in C++, you can think of the start of the main function as the equivalent of Python's "top of the file". Your program will begin running at the top of main, and stop running at the end of it. Anything that is not invoked starting from the main function (or transitively invoked) will not be executed.
What you wrote is roughly equivalent to the following Python:
def main():
print('Hello')
return 0
name = 'My name is ____'
def main2():
print(name)
return 0
if __name__ == '__main__'
sys.exit(main())
I believe now it's clear why main2 never gets executed: it's never called.
Note that the main function in C++ is the one function which gets called automatically at program start, and exiting main terminates the program. Whatever you want to happen while your program is running must be called from within main.
Also note that a name (such as a function) must be declared before it can be used. So you'll have to either move the definition of main2 before that of main, or at least declare main2 there. Which you could do like this:
#include <iostream>
using namespace std;
int main2();
int main() {
cout << "Hello!";
return main2();
}
string name = "My name is ______";
int main2() {
cout << name;
return 0;
}
You have to call main2() inside main():
#include <iostream>
using namespace std;
string name = "My name is ______";
int main2() {
cout << name;
return 0;
}
int main() {
cout << "Hello!";
main2();
return 0;
}
Related
This question already has answers here:
Making variables persist even after program termination
(3 answers)
I want to store a value even after my program ends
(1 answer)
Closed 2 years ago.
Most (if not all) games will remember the things (money, village size, manager amount, etc.) that you have so that when you start the game again, you still have everything you had obtained when you last left off. I am programming a game that needs to remember values, and I declare them initially at the beginning of the program. But since money equals zero, every time the user comes back to play the game, their earnings will be reset.
I will ultimately need this for most of my variables but I am actively working on Tutorial();. I am going to need the tutorial to only run once so I need the program to check whether the user has completed the tutorial every time the program begins. I have tried setting up a boolean value called isTutorialCompleted and having it check that in main when I run my functions. Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Game Variables
int money = 0;
string existingManagers = { "Benny, Rachel, Barnes, Flora, Gregory" };
string createManager = "";
vector<string> createdManagers {""};
bool isTutorialCompleted = false;
void initApp () {
}
void Tutorial() {
cout << "Please Enter your name" << "\n";
cin >> createManager;
createdManagers.push_back(createManager);
cout << "\n" << "You have created Manager " << createManager;
}
int main() {
initApp();
if (isTutorialCompleted == false) {
Tutorial();
}
}
This did not work due to the fact that every time I restarted the program the boolean value would reset so I tried making the boolean value undefined initially and then having it change in Tutorial. Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Game Variables
int money = 0;
string existingManagers = { "Benny, Rachel, Barnes, Flora, Gregory" };
string createManager = "";
vector<string> createdManagers {""};
bool isTutorialCompleted;
void initApp () {
}
void Tutorial() {
isTutorialCompleted = false;
cout << "Please Enter your name" << "\n";
cin >> createManager;
createdManagers.push_back(createManager);
cout << "\n" << "You have created Manager " << createManager;
isTutorialCompleted = true;
}
int main() {
initApp();
Tutorial();
}
The problem with this is that it would not keep the value. isTutorialCompleted would go back to undefined in the program and ultimately change to false when tutorial begins. Finally, I tried checking for the boolean value in initApp();. Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Game Variables
int money = 0;
string existingManagers = { "Benny, Rachel, Barnes, Flora, Gregory" };
string createManager = "";
vector<string> createdManagers {""};
bool isTutorialCompleted;
void initApp () {
//Check if tutorial is completed
if (isTutorialCompleted = true) {
Tutorial();
}
}
void Tutorial() {
cout << "Please Enter your name" << "\n";
cin >> createManager;
createdManagers.push_back(createManager);
cout << "\n" << "You have created Manager " << createManager;
isTutorialCompleted = true;
}
int main() {
initApp();
}
So the ultimate question is: How do you permanently store a variable so that when you restart the program, the stored variable does not reset?
Store and import from a config file whenever the game starts.
I've read that an #include header.h is a preprocessor (because of #), which means it gets processed before compilation.
Is that why my code can't run? Because I'm trying to make an if statement in main with my function from my header(that takes a parameter) and it won't work.
Source.cpp
#include <iostream>
#include "Header.h"
using namespace std;
int main(){
test(46);
if (test() > 30){
cout << "great";
}
else{
cout << "It needs to be higher";
}
system("PAUSE");
return 0;
}
Header.h
using namespace std;
int test(int x){
return x;
}
That isn't the problem. I suspect you might be getting a compiler error message (or linker error) because you have declared test(int x) with an integer parameter and then you call it with no parameter, e.g.: test().
I've modified your code to include an integer result:
int main(){
int result = test(46); // Save the result of calling the function
if (result > 30){ // Test the value of the result
cout << "great";
}
else{
cout << "It needs to be higher";
}
system("PAUSE");
return 0;
}
The test function in Header.h file takes a int as parameter.But in your code you lose it.Pass a int to test function like this.
if (test(42) > 30)
You will get the output: great.
I recently started learning C++ but I came across a problem. The program given below is not giving me the desired result as I only see 'Hi' in the result but not what's written in the void function. Please tell me the reason that this is happening along with the solution.
I am using Xcode 6.3.1 and the I have selected the language C++.
#include <iostream>
using namespace std;
void ABC () {
cout << "Hey there ! \n";
}
int main () {
cout << "Hi \n";
void ABC ();
return 0;
}
You are redeclaring a void ABC() function inside main(). Just call ABC(); without the void.
You can take a look at this question about declaring a function within the scope of another.
In your code your function call was wrong.
When you call your function you don't need to add the return type:
#include
void ABC () {
cout << "Hey there ! \n";
}
int main () {
cout << "Hi \n";
ABC ();
return 0;
}
you need to call your method and not declare it inside main
#include <iostream>
using namespace std;
void ABC () {
cout << "Hey there ! \n";
}
int main ()
{
cout << "Hi \n";
ABC ();
return 0;
}
EDIT 1:
Since you started learning C++ i recommend the following recommendations to make sure your code is cleaner. Please note , these are not rules by any mean , but more of best practices and a style of coding.
Use meaningful names for your variables, methods, functions , classes
... So instead of ABC() name it something that if you (or someone
else is reading it) will now what it suppose to do.
When calling methods and functions try to declare them with the
appropriate returning value. Void by definition doesn't return any
value it just process the code inside of it. so your methods/function
should return appropriate values of do what it suppose to.
Here's version 2 of your code with examples of 3 different methods and calls:
#include <iostream>
using namespace std;
int sum;
string MethodReturningString()
{
return "Hey there i am the result of a method call !";
}
int MethodReturningInt()
{
return 5;
}
void CalculateSum(int x,int y)
{
sum=x+y;
}
int main()
{
cout << MethodReturningString() << endl;
cout << MethodReturningInt() << endl;
cout << "Calculating sum:" ;
CalculateSum(5,4);
cout << sum << endl;
return 0;
}
Happy coding
In C++, like pretty much any other language, you do not specify the return type when calling a function. So change the line that reads:
void ABC ();
to:
ABC();
Try this:
#include <iostream>
using namespace std;
void ABC () {
cout << "Hey there ! \n";
}
int main () {
cout << "Hi \n";
ABC();
return 0;
}
You should call a function simply by stating its name and adding parentheses.
instead of using void ABC() for calling the function ABC() in main(), use the following code:
#include
void ABC ()
{
cout << "Hey there ! \n";
}
int main ()
{
cout << "Hi \n";
ABC ();
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Im having tropel with a little project of mine and would like some help.
this is the code so far.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(timetaker)
{
}
int after()
{
srand(data from above);
for (int x = 1; x<2;x++)
{
cout << 1+(rand()) << endl;
}
}
what im having trople with is the function that takes time and gives it to the int after() function. But i would be gratefull for some help with the int main (timetaker)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
std::cout << "First Click: ";
std::cin.ignore();
unsigned int start = clock();
std::cin.ignore();
std::cout << "Next when you are ready ";
std::cin.ignore();
std::cout << "Time taken in millisecs: " << clock() << endl;
std::cout << "Now for the random number. Are you ready" << endl;
std::cin.ignore();
srand(clock());
for (int x = 1; x<2;x++)
{
cout << 1+(rand()) << endl;
}
std::cout << "That is the random number from the time taken.";
return 0;
}
It's easier if you put the code you want to call before the place you want to call it from. Getting a time value for srand(), and passing it from main(), can be done as illustrated below...
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void after(time_t seed)
{
srand(seed);
for (int x = 1; x<2;x++)
{
cout << 1+(rand()) << endl;
}
}
int main()
{
do_stuff(time(NULL));
}
Considering the very vague nature of this question, I'd say this is your best resource:
http://www.cplusplus.com/reference/ctime/time/
I don't see any question. By the way, you're missing the return value for main and after. Also you can't do int main (timetaker) what should that be ?
Your srand function should work with data from the main function ? You will need to pass some parameters in your function int after.
I also would not recommend using using namespace std; as this may cause undefined behavior if you're going to implement, for instance, your own cout function. Using std:: is the better way. Still, it's your choice, and in this code it's fine.
If you want to work with time, you may check out those links:
Time
Clock
Here is my code in C++:
#include <iostream>
using namespace std;
int tuna = 20; // this is global
//main function
int main()
{
int tuna = 69; // this is local
cout << tuna << endl;
}
//fish function
int fish()
{
cout << tuna << endl; // this should print global?
}
The fish function doesn't print to the console when I run it. I am not sure why and it doesn't make sense to me.
You are not calling fish() so it doesn't seem strange its body is is not executed.
Try with:
int main()
{
fish();
return 0;
}
because main() is the only possible entry point for your program and the only way to call other, user-defined, functions.
Do you ever call the function (fish) ? Not in your sample.
Because you don't call it at all.