How efficient is this code?

Hi guys, im a first time poster and a complete beginner with c++,
I've written the following piece of code simply to familiarise myself with variables & function, and im hoping someone could take a look at it and let me know if im constructing my code in the correct way. The program works fine, but obviously I dont want to pick up any bad habits this early on!
Thanks in advance for your advice :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Aliens.cpp : A basic program to practise with variables and functions

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

string charactername()											// Choose Marine's Name
{	
	string chName;
	cout << "Please enter your marine's name..." << endl;
	cin >> chName;
	return chName;
}

void outputname(string chName)									// Output name to Screen
{
	cout << chName << endl;
}

void outputinfo(int s,int p,string chName)						// Output Information to screen
{
	outputname(chName);											// Call to output name to screen
	cout << "Speed: " << s << endl;
	cout << "Power: " << p << endl;
}


int main()														// Main Program
{	string chName = charactername();
	int nSpeed = 2, nPower = 4;									// Generate Marine Speed & Power
	string chAlienname = "Alien Warrior";						// Generate Alien name & stats
	int nAlienspeed = 4, nAlienpower = 5;
	cout << "Cpl ";
	outputinfo(nSpeed, nPower, chName);							// Call to output information to screen

	cout << endl << "An Alien approaches...\n" << endl;			// Introduces alien, and calls outputinfo to display stats
	outputinfo(nAlienspeed,nAlienpower, chAlienname);
system ("PAUSE");	
return 0;
}
There's no need to worry about efficiency unless it is critical in the application you're creating, this probably won't be true of programs you make as a beginner. The only thing I would add about the code you've posted is that you should pass strings into functions by const reference e.g.
void outputname(const string& chName)

passing basic types like int and char by value is fine but when you're passing an object like a string of an object of a class you should generally pass a const reference.
Thanks for the pointer, im taking any advice i can get at the moment!
I was always taught to put the main() functions first. To do that you need to make your function prototype definitions below using namespace std. Just write the type, name of the functions and arguments like normal but add a semicolon at the end.
1
2
3
4
5
6
7
8
9
10
using namespace std;
string charactername();
void outputname( string chName );
void outputname( string chName );
void outputinfor( int s, int p, string chName );

int main()
{
    // do stuff...
}


Also, and this is just my preference, it is easier to use camelback text or to use underscores to make reading variable/function names easier. As your programs get bigger and bigger, the easier they are to read, the better.
Last edited on
thanks for that, is there a practical reason for putting the main() function first? Will it affect the overall program in any way?
No, it won't. But it helps if you can find the main function easily.
Thanks to modern IDEs, it's a non-issue anyway, since you can easily jump to any function in the current source file.
closed account (zb0S216C)
Mr Zen wrote:
Will it affect the overall program in any way? (sic)

No. You're basically informing the compiler/linker that the function exists, but you haven't yet defined its body. These are called Function Prototypes. This is valid and is commonly used when working with header modules.

As for your code, I would've explicitly told the compiler the parts of the std namespace I was using. For example:

1
2
3
4
5
6
7
#include <iostream>

int main( )
{
    std::cout << "..." << std::endl;
    return 0;
}

Another suggestion would be to tell the compiler/linker where the function you're calling is located. For example:

1
2
3
4
5
6
7
8
9
void Function( void )
{
    // ...
}

int main( )
{
    ::Function( ); // This function is declared within the global namespace.
}

This way, the compiler/linker knows exactly where to find the function without searching for it.

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