This is my second assignment; which is making a simple code.
with that out of the way, here's my code!!!
because i've been learning classes and how to use them in C++, i figured why not kill 2 birds with one stone by practicing both my assignment and classes in one code:
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
class presunivClass {
public:
presunivClass();
~presunivClass();
void setSeats (int newSeats){
if (newSeats > 0){
numSeats = newSeats;
}else{
cout << "do you expect them to just stand up during the whole lecture session?" << endl;
}
}
void setName (string nameDude){
if (nameDude != ""){
className =nameDude;
}else{
cout << "enter a name, how else could the students recognize the classroom?" << endl;
}
}
void setLecturer (string lectureGuy){
if (lectureGuy != ""){
lecturerName = lectureGuy;
}else{
cout << "you have to address who will be teaching the class";
}
}
void setTime (int newTime){
timePack = newTime;
}
int getSeats (){return numSeats;}
string getTime (){
if (timePack == 1){
return "7:30";
}else{
return "10:30";
}
}
string getName (){return className;}
string getLecturer(){return lecturerName;}
private:
int numWindows, numSeats, numDoors, timePack;
string className, lecturerName;
};
presunivClass::presunivClass(){
}
presunivClass::~presunivClass(){
cout << "class dismissed! Hurrahh!!!" << endl;
}
int main (){
//kode tugas mulai dari sini:
bool informationFilled = false;
string nameHolder, lecturerHolder;
int seatsHolder;
presunivClass programmingConcepts;
while (!informationFilled){
cout << "you've created a class, please fill out these required information!" << endl;
cout << "what class will this be?" << endl;
cin >> nameHolder;
cout << endl;
cout << "Great, now who will be the lecturer?" << endl;
cin >> lecturerHolder;
cout << endl;
cout << "okay, now how many students will be attending this class" << endl;
cin >> seatsHolder;
cout << endl;
cout << "thank you for filling this out, now checking if it's complete, please wait..." << endl;
programmingConcepts.setSeats(seatsHolder);
programmingConcepts.setName(nameHolder);
programmingConcepts.setLecturer(lecturerHolder);
if (nameHolder != "" && lecturerHolder != "" && seatsHolder > 0){
cout << "Great job, the form has been succesfully completed!"<<endl;
bool timeChoose = true;
while (timeChoose){
cout << "please choose which time would you like the class to begin" <<endl;
cout << "1. 7:30 2. 10:30" <<endl;
int time = 0;
scanf ("%d", &time);
switch (time){
case 1:
cout << "you have chosen 7:30, don't be late!" <<endl;
programmingConcepts.setTime(time);
timeChoose = false;
break;
case 2:
cout << "you have chosen 10:30, don't be late!" <<endl;
programmingConcepts.setTime(time);
timeChoose = false;
break;
default:
cout << "that is not an option, try again:" << endl;
timeChoose = true;
break;
}
}
informationFilled = true;
}
else{
cout << "the information is false, please try again;" << endl;
}
}
cout << endl << "great, begin class with this information; class name: "<< programmingConcepts.getName() <<endl << "class lecturer: " << programmingConcepts.getLecturer()<<endl << "number of students: "<< programmingConcepts.getSeats()<< endl << "and it begins: "<< programmingConcepts.getTime() <<endl;
return 0;
}
incase i'm not allowed to use C++
This one is the one without the OOP aspects:
#include <stdio.h>
int dude(int x, int y){
if (x < y){
return 1;
}else{
return 0;
}
}
int main (){
int a = 0, b = 0;
bool filled = false;
while (!filled){
printf("fill in two variables, no zero or negative numbers\n");
printf("enter a value for a: \n");
scanf("%d", &a);
printf("enter a value for b: \n");
scanf("%d", &b);
if (a > 0 && b > 0){
printf("thank you for filling this out\n");
filled = true;
}else{
printf("you cannot enter that number\n");
}
}
switch (dude (a, b)){
case 1:
printf ("a is less than b\n");
break;
case 0:
printf ("a is more than b\n");
break;
}
return 0;
}
Explanation:
starting from "int main (){"
first i declare variables using this format:
datatype variableName;
in the code i declared informationFilled as a boolean by typing:
bool informationFilled = false;
and i immediately assigned a value by adding an equation mark and the value of "false" for booleans.
next, i started a while loop which is structured like this:
while (condition){
//blah blah blah
}
the condition i used is (!informationFilled), which basically means that anything within this while block will loop as long as informationFilled is false (! stands for "not").
within that while loop are a bunch of cin's and cout's, which has the same function as scanf and printf so i don't think i need to explain that.
ignore the stuff that starts with "programmingConcept", those are class functions that i like to call "setters".
next, still in the while loop is an If statement, which is structured like this :
if (condition){
//do stuff
}
you can also add an else or else if at the bottom to add more complexity:
if (condition){
}else if (other condition){
}else{
}
i forgot to mention, "conditions" are basically boolean values (true or false), that can be replaced with comparative operators comparing two or more variables (in the case of more comparision you could use logical operators such as and (&&) and or (||))
within the if statement, i added another while loop, just to show you can do that.
and within THAT while loop i added a switch statement which is formatted like this:
switch (variable){
case condition1:
//do something
break;
case condition2:
//do something else.
break;
default:
//do something if no other case conditions are met
breaK:
}
you can have as many cases as you want. basically it's just another version of the if statements (and it's friends)
in case you didn't notice, i added an assignment statement to the conditions in both whiles in some if statements and some switch cases, these are what i like to call "loop breakers"i coined the term myself, don't judge me!. the purpose of these are to prevent any unlimited loops and to make it possible to exit a loop.
now let's get down to functions; the functions i used in my code are functions inside classes, including special ones like constructor and destructor functions; but simpler functions in non OOP language are like this
functions are stated like so:
returnType functionName (dataType parameterVariable){
//do stuff
return someVariableMatchingTheReturnType
}
of course you can ignore the return if the return type is void.
this is how you call a function;
functionName (argumentVariable);
functions with returnTypes can also be used in places where you can use a normal variable for example; variable assignment;
int x = someFunctionWithAReturnTypeOfInteger(argumentVariable);
Result of first code (i don't know why but i can't put any spaces between the string inputs):
Result of second code:
That is it for this assignments.