Jump to content


My first C++ script :D


21 replies to this topic

#1 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 06 March 2008 - 17:43

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.


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();

}


Posted Image

#2 Reaper94

    rawr!!

  • Member
  • 1178 posts
  • Projects: Being more loved and less loathed by community

Posted 06 March 2008 - 18:00

you can not understand that?

how can that make any sense anbd where would you put that code?

 RaiDK, on 3 Jun 2009, 10:09, said:

MY BEAK IS ONE WHICH WILL PIERCE THE HEAVENS.

Posted Image

#3 Jok3r

    veritas vos liberabit

  • Project Team
  • 1909 posts
  • Projects: Hangar 13 Projects

Posted 06 March 2008 - 18:02

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
kinda, sorta alive.



#4 Reaper94

    rawr!!

  • Member
  • 1178 posts
  • Projects: Being more loved and less loathed by community

Posted 06 March 2008 - 18:03

ill try wrapping my head around a brick and see which feels nicest...

 RaiDK, on 3 Jun 2009, 10:09, said:

MY BEAK IS ONE WHICH WILL PIERCE THE HEAVENS.

Posted Image

#5 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 06 March 2008 - 18:13

The code can be compiled and run on its own.

If you have a look at the structure, its quite simple.

Quote

#include <iostream.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;

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";


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();

}


Posted Image

#6 Dauth

    <Custom title available>

  • Gold Member
  • 11193 posts

Posted 06 March 2008 - 18:19

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.

#7 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 06 March 2008 - 18:34

Is your code gonna put mine to shame? lol

Posted Image

#8 Dauth

    <Custom title available>

  • Gold Member
  • 11193 posts

Posted 06 March 2008 - 18:40

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.

#define Class1_cxx
#include &#34;Class1.h&#34;
#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&#91;100&#93;;
TH2F *h2&#91;100&#93;;

void Class1&#58;&#58;Loop&#40;&#41;
{

  cout<< &#34;in Loop &#34; <<endl;

  if &#40;fChain == 0&#41; return;

   Long64_t nentries = fChain->GetEntriesFast&#40;&#41;;

  double pi;
  pi = 4 * atan&#40;1.0&#41;;

   Int_t nbytes = 0, nb = 0;
   for &#40;Long64_t jentry=0; jentry<nentries;jentry++&#41; {
	  Long64_t ientry = LoadTree&#40;jentry&#41;;
	  if &#40;ientry < 0&#41; break;
	  nb = fChain->GetEntry&#40;jentry&#41;;   nbytes += nb;

	  if&#40;jentry%50000 == 0&#41; cout << jentry << endl;

	  if&#40;nTRK == 3&#41; continue;
	  if&#40;thrustMagAll >  0.995&#41; continue;
	  if&#40;eTotal > 8&#41; continue;
	  if&#40;nTracks > 2&#41; continue;
	  if&#40;eTotal < 5&#41; continue;

	  float_t dth;
	  float_t theta1, theta2;


	   theta1 = acos&#40;TRKcosthCM&#91;0&#93;&#41;;
	   theta2 = acos&#40;TRKcosthCM&#91;1&#93;&#41;;


	   dth = theta1 + theta2;
	   dth = abs&#40;dth&#41;;

	  if&#40;dth > pi&#41;{
	dth = 2*pi-dth;
	  }	 

	  if&#40;dth < 2.5&#41; continue; 

	  float_t dphi = TRKphiCM&#91;0&#93; - TRKphiCM&#91;1&#93;;
	  dphi = abs&#40;dphi&#41;;

	  if&#40;dphi > pi&#41;{
	dphi = 2*pi-dphi;
	  }


	  if&#40;dphi < 1.57 || dphi > 3&#41; continue;
	  if&#40;dphi > 3 && dth > 3&#41; continue;
	  if&#40;dphi > 2.9 && dth < 2.7&#41; continue;
	  if&#40;dphi > 2.9&#41; continue;

	  double momentum1&#91;3&#93;;
	  double momentum2&#91;3&#93;;

	  double cosphi1;	 
	  cosphi1 = cos&#40;TRKphiCM&#91;0&#93;&#41;;
	  double sinphi1;	 
	  sinphi1 = sin&#40;TRKphiCM&#91;0&#93;&#41;;
	  double sinth1;
	  sinth1 = acos&#40;TRKcosthCM&#91;0&#93;&#41;;
	  sinth1 = sin&#40;sinth1&#41;;
	  double cosphi2;	 
	  cosphi2 = cos&#40;TRKphiCM&#91;1&#93;&#41;;
	  double sinphi2;	 
	  sinphi2 = sin&#40;TRKphiCM&#91;1&#93;&#41;;
	  double sinth2;
	  sinth2 = acos&#40;TRKcosthCM&#91;1&#93;&#41;;
	  sinth2 = sin&#40;sinth2&#41;;

	  momentum1&#91;0&#93; = TRKp3CM&#91;0&#93; * cosphi1 * sinth1;//x comp
	  momentum1&#91;1&#93; = TRKp3CM&#91;0&#93; * sinphi1 * sinth1;// y comp
	  momentum1&#91;2&#93; = TRKp3CM&#91;0&#93; * TRKcosthCM&#91;0&#93;;// z comp


	  momentum2&#91;0&#93; = TRKp3CM&#91;1&#93; * cosphi2 * sinth2;//x comp
	  momentum2&#91;1&#93; = TRKp3CM&#91;1&#93; * sinphi2 * sinth2;// y comp
	  momentum2&#91;2&#93; = TRKp3CM&#91;1&#93; * TRKcosthCM&#91;1&#93;;// z comp


	  double dmom&#91;3&#93;;
	  for &#40;int l = 0; l < 3; l++&#41;{
	dmom&#91;l&#93; = momentum1&#91;l&#93; + momentum2&#91;l&#93;;

	dmom&#91;l&#93; = -1*dmom&#91;l&#93;;

	  }

	  double dmom2;

	  dmom2 = &#40;dmom&#91;0&#93;*dmom&#91;0&#93;&#41; + &#40;dmom&#91;1&#93;*dmom&#91;1&#93;&#41; + &#40;dmom&#91;2&#93;*dmom&#91;2&#93;&#41;;

	  double thetamissing;

	  thetamissing = dmom&#91;2&#93; / &#40;pow&#40;dmom2, 0.5&#41;&#41;&#59;
	  thetamissing = acos&#40;thetamissing&#41;;

	  double c, y, cprime;
	  c = 22;
	  y = thrustMagAll * 30;
	  cprime = y - eTotal;

	  if&#40; cprime < c &#41; continue;

	  double energy1;
	  double energy2;
	  double mass;


	  bool echeck&#91;2&#93;&#91;6&#93;;
	  bool mucheck&#91;2&#93;&#91;16&#93;;

	  for&#40;int j=0; j < nTRK; j++&#41;{  //loop for eSM
	   int eSM = eSelectorsMap&#91;j&#93;;

	 for&#40;int i=0; i < 6; i++&#41;{ //Loop for echeck
	 echeck&#91;j&#93;&#91;i&#93; = eSM%2;
	 eSM = eSM/2;
   }
  }
	  for&#40;int j=0; j < nTRK; j++&#41;{  //loop for eSM
	   int mSM = muSelectorsMap&#91;j&#93;;

	 for&#40;int i=0; i < 16; i++&#41;{ //Loop for echeck
	  mucheck&#91;j&#93;&#91;i&#93; = mSM%2;
	 mSM = mSM/2;
   }
  }

	  //mu mu begin
  
	  if&#40;mucheck&#91;0&#93;&#91;8&#93; == 1 && mucheck&#91;1&#93;&#91;8&#93; == 1&#41;{

	  energy1 = &#40;TRKp3CM&#91;0&#93;*TRKp3CM&#91;0&#93;&#41; + pow&#40;0.105658, 2&#41;;
	  energy1 = pow&#40;energy1, 0.5&#41;;

	  energy2 = &#40;TRKp3CM&#91;1&#93;*TRKp3CM&#91;1&#93;&#41; + pow&#40;0.105658, 2&#41;;
	  energy2 =  pow&#40;energy2, 0.5&#41;;

	  mass = &#40;&#40;10.53 - energy1 - energy2&#41;*&#40;10.53 - energy1 - energy2&#41;&#41; - dmom2;
	  mass = pow&#40;mass,0.5&#41;;

	  if&#40;mass < 2&#41; continue;

	  double dmomn = pow&#40;dmom2,0.5&#41;;

	  h&#91;1&#93;->Fill&#40;TRKp3CM&#91;0&#93;&#41;;
	  h&#91;2&#93;->Fill&#40;TRKp3CM&#91;1&#93;&#41;;
	  h&#91;3&#93;->Fill&#40;dmomn&#41;;

	  h&#91;4&#93;->Fill&#40;mass&#41;;
	  h&#91;5&#93;->Fill&#40;eTotal&#41;;
	  h&#91;6&#93;->Fill&#40;thetamissing&#41;;
	  h2&#91;2&#93;->Fill&#40;eTotal,thrustMagAll&#41;;
	  h&#91;11&#93;->Fill&#40;dphi&#41;;
	  h&#91;14&#93;->Fill&#40;dth&#41;;
	  h2&#91;5&#93;->Fill&#40;dphi,dth&#41;;
	  h2&#91;8&#93;->Fill&#40;dphi, eTotal&#41;;
	  }

	  //emu begin

		   if&#40;&#40;mucheck&#91;0&#93;&#91;8&#93; == 1 && echeck&#91;1&#93;&#91;4&#93; == 1&#41; || &#40;echeck&#91;0&#93;&#91;4&#93; == 1 && mucheck&#91;1&#93;&#91;8&#93; == 1&#41;&#41;{

	if&#40;echeck&#91;0&#93;&#91;4&#93; ==1&#41;{
	  energy1 = &#40;TRKp3CM&#91;0&#93;*TRKp3CM&#91;0&#93;&#41; + pow&#40;0.000511, 2&#41;;
	  energy1 = pow&#40;energy1, 0.5&#41;;

	  energy2 = &#40;TRKp3CM&#91;1&#93;*TRKp3CM&#91;1&#93;&#41; + pow&#40;0.105658, 2&#41;;
	  energy2 =  pow&#40;energy2, 0.5&#41;;

	  mass = &#40;&#40;10.53 - energy1 - energy2&#41;*&#40;10.53 - energy1 - energy2&#41;&#41; - dmom2;
	  mass = pow&#40;mass,0.5&#41;;

	  if&#40;mass < 2&#41; continue;
	}

	if&#40;echeck&#91;1&#93;&#91;4&#93; ==1&#41;{
	  energy1 = &#40;TRKp3CM&#91;0&#93;*TRKp3CM&#91;0&#93;&#41; + pow&#40;0.105658, 2&#41;;
	  energy1 = pow&#40;energy1, 0.5&#41;;

	  energy2 = &#40;TRKp3CM&#91;1&#93;*TRKp3CM&#91;1&#93;&#41; + pow&#40;0.000511, 2&#41;;
	  energy2 =  pow&#40;energy2, 0.5&#41;;

	  mass = &#40;&#40;10.53 - energy1 - energy2&#41;*&#40;10.53 - energy1 - energy2&#41;&#41; - dmom2;
	  mass = pow&#40;mass,0.5&#41;;

	  if&#40;mass < 2&#41; continue;
	}

	  h&#91;7&#93;->Fill&#40;mass&#41;;
	  h&#91;8&#93;->Fill&#40;eTotal&#41;;
	  h&#91;9&#93;->Fill&#40;thetamissing&#41;;
	  h2&#91;3&#93;->Fill&#40;eTotal,thrustMagAll&#41;;
	  h&#91;12&#93;->Fill&#40;dphi&#41;;
	  h&#91;15&#93;->Fill&#40;dth&#41;;
	  h2&#91;6&#93;->Fill&#40;dphi,dth&#41;;
	  h2&#91;9&#93;->Fill&#40;dphi, eTotal&#41;;

	 }


   }


}


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).

#9 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 06 March 2008 - 22:42

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?

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();
}
}




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.


Posted Image

#10 Dauth

    <Custom title available>

  • Gold Member
  • 11193 posts

Posted 07 March 2008 - 00:29

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.

cout << &#34;Please enter in years your work experience&#34; << endl;
cin>> exp; //Gets number of years of experience

cout << &#34;Do you have a Degree qualification, answer Y for yes or N for no&#34; << endl;
cin >> deg; //Do you have a degree &#40;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&#39;t this line is missed

If{exp > 5 && exp < 60
cout << &#34;You are eligible for an interview&#34; << 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.

#11 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 07 March 2008 - 01:15

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.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#12 Dauth

    <Custom title available>

  • Gold Member
  • 11193 posts

Posted 07 March 2008 - 08:29

I have to ask what does it do? (Since it's and extension/library I've never used)

#13 Alias

    Member Title Goes Here

  • Member
  • 11705 posts

Posted 07 March 2008 - 08:47

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.


Posted Image

#14 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 07 March 2008 - 10:16

I know, I was just happy I got this far in one afternoon.
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();
}
}


Posted Image

#15 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 07 March 2008 - 11:47

Seren, you realise we have [code] for that kind of 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.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#16 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 07 March 2008 - 11:55

lol, yeah i forgot codecat. woopsy.
Dauth I made this...
#include <iostream.h>
#include <conio.h>

void main &#40;&#41;
{
	int exp = 0;
	char deg = 89;
	char ans = 89;

	clrscr&#40;&#41;;

	while &#40;ans == 89 || ans == 121&#41;
	{
cout << &#34;Please enter in years your work experience&#34; << endl;
cin>> exp;
cin.get&#40;&#41;;

cout << &#34;Do you have a Degree qualification, answer Y for yes or N for no&#34; << endl;
cin >> deg;
cin.get&#40;&#41;;
{
while&#40;deg == 89 && exp < 60, exp = 10&#41;;
}
while&#40;exp > 5 && exp < 60&#41;;
cout << &#34;You are eligible for an interview&#34; << endl;
}

getch&#40;&#41;;


	 cout << &#34;do you want to do another age check? &#40;y / n&#41;&#58; &#34;;
	 cin >> ans;
	 cin.get&#40;&#41;;

	 if &#40;ans == 89 || ans == 121&#41;
	 {
	   ans = 89;
	 }
	 else
	 {
	cout << &#34;Goodbye!&#34;;
	cin.get&#40;&#41;;
	ans = 110;
	 }
}


but it says "possible wrong assignment" just before "exp" on this line

Quote

(deg == 89 && exp < 60, exp = 10);


Posted Image

#17 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 07 March 2008 - 11:57

What is that line supposed to do? Right now, it'll loop forever...
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#18 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 07 March 2008 - 12:06

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*

Posted Image

#19 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 07 March 2008 - 12:53

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.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#20 UnderFlow

    Amateur

  • Member
  • 121 posts

Posted 07 March 2008 - 13:45

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.

#21 Dauth

    <Custom title available>

  • Gold Member
  • 11193 posts

Posted 07 March 2008 - 14:20

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.

#22 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 07 March 2008 - 14:36

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.

Posted Image



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users