NameSilo

C++ help needed

Spaceship Spaceship
Watch
Impact
34
I'm trying to learn C++ and when I compile
PHP:
#include <stdio.h>

int main()
{
    printf("Lol its line one!\n");
    printf("Line 2\n");
    printf("LINE THREE WTFHAX;\n");
}
in Dev-C++ it just flashes for a second, the book im reading says press alt f5, but this is for C and is pretty old and uses the DOS operating system, anyone know what to do? I made a better script before but closed it because it would work

EDIT: figured it out
add this to the header
HTML:
#include <stdlib.h>

then add this
PHP:
    system("pause");
to the bottom just above the } tag
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
The same thing happened to me when I was trying to create something similar, so I'll be watching this topic. I couldn't figure out why.
 
0
•••
Awesome :), this is really annoying me, I keep googling it but it isn't showing any results :(
 
0
•••
Its because the compiler will run the main() method, printing all those lines and then close itself. As a program which leads to the end of the main() method assumes its finished with.

You need to pause it as you did, or ask it to wait for input using the std::cin function.

Alernatively export an exe and run it in command prompt
 
0
•••
For the record, you're coding C. In C++, your program would resemble:

Code:
#include <iostream>
using namespace std;

int main()
{
    cout << "Lol its line one!" << endl;
    cout << "Line 2" << endl;
    cout << "LINE THREE WTFHAX" << endl;
    system("pause");
    return 0;
}
 
Last edited:
0
•••
mholt said:
For the record, you're coding C.

C++ is a superset of C, so the original code is perfectly valid as either C or C++.
 
0
•••
Well, I know in VB and C# you need to have a readline at the end of the program to make it idle and wait for the user to close the window. Just throw a readline command at the end of the program. It should work.

This might not be necessary in C though...
 
0
•••
#include <stdio.h>

int main()
{
printf("Lol its line one!\n");
printf("Line 2\n");
printf("LINE THREE WTFHAX;\n");
}
The problem is that this algorhitm doesn't wait for any input, it just resolves and shuts down. You need to include getchar() and return 0... Here:

#include <stdio.h>

int main()
{
printf("Lol its line one!\n");
printf("Line 2\n");
printf("LINE THREE WTFHAX;\n");
getchar();
getchar();
return 0;
}
What does getchar() do? It stops the program and waits until you press enter - then the program continues (it can also be used to recognize what key did user press...).

return 0 ends the program.

By the way, that is pure C code - you don't use any C++ functions or libraries there ;) In C++ it would be like this:

#include <iostream>
using namespace std;

int main()
{
cout >> "Lol its line one!\n";
cout >> "Line 2\n";
cout >> "LINE THREE WTFHAX;\n";
getchar();
getchar();
return 0;
}
;)
 
0
•••
risoknop said:
The problem is that this algorhitm doesn't wait for any input, it just resolves and shuts down. You need to include getchar() and return 0... Here:


What does getchar() do? It stops the program and waits until you press enter - then the program continues (it can also be used to recognize what key did user press...).

return 0 ends the program.

By the way, that is pure C code - you don't use any C++ functions or libraries there ;) In C++ it would be like this:


;)
Did you even read the thread - no - just his original post even? You just made yourself look rather unintelligent... :|

qbert220 said:
C++ is a superset of C, so the original code is perfectly valid as either C or C++.
True, however while the code is valid, I don't think that you can say the above code is C++ just by looking at that. I think in order for it to be C++ you need to use at least one C++ concept/aspect/feature.

Header files with .h extension generally are not in and of themselves C++, as they are plain ol' C libraries.

Oh well. Doesn't matter. :p
 
0
•••
Are you trying to create a command prompt or WinAPI? I can help if you let me know. I wouldn't recommend using 'printf' for command prompt in C++ :p programming with 'cout >>' - a lot better for your purposes.

-RageD
 
0
•••
Did you even read the thread - no - just his original post even? You just made yourself look rather unintelligent...

Maybe I'm just too tired but I really don't see how I've made myself look dumb. Care to explain?

If I did then I apologize.
 
0
•••
Well, he stated his resolution at the bottom of his OP, then you just repeated what was already said in the thread. I've made similar mistakes too... we all do.
 
0
•••
Yeah I messed up. Sorry, my bad.
 
1
•••
Lol, it's fine... I see you're sorta new here, so I guess it's just a heads up to read the thread. Anyway, welcome to NamePros. It's great that you are contributing, and we all love to see it!
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back