Beginner with cpp and h files - help

Hey, I'm new here, and I'm learning C++ you help me if this right?, is it professional?
BTW I'm using, Visual Studio 2017 win32 debug

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include "core_text.h"
using namespace std;

int main()
{
	string xs;
	xs = input("Input:");
	print(xs);
	system("pause");
	return 0;
}


core_text.h
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
using namespace std;
#ifndef CORE_TEXT_H
#define CORE_TEXT_H
int print(string msg);
int write(string msg);
string input(string msg);
#endif 


core_text.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

int print(string msg) {
	cout << msg << endl;
	return 0;
}

int write(string msg) {
	cout << msg;
	return 0;
}

string input(string msg) {
	string inmsg;
	cout << msg;
	cin >> inmsg;
	return inmsg;
}
Last edited on
Do not place using directives in header files.
Because of how overload resolution works, there is a potential for using namespace declarations to at worst silently change the behavior of the program. By including that header file, you silently introduce a large number of common names which may silently collide with those in your code.

This has been a topic of active discussion on this forum before: see, e.g.,
http://www.cplusplus.com/forum/beginner/212153/#msg992333

Personally, I don't introduce namespace using declarations at global scope, especially not for namespace std. Where necessary, I introduce namespace aliases. In my opinion, this reduces ambiguity and results in clearer and less error-prone code.

Use const where applicable. Consider passing potentially expensive-to-copy objects (like std::string) by reference (const reference here).
1
2
3
int write(string const& msg) { ... }
int print(string const& msg) { ... } 
std::string input(std::string const& msg) { ... }


Otherwise, it looks good. Be aware that std::system("pause") requires the user to be sitting at the computer to un-pause it, which defeats the point of most command line programs, which should be designed to be used non-interactively.
Hello Cyndanera,

Not much I could add to what mbozzi said,but for the "system("pause");" you can check this out:
http://www.cplusplus.com/forum/beginner/1988/

Something I do is:
1
2
3
4
5
#define DEBUG

#ifdef DEBUG
    code for pause
#endif 


This will use this code until you comment out the #define. This way you can have the pause when working with the program and by putting a comment on the "#define DEBUG" this code will not be included in the final compile, so the program will not pause at the end, but exit the window.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.