Tips Required

Hi,

I have managed to get this program to work but was wondering if there is anyway I could improve the code. I am quite new to programming so nothing too advanced please. Any help would be appreciated.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Author:       
//Date: 	25/09/2013
//Title:        Student Names
//Description:  Gets student names and return number of students with their names

#include <iostream>//header to allow program to use input and output
#include <string>//header to allow program to use strings
#include <cstdlib>//header to allow program to use clear screen command

using namespace std;

void get_names (string &Input_Name);//function prototype for getting names of students


int main ()
{
	int count;//declaring variable of type int
	int Number_of_Students;//declaring variable of type int
	string Input_Name;//declaring variable of type string
	string Student_Name [10];//declaring array of type string which can hold 10 elements

	for (count = 0; count < 10; count++)//loop to be executed max of 10 times as that is the maximum number of students names that can be entered
	{
		get_names (Input_Name);//calling function
		if (Input_Name == "")//tests if name was entered, true terminates for loop, false allows for loop to execute
		{
    		break;//terminates for loop if test condition is true
		}
		Student_Name [count] = Input_Name;//count determines offset of array which stores the student's name inputted by user
 	}
 	system("CLS");//clears screen
 	Number_of_Students = count;//initialising variable
 	cout << "There are " << Number_of_Students << " students, whose names are: \n\n";//prints number of students
 	
 	for (count ; count > 0; count--)//loop counts down until test condition no longer true. Number of executions equal to value of count
 	{
		cout << Student_Name [count-1] << endl;//prints student's name to screen. count-1 used to output the correct element of the array
 	}
 	//next 2 lines of code are used to keep message on screen as my compiler closes DOS window once program finished running. 
	int x;
	cin >> x;
	return 0;
}

//function gets user to input students' names and assigns them to the variable Input_Name
void get_names (string &Input_Name)
{
    cout << "\nEnter student's name: ";
	getline (cin, Input_Name);
}




	

Last edited on
closed account (o3hC5Di1)
Hi there,

It looks pretty good to me, I assume it's for a school assignment so it seems quite good.

Perhaps putting comments on the line above the line of code they comment about would make it a little bit more readable.

One small thing, if i may nitpick:

//next 2 lines of code are used to keep message on screen as my compiler closes DOS window once program finished running.

It's not your compiler which closes the window. A compiler basically translates your code into assembly, a language which the processor knows. It's actually the commandprompt which closes itself after the program has run (i.e. when main() returns). But anyway, that's just a detail - I thought I'd take the opportunity to explain that :)

All the best,
NwN
Hi NwN,

Thanks for taking the time to take a look. The code I have written was part of an exercise given to me by the lecturer as I asked for extra work in order to keep practicing and applying the little knowledge I have gained. I also appreciate your reply regarding my comments.

Ideally I would have liked to use more functions as I want to get in to the habit of keeping code out of the main program unless essential. Unfortunately, I was not able to work out a way of using arrays as parameters and the reading I did indicated that I had to use pointers which is something I have not covered yet. Therefore, I think I will keep reading and come back to it later once I have more knowledge.
Last edited on
closed account (o3hC5Di1)
Hi there,

Creating more functions would be a good idea. For small programs like this it's no much of an issue because the program only serves the one exercise, but it is a good habit indeed.

On this page, at the bottom, you can find some information about passing arrays as parameters to functions:
http://www.cplusplus.com/doc/tutorial/arrays/

All the best,
NwN
Cheers mate, I will be sure to have a read at that later.
around here they will tell you not to use system(). instead of system ("cls") you can use

for (int i=0; i<50; ++i) {std::cout << std::endl}
Topic archived. No new replies allowed.