Output everything in Function Main | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Output everything in Function Main

Guys, if I have a simple program that combines two numbers after i collect them, I want to get out everything that's written inside the main function? Is there an idea for this or a code solution

2nd Feb 2020, 8:53 PM
S E N I O R
S E N I O R - avatar
12 Answers
+ 1
As I understand...you'r looking output to the console the contents of your cpp file. This output everything in the file...it's up to to to find a way to navigate to the "main" function and print everything after, I don't know enough to help you with that. #include <iostream> #include <fstream> using namespace std; int main(){ string myText; ifstream myinput("main.cpp"); // <-change the filename here, my working file was "main.cpp". while (getline (myinput, myText)) cout << myText << endl; return 0; }
2nd Feb 2020, 9:50 PM
rodwynnejones
rodwynnejones - avatar
+ 4
Hannah Chris I already answered, you can't do that. If I understand right, you want to add to the main function, while it is being executed, which is not possible.
2nd Feb 2020, 9:23 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Question unclear. What do you mean ? Share the code please.
2nd Feb 2020, 8:54 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
1. Can you link your code ? I don't know what it is you want to put inside the main function, so a code is needed. 2. Don't add unecessary tags in your post, only keep the ones that are relevant: C++ main-function
2nd Feb 2020, 9:04 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
You have mentioned 2 programming languages: C++ and python. Without having your current code shared here, we cannot help you. It sounds a bit as if it's your homework?
2nd Feb 2020, 9:05 PM
Lothar
Lothar - avatar
+ 2
Hannah Chris what do you mean by "add to the main function" ? You can't output something in the main function, the output is always in a consone. Even if you rewrite the file, you can't recompile it and run it as the original one, if you get what I mean.
2nd Feb 2020, 9:16 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
1-It's not my homework 2- I'm Studying Python to find a job and c++ to solve problems
2nd Feb 2020, 9:20 PM
S E N I O R
S E N I O R - avatar
+ 2
When i run the program it does all what it should do. The only thing you can improvr is to include a sequence for ‚new line‘: cout <<"\nthanks to you "; But it‘s still not clear what you exactly expect. May be you can wtite a sample what the output should looks like?
2nd Feb 2020, 9:44 PM
Lothar
Lothar - avatar
+ 1
Click this link...It can explain it better that I can... http://www.cplusplus.com/doc/tutorial/files/
2nd Feb 2020, 10:34 PM
rodwynnejones
rodwynnejones - avatar
0
The idea is, I want to print everything in the main function with all its variables and values, as if it were self-printed
2nd Feb 2020, 8:59 PM
S E N I O R
S E N I O R - avatar
2nd Feb 2020, 9:13 PM
S E N I O R
S E N I O R - avatar
0
rodwynnejones You did it, my expert friend Can you explain your code to me
2nd Feb 2020, 10:15 PM
S E N I O R
S E N I O R - avatar