Would an if statement work here?

I am trying to figure out how to make a user input a certain number of students and certain number of classes. Say 3 students and 5 classes and if they dont, it asks them again. I am having trouble on figuring out just how to write and implement that into the code. I am still new to this so I know it isn't pretty. I started to work on it around line 71, but then drew a blank.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "Student.h"
#include "Class.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int menu();  //Function for user interface
string addStudent();
string addCourse();
string addSchedule();
string addDisplay();
void sysPause();
void exit();

int main() {
	int userInput;
	
	do
	{
		userInput = menu();
		switch(userInput)
			{ 
				case 1: addStudent();
			break; 
				case 2: addCourse();
			break; /*
				case 3: addSchedule();
			break; /*
				case 4: addDisplay();
			break; */
				case 5: exit();
			break; 
				default: cout << "Please enter a valid choice \n";
			}
		}	while (userInput != 5);

		cout << "Johnny 5 need more knowledge" << endl;
	system("PAUSE");
	return 0;
}

int menu() {
	int choice;
		cout << "\n\n\t====================================================\n"
			 << "\t\t\t\t  Main Menu\n"
			 << "\t====================================================\n"
			 << "\t\t 1) Enter a student\n"
			 << "\t\t 2) Enter a course\n"
			 << "\t\t 3) Add a course to a student\n"
			 << "\t\t 4) Show student schedule\n"
			 << "\t\t 5) Save and exit\n"
			 << "\t====================================================\n"
			 << " \n\t\t Please enter a selection: ";
			 
		cin >> choice;
		return choice;
}

string addStudent() 
{
	string SID;
	string Firstname;
	string Lastname;
	int numStudents;
	
	cout << "Please enter number of students:";
	cin >> numStudents;
		
	if(numStudents < 4)
	{
		cout << "Please enter at least 4 students: \n";
	}
		
		else if(numStudents >= 4){
		cout << "Please enter the required information: \n";
		}
		
	cout << "Please enter the student's ID: ";
	cin >> SID; 
	
	cout << "Please enter the student's first name: ";
	cin >> Firstname;
	
	cout << "Please enter the student's last name: ";
	cin >> Lastname; 
	
	cout << "The following information has been entered: \n";
	cout << "Student ID: " << SID << endl;
	cout << "First name: " << Firstname << endl;
	cout << "Last name: " << Lastname << endl;
	
	ofstream myfile;
  myfile.open ("StudentFile.txt", ios::app);
  myfile << "Student id: " << SID;
  myfile << "\nFirst name: " << Firstname;
  myfile << "\nLast name: " << Lastname << endl;
  myfile.close();
	
	return std::string();
	system("PAUSE");
	
}

string addCourse() {
	
	string CID;
	string Course;
	int Credits;
	int numCourses;
	
	cout << "Please enter the number of courses: ";
	cin >> numCourses;
	
	if(numCourses < 10)
	{
		cout << "Please enter at least 10 courses\n";
	}
	
	else if(numCourses >= 10)
	{
		cout << "Please enter the required information: \n";
	}
	
	cout << "Please enter the course ID: ";
	cin >> CID;
	
	cout << "Please enter the course name: ";
	cin >> Course;
	
	cout << "Please enter the amount of credits the course is worth: ";
	cin >> Credits;
	
	cout << "You entered the following information: \n";
	cout << "Course ID: " << CID << endl;
	cout << "Course name: " << Course << endl;
	cout << "Credits applied: " << Credits << endl;
	
	ofstream myfile;
  myfile.open ("StudentFile.txt", ios::app);
  myfile << "\nCourse id: " << CID;
  myfile << "\nCourse: " << Course;
  myfile << "\nCredits: " << Credits << endl;
  myfile.close();
	
	return std::string();
	system("PAUSE");

}

void exit(){

  ifstream myfile;
  myfile.open ("StudentFile.txt");
  
  myfile.close();
}
Last edited on
Hello ace184,

What I see until I have a chance to load the program and compile it.

Line 14. I do not see the function for "sysPause()". Later I will have a suggestion for you.

Line 15. Be careful because there is a function called "exit(1);" that takes one parameter. Your function with no parameters may just be considered an overloaded version, but in the end the function has no real use. Is there a point to opening and closing a file stream?

For now "main" looks OK.

The function "menu" could be a potential problem as there is no validation for the input. Counting on the default case in the switch is not the best way to deal with bad input. For a menu function I generally put most of the contents in a do while loop and only return a valid choice. You will also have to deal with an entry of a letter or something other than a number that will cause "cin" to fail which will leave "cin" unusable until you reset it.

Your two other functions look OK for now except lines 102 and 147. What is return std::string()? A function can only return one thing, so what is it you are trying to return? And what do you want to return?

Your last function void exit() is a potential problem because there is a built in function "exit()", see http://www.cplusplus.com/reference/cstdlib/exit/?kw=exit , which takes one parameter usually a number. Then you open a file and close it. is there a point to this?

I will let you know if I find anything else once I have a chance to compile the program.

Hope that helps for now,

Andy
if(numStudents < 4)
{
cout << "Please enter at least 4 students: \n";
}


typically this is done with a loop along these lines:

do
{
cout << enter students blah blah;
cin >> numstudents;
if(numStudents < 4)
{
cout << "Please enter at least 4 students: \n";
}
}while(numStudents < 4);

Hello ace184,

I made some changes to your program, but not sure if you would want to use them.

jonnin's example using the do while loop is the same as what I did, but after this your function does not make use of the variable "numStudents". It just inputs information for one student and returns to the main menu.

An Idea I had is to create two structs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef STUDENTINFO_HPP
#define STUDENTINFO_HPP

struct Courses
{
	std::string s_CID;
	std::string s_Course;
	int s_Credits;
};

struct StudentInfo
{
	std::string s_sid;
	std::string s_firstName;
	std::string s_lastname;

	std::vector<Courses> s_vCourses;

	// <--- This can be avoided by entering information directly into the struct variables.
	void SetInfo(std::string sid, std::string firstName, std::string lastname);
};

#endif // !STUDENTINFO_HPP 


In "main" you could create a vector of type "StudentInfo", something like std::vector<StudentInfo> vStudentInfo;, to hold the information about each student. And from the "addStudent" function you could call the "addCourse" function passing the temporary "StudentInfo" struct to fill the vector about the courses. When you return from the "addCourses" function put the completed struct into the vector.

I will take this opportunity to say that the function "exit()" is a bad choice for a name as there is a built in function called "exit(1)" that takes one parameter of type int. Any number greater than zero denotes an error or a problem. Your function having no parameter makes it an overloaded function and that is why it works. Since the menu choice is "Save and Exit" call the function "saveAndExit()" and avoid any confusion or problems.

That said by passing the vector "vStudentInfo", defined in "main", to the "saveAndExit" function you will have something to work with and your output would keep the information about each student and the courses together making it easier to understand. This way you could write to "StudentFile.txt" the way you have and create a second file with different output that would make it easier for the program to read in the future, (thinking of something like a CSV file).

Another problem I see if that each function ends with writing the information just input to a file. Done properly this would create a section of 4 or more student followed by the courses which would be a separate section of courses information with the only thing tying them together being the student ID. Not the greatest idea for creating a file, but it may work for you.

I do not know what information you were given to write this program, but it would be interesting to know.

Most of your code is useful and usable just not quite in the right order or done at the right time.

There is something to think about.

Hope that helps,

Andy
> I am having trouble on figuring out just how to write and implement that into the code.

It is much easier if we write a function to do this.

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
#include <iostream>
#include <string>

// accept an int at least equal to min_value from stdin. retry on input error
int get_int_ge( const std::string& prompt, int min_value )
{
    std::cout << prompt << " (" << min_value << " or greater): " ;

    int value ;
    if( std::cin >> value ) // if the user entered an int
    {
        if( value >= min_value ) return value ; // valid input, return it

        else std::cout << "value is too small. must be at least " << min_value << '\n' ;
    }

    else // nonsense input (not an integer)
    {
        std::cout << "please enter an integer\n" ;
        std::cin.clear() ; // clear the failed state of cin
        std::cin.ignore( 1000, '\n' ) ; // and throw the bad input line away
    }

    return get_int_ge( prompt, min_value ) ; // bad input, try again
}

int main()
{
    const int MIN_STUDENTS = 4 ;
    const int MIN_COURSES = 10 ;

    const int numStudents = get_int_ge( "please enter number of students", MIN_STUDENTS ) ;
    const int numCourses = get_int_ge( "please enter number of courses", MIN_COURSES ) ;

    std::cout << "#students: " << numStudents << "  #courses: " << numCourses << '\n' ;
}
Topic archived. No new replies allowed.