My first C++ script :D
Warbz 06 Mar 2008
Basically its meant to be used at a firestation recruitment center, the user inputs their height, and the script tells them whether or not they are eligible to join.
Before anyone says anything about it not being real, its not meant to be, its just I had to code something that give a TRUE or FALSE type of output, and base it on a scenario.
Any recomendations would be appreciated.
Before anyone says anything about it not being real, its not meant to be, its just I had to code something that give a TRUE or FALSE type of output, and base it on a scenario.
Any recomendations would be appreciated.
Quote
#include <iostream.h>
#include <conio.h>
void main ()
{
int height;
clrscr();
cout<<"Please input height in centimeters ";
cin>>height;
if (height>=165 && height<=175)
cout<<"Congratulations, you are eligable";
else
cout<<"Sorry, you are not eligable";
getch();
}
#include <conio.h>
void main ()
{
int height;
clrscr();
cout<<"Please input height in centimeters ";
cin>>height;
if (height>=165 && height<=175)
cout<<"Congratulations, you are eligable";
else
cout<<"Sorry, you are not eligable";
getch();
}
Reaper94 06 Mar 2008
you can not understand that?
how can that make any sense anbd where would you put that code?
how can that make any sense anbd where would you put that code?
Jok3r 06 Mar 2008
You would compile it. Then you would run it as a .exe file. It would say on the screen "please enter your height in centimeters"
~SLG
~SLG
Warbz 06 Mar 2008
The code can be compiled and run on its own.
If you have a look at the structure, its quite simple.
meaning include prewritten functions to be used within the code.
define variable height.
Clear screen then display "please input height in centimeters", then wait for user input for the variable hieght.
If unput was greater than or equal to 165 AND less than or equal to 175, then display "congratulations, you are eligable" otherwise, display "sorry you are not eligable".
Another example,
see if you can understand this one.
If you have a look at the structure, its quite simple.
Quote
#include <iostream.h>
#include <conio.h>
#include <conio.h>
meaning include prewritten functions to be used within the code.
Quote
int height;
define variable height.
Quote
clrscr();
cout<<"Please input height in centimeters ";
cin>>height;
cout<<"Please input height in centimeters ";
cin>>height;
Clear screen then display "please input height in centimeters", then wait for user input for the variable hieght.
Quote
if (height>=165 && height<=175)
cout<<"Congratulations, you are eligable";
else
cout<<"Sorry, you are not eligable";
cout<<"Congratulations, you are eligable";
else
cout<<"Sorry, you are not eligable";
If unput was greater than or equal to 165 AND less than or equal to 175, then display "congratulations, you are eligable" otherwise, display "sorry you are not eligable".
Another example,
see if you can understand this one.
Quote
#include <iostream.h>
#include <conio.h>
void main ()
{
int score;
clrscr();
cout<<"Please input score achieved in test ";
cin>>score;
if (score>=40)
cout<<"The student has passed the test. Congratulations";
else
cout<<"The student has failed the test because he/she is a retard. Have a nice day!";
getch();
}
#include <conio.h>
void main ()
{
int score;
clrscr();
cout<<"Please input score achieved in test ";
cin>>score;
if (score>=40)
cout<<"The student has passed the test. Congratulations";
else
cout<<"The student has failed the test because he/she is a retard. Have a nice day!";
getch();
}
Dauth 06 Mar 2008
Nice little bits of code there, at some point I'll show you the code I used to run my MPhys project.
It's too simple really to improve upon, it will work and most likely won't memory leak.
It's too simple really to improve upon, it will work and most likely won't memory leak.
Dauth 06 Mar 2008
Well I did spend 12 weeks working on it, tbh it's quite easy to understand, you point it at data and it does the rest.
Theres a Script file containing all my graph plotting and the loaction of the data.
This was designed to run in ROOT, and when I did a full run it took 4 days to complete, using the most powerful computers in my department (we can access higher power ones but they are off site).
#define Class1_cxx #include "Class1.h" #include <TH2.h> #include <TStyle.h> #include <TFile.h> #include <TCanvas.h> #include <TBranch.h> #include <TH2F.h> #include <cmath> #include <math.h> TH1F *h[100]; TH2F *h2[100]; void Class1::Loop() { cout<< "in Loop " <<endl; if (fChain == 0) return; Long64_t nentries = fChain->GetEntriesFast(); double pi; pi = 4 * atan(1.0); Int_t nbytes = 0, nb = 0; for (Long64_t jentry=0; jentry<nentries;jentry++) { Long64_t ientry = LoadTree(jentry); if (ientry < 0) break; nb = fChain->GetEntry(jentry); nbytes += nb; if(jentry%50000 == 0) cout << jentry << endl; if(nTRK == 3) continue; if(thrustMagAll > 0.995) continue; if(eTotal > 8) continue; if(nTracks > 2) continue; if(eTotal < 5) continue; float_t dth; float_t theta1, theta2; theta1 = acos(TRKcosthCM[0]); theta2 = acos(TRKcosthCM[1]); dth = theta1 + theta2; dth = abs(dth); if(dth > pi){ dth = 2*pi-dth; } if(dth < 2.5) continue; float_t dphi = TRKphiCM[0] - TRKphiCM[1]; dphi = abs(dphi); if(dphi > pi){ dphi = 2*pi-dphi; } if(dphi < 1.57 || dphi > 3) continue; if(dphi > 3 && dth > 3) continue; if(dphi > 2.9 && dth < 2.7) continue; if(dphi > 2.9) continue; double momentum1[3]; double momentum2[3]; double cosphi1; cosphi1 = cos(TRKphiCM[0]); double sinphi1; sinphi1 = sin(TRKphiCM[0]); double sinth1; sinth1 = acos(TRKcosthCM[0]); sinth1 = sin(sinth1); double cosphi2; cosphi2 = cos(TRKphiCM[1]); double sinphi2; sinphi2 = sin(TRKphiCM[1]); double sinth2; sinth2 = acos(TRKcosthCM[1]); sinth2 = sin(sinth2); momentum1[0] = TRKp3CM[0] * cosphi1 * sinth1;//x comp momentum1[1] = TRKp3CM[0] * sinphi1 * sinth1;// y comp momentum1[2] = TRKp3CM[0] * TRKcosthCM[0];// z comp momentum2[0] = TRKp3CM[1] * cosphi2 * sinth2;//x comp momentum2[1] = TRKp3CM[1] * sinphi2 * sinth2;// y comp momentum2[2] = TRKp3CM[1] * TRKcosthCM[1];// z comp double dmom[3]; for (int l = 0; l < 3; l++){ dmom[l] = momentum1[l] + momentum2[l]; dmom[l] = -1*dmom[l]; } double dmom2; dmom2 = (dmom[0]*dmom[0]) + (dmom[1]*dmom[1]) + (dmom[2]*dmom[2]); double thetamissing; thetamissing = dmom[2] / (pow(dmom2, 0.5)); thetamissing = acos(thetamissing); double c, y, cprime; c = 22; y = thrustMagAll * 30; cprime = y - eTotal; if( cprime < c ) continue; double energy1; double energy2; double mass; bool echeck[2][6]; bool mucheck[2][16]; for(int j=0; j < nTRK; j++){ //loop for eSM int eSM = eSelectorsMap[j]; for(int i=0; i < 6; i++){ //Loop for echeck echeck[j][i] = eSM%2; eSM = eSM/2; } } for(int j=0; j < nTRK; j++){ //loop for eSM int mSM = muSelectorsMap[j]; for(int i=0; i < 16; i++){ //Loop for echeck mucheck[j][i] = mSM%2; mSM = mSM/2; } } //mu mu begin if(mucheck[0][8] == 1 && mucheck[1][8] == 1){ energy1 = (TRKp3CM[0]*TRKp3CM[0]) + pow(0.105658, 2); energy1 = pow(energy1, 0.5); energy2 = (TRKp3CM[1]*TRKp3CM[1]) + pow(0.105658, 2); energy2 = pow(energy2, 0.5); mass = ((10.53 - energy1 - energy2)*(10.53 - energy1 - energy2)) - dmom2; mass = pow(mass,0.5); if(mass < 2) continue; double dmomn = pow(dmom2,0.5); h[1]->Fill(TRKp3CM[0]); h[2]->Fill(TRKp3CM[1]); h[3]->Fill(dmomn); h[4]->Fill(mass); h[5]->Fill(eTotal); h[6]->Fill(thetamissing); h2[2]->Fill(eTotal,thrustMagAll); h[11]->Fill(dphi); h[14]->Fill(dth); h2[5]->Fill(dphi,dth); h2[8]->Fill(dphi, eTotal); } //emu begin if((mucheck[0][8] == 1 && echeck[1][4] == 1) || (echeck[0][4] == 1 && mucheck[1][8] == 1)){ if(echeck[0][4] ==1){ energy1 = (TRKp3CM[0]*TRKp3CM[0]) + pow(0.000511, 2); energy1 = pow(energy1, 0.5); energy2 = (TRKp3CM[1]*TRKp3CM[1]) + pow(0.105658, 2); energy2 = pow(energy2, 0.5); mass = ((10.53 - energy1 - energy2)*(10.53 - energy1 - energy2)) - dmom2; mass = pow(mass,0.5); if(mass < 2) continue; } if(echeck[1][4] ==1){ energy1 = (TRKp3CM[0]*TRKp3CM[0]) + pow(0.105658, 2); energy1 = pow(energy1, 0.5); energy2 = (TRKp3CM[1]*TRKp3CM[1]) + pow(0.000511, 2); energy2 = pow(energy2, 0.5); mass = ((10.53 - energy1 - energy2)*(10.53 - energy1 - energy2)) - dmom2; mass = pow(mass,0.5); if(mass < 2) continue; } h[7]->Fill(mass); h[8]->Fill(eTotal); h[9]->Fill(thetamissing); h2[3]->Fill(eTotal,thrustMagAll); h[12]->Fill(dphi); h[15]->Fill(dth); h2[6]->Fill(dphi,dth); h2[9]->Fill(dphi, eTotal); } } }
Theres a Script file containing all my graph plotting and the loaction of the data.
This was designed to run in ROOT, and when I did a full run it took 4 days to complete, using the most powerful computers in my department (we can access higher power ones but they are off site).
Warbz 06 Mar 2008
Sorry, but most of those commands I have never seen before.
I basically started C++ today, though I actually started 6 months I never actually learnt anything from my tutor, shes useless.
It was only when a mate helped me out today.
EDIT:
Could you help me with this?
I need to make it so that it says "you are eligable for an interview" if the user has 5 years work experience AND/OR a degree.
Edited by Seren, 06 March 2008 - 22:45.
I basically started C++ today, though I actually started 6 months I never actually learnt anything from my tutor, shes useless.
It was only when a mate helped me out today.
EDIT:
Could you help me with this?
Quote
#include <iostream.h>
#include <conio.h>
void main ()
{
int exp = 0;
char ans = 89;
clrscr();
while (ans == 89 || ans == 121)
{
cout<<"How many years work experience do you have? ";
cin>>exp;
if (exp<60)
{
if (exp>=5)
{
cout<<"Congratulations, you are eligable for a job
interview!" << endl;
}
else
{
cout<<"Sorry, you are not eligable for an interview."
<< endl;
}
}
else
{
cout<<"Invalid entry, please input a realistic value." <<
endl;
}
getch();
cout << "do you want to do another age check? (y / n): ";
cin >> ans;
cin.get();
}
}
#include <conio.h>
void main ()
{
int exp = 0;
char ans = 89;
clrscr();
while (ans == 89 || ans == 121)
{
cout<<"How many years work experience do you have? ";
cin>>exp;
if (exp<60)
{
if (exp>=5)
{
cout<<"Congratulations, you are eligable for a job
interview!" << endl;
}
else
{
cout<<"Sorry, you are not eligable for an interview."
<< endl;
}
}
else
{
cout<<"Invalid entry, please input a realistic value." <<
endl;
}
getch();
cout << "do you want to do another age check? (y / n): ";
cin >> ans;
cin.get();
}
}
I need to make it so that it says "you are eligable for an interview" if the user has 5 years work experience AND/OR a degree.
Edited by Seren, 06 March 2008 - 22:45.
Dauth 07 Mar 2008
OK for that you'll need commands of the order this (bear in mind these are from the top of my head so you may need to correct syntax) I also have most experience using number based systems so thats how I code, there will be shortcuts you can make if someone else looks over it.
The rest and the implementation is up to you.
cout << "Please enter in years your work experience" << endl; cin>> exp; //Gets number of years of experience cout << "Do you have a Degree qualification, answer Y for yes or N for no" << endl; cin >> deg; //Do you have a degree (you can require only Y or N but thats a piece of code I believe you can create yourself If{deg == y && exp < 60, exp = 10} // If they have a degree and less that 60 yrs experience this grants a pass mark for the inerview, if they don't this line is missed If{exp > 5 && exp < 60 cout << "You are eligible for an interview" << endl; } // This requires that you have between 5 and 60 years of experience, anyone with a degree and under 60 yrs experience automatically passes
The rest and the implementation is up to you.
CodeCat 07 Mar 2008
I'd like to add for whoever is reading this that <conio.h> is a nonstandard extension that some compilers provide. So if it doesn't work, then you know why.
Dauth 07 Mar 2008
I have to ask what does it do? (Since it's and extension/library I've never used)
Alias 07 Mar 2008
I could write something simpler in batch. :S
Not bad for a start, but it's really nothing special.
Edited by Alias, 07 March 2008 - 08:47.
Not bad for a start, but it's really nothing special.
Edited by Alias, 07 March 2008 - 08:47.
Warbz 07 Mar 2008
I know, I was just happy I got this far in one afternoon.
Thanks Dauth.
My latest:
Thanks Dauth.
My latest:
Quote
#include <iostream.h>
#include <conio.h>
void main()
{
int num=0;
char ans=89;
clrscr();
while (ans == 89 || ans == 121)
{
cout<<"Please enter the day of the week: \n";
cin>>num;
cin.get();
cout<<"Your schedule for today is: \n";
switch (num)
{
case 1: cout<<"IT Troubleshooting, Computer Animation, Personal Development.";
break;
case 2: cout<<"Advanced Spreadsheet, LearnDirect, Web Design, Tutorial.";
break;
case 3: cout<<"Text Processing, Business Skills, Information Systems, Succeed.";
break;
case 4: cout<<"Computer Systems, Digital Graphics, Software Design and Development.";
break;
case 5: cout<<"Study Day!";
break;
case 6: cout<<"Weekend!";
break;
case 7: cout<<"Weekend!";
break;
default: cout<<"Please enter a numerical value, 1 for Monday, 2 for Tuesday etc. \n";
}
cin.get();
getch;
cout << "do you want to do another age check? (y / n): ";
cin >> ans;
cin.get();
if (ans == 80 || ans == 121)
{
ans=89;
}
else
cout<<"Goodbye!";
cin.get();
}
}
#include <conio.h>
void main()
{
int num=0;
char ans=89;
clrscr();
while (ans == 89 || ans == 121)
{
cout<<"Please enter the day of the week: \n";
cin>>num;
cin.get();
cout<<"Your schedule for today is: \n";
switch (num)
{
case 1: cout<<"IT Troubleshooting, Computer Animation, Personal Development.";
break;
case 2: cout<<"Advanced Spreadsheet, LearnDirect, Web Design, Tutorial.";
break;
case 3: cout<<"Text Processing, Business Skills, Information Systems, Succeed.";
break;
case 4: cout<<"Computer Systems, Digital Graphics, Software Design and Development.";
break;
case 5: cout<<"Study Day!";
break;
case 6: cout<<"Weekend!";
break;
case 7: cout<<"Weekend!";
break;
default: cout<<"Please enter a numerical value, 1 for Monday, 2 for Tuesday etc. \n";
}
cin.get();
getch;
cout << "do you want to do another age check? (y / n): ";
cin >> ans;
cin.get();
if (ans == 80 || ans == 121)
{
ans=89;
}
else
cout<<"Goodbye!";
cin.get();
}
}
CodeCat 07 Mar 2008
Seren, you realise we have [code] for that kind of stuff?
It's basically a set of functions that let you do some more advanced console i/o, which the standard library doesn't allow for. iostream/stdio are based on stream i/o, but conio also allows direct character access and views the screen as a buffer of given size. So you can insert characters at any position on the screen and change colours and stuff.
Dauth, on 7 Mar 2008, 9:29, said:
I have to ask what does it do? (Since it's and extension/library I've never used)
It's basically a set of functions that let you do some more advanced console i/o, which the standard library doesn't allow for. iostream/stdio are based on stream i/o, but conio also allows direct character access and views the screen as a buffer of given size. So you can insert characters at any position on the screen and change colours and stuff.
Warbz 07 Mar 2008
lol, yeah i forgot codecat. woopsy.
Dauth I made this...
but it says "possible wrong assignment" just before "exp" on this line
Dauth I made this...
#include <iostream.h> #include <conio.h> void main () { int exp = 0; char deg = 89; char ans = 89; clrscr(); while (ans == 89 || ans == 121) { cout << "Please enter in years your work experience" << endl; cin>> exp; cin.get(); cout << "Do you have a Degree qualification, answer Y for yes or N for no" << endl; cin >> deg; cin.get(); { while(deg == 89 && exp < 60, exp = 10); } while(exp > 5 && exp < 60); cout << "You are eligible for an interview" << endl; } getch(); cout << "do you want to do another age check? (y / n): "; cin >> ans; cin.get(); if (ans == 89 || ans == 121) { ans = 89; } else { cout << "Goodbye!"; cin.get(); ans = 110; } }
but it says "possible wrong assignment" just before "exp" on this line
Quote
(deg == 89 && exp < 60, exp = 10);
Warbz 07 Mar 2008
If degree yes (89) or experience is more than 5 and less than 60, then they can have an interview, otherwise, it continues to the next line.
hmmm, I think I may have buggered this piece up quite bad.
*Starts again*
hmmm, I think I may have buggered this piece up quite bad.
*Starts again*
CodeCat 07 Mar 2008
I think you're getting if and while mixed up. If is a one-off check: it's either true or false, and does a different thing depending on the result. While is a loop, it'll continue to repeat the same instructions over and over until a condition is false.
UnderFlow 07 Mar 2008
I don't know C++; do I understand it correctly that you set the experience value to 10? That would be an awkward solution.
And you should start to format your code or you will get problems with debugging larger applications.
And you should start to format your code or you will get problems with debugging larger applications.
Dauth 07 Mar 2008
I just gave him the bones of the code so he could play around with it and make a working system, debugging is his job, as for the 10years experience, since the program is only for a true false system it's not hugely problematic.
Warbz 07 Mar 2008
Well I changed IF to while because the compiler was returning "Undefined IF Statement"
Meh, I cant think straight at the moment. I'll go back over it later.
Meh, I cant think straight at the moment. I'll go back over it later.