Thread: My Physics Programme

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    My Physics Programme

    Somebody had created a physics programme somewhere so I thought i would have a go and add in some other stuff. It is very long. If someone could show me if there is any way to make it shorter I would be pleased. I have put in comments:
    Code:
    #include <iostream>
    #include <string>                                                                           //included for the use of strings
    #include <cstdlib>                                                                          //included for the use of system ("CLS")
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      float resistance1 = 0;                                                                    //obviously create a float varibale, same down below
      float resistance2 = 0;
      float resistancet = 0;
      float voltage = 0;
      float current = 0;
      string type = "";                                                                         //create a string, a string is just a line so it can take any number of letters, but there is a limit
      string rtype = "";
      string end = "";
    
      do                                                                                        //start a do while loop
      {
       cout<<"Please enter the type of calculation (lower case): ";                             //obviously enters text, note that i didnt use endl so that the input is on the same line as the writing
       getline(cin,type);                                                                       //gets the input from the string named type
       cout<<endl;                                                                              //this is where the line ends
    
       if (type == "voltage") {                                                                 //if the calculation type is voltage then the following code is executed
          cout<<"Please enter the total resistance (Ohms): ";                                   //once again not that endl is not there
          cin>>resistance1;                                                                     //obtains the input as resistance1
          cout<<endl;
          cout<<"Please enter the circuit current (A): ";
          cin>>current;
          voltage = resistance1 * current;                                                      //once the current and resistance are set then it will multiply them together to give you the voltage
          cout<<endl;
          cout<<"The supply voltage = "<<voltage<<"V"<<endl;                                    //shows the answer
          cin.get();                                                                            //waits for the user to press return, I think
          cout<<endl;
          cout<<"Would you like to continue? (yes / no): ";
          getline(cin,end);                                                                     //gets the input whether the user wants to end, when the input is yes the do/while loop repeats when no it ends.
          system("CLS");                                                                        //clears the screen incase the programme is repeating
       }
       else if (type == "current") {                                                            //else if the calculation required is current then do the following
          cout<<"Please enter the voltage supply (V): ";
          cin>>voltage;
          cout<<endl;
          cout<<"Please enter the total resistance (Ohms): ";
          cin>>resistance1;
          current = voltage / resistance1;                                                      //divides voltage by resistance to get the current
          cout<<endl;
          cout<<"The circuit current = "<<current<<"A"<<endl;                                   //gives the answer
          cin.get();
          cout<<endl;
          cout<<"Would you like to continue? (yes / no): ";
          getline(cin,end);
          system("CLS");                                                                        //clear the screen incase the programme is repeating
       }
       else if (type == "resistance") {
          cout<<"How Would you like to work this out? (series, parallel or v=ir): ";            //asks you how you would like to calculate resistance
          getline(cin,rtype);                                                                   //this is where the rtype string is used
          cout<<endl;
             if (rtype == "series") {                                                           //nested if staement...if you want to do it by series then do the following
                cout<<"NB, This Calculator can only take two resistances"<<endl;                //this line of text does not take input
                cout<<endl;
                cout<<"Please enter the value of resistor 1 (Ohms): ";
                cin>>resistance1;
                cout<<endl;
                cout<<"Please enter the value resistor 2 (Ohms): ";
                cin>>resistance2;
                resistancet = resistance1 + resistance2;                                        //adds the two resistances together for series circuits
                cout<<endl;
                cout<<"The total resistance = "<<resistancet<<" Ohms"<<endl;                    //gives the answer
                cin.get();
                cout<<endl;
                cout<<"Would you like to continue? (yes / no): ";
                getline(cin,end);
                system("CLS");                                                                  //clears screen
             }
             else if (rtype == "parallel") {                                                    //nested else if statement, just the same as if, if you want parallel resistances then do the following
                cout<<"NB, This Calculator can only take two resistances"<<endl;
                cout<<endl;
                cout<<"Please enter the value of resistor 1 (Ohms): ";
                cin>>resistance1;
                cout<<endl;
                cout<<"Please enter the value resistor 2 (Ohms): ";
                cin>>resistance2;
                resistancet = (resistance1 * resistance2) / (resistance1 + resistance2);        //more complicated sum, note brackets because divide is more important than + and *
                cout<<endl;
                cout<<"The total resistance = "<<resistancet<<" Ohms"<<endl;                    //gives the answer to the sum
                cin.get();
                cout<<endl;
                cout<<"Would you like to continue? (yes / no): ";                               //asks the user if they would like to continue
                getline(cin,end);                                                               //gets the input to string end
                system("CLS");                                                                  //clears screen
             }
             else if (rtype == "v=ir") {                                                        //I think it is probablyobvious what this piece of code does
                cout<<"Please enter the supply voltage (V): ";
                cin>>voltage;
                cout<<endl;
                cout<<"Please enter the circuit current (A) : ";
                cin>>current;
                resistancet = voltage / current;
                cout<<endl;
                cout<<"The total resistance = "<<resistancet<<" Ohms"<<endl;
                cin.get();
                cout<<endl;
                cout<<"Would you like to continue? (yes / no): ";
                getline(cin,end);
                system("CLS");
             }
             else {                                                                               //basically if the type of resistance calcultion is entered wrong then it will do the following
             cout<<"Sorry this calculator cannot do that!"<<endl;                                 //another no input line
             cout<<endl;
             cout<<"Would you like to continue? (yes / no): ";                                    //asks the user if they want to end
             getline(cin,end);                                                                    //gets the string end
             system("CLS");                                                                       //clears screen
             }
       }                                                                                          //ends all the resistance code
       else {                                                                                     //as above just incase the calculation input is wrong, ie ie you type fgakjk instead of voltage
          cout<<"Sorry this calculator cannot do that!"<<endl;
          cout<<endl;
          cout<<"Would you like to continue? (yes / no): ";
          getline(cin,end);
          system("CLS");
       }
      }
      while (end == "yes");                                                                        //end of the do/while loop, as long as the end string is equal tio yes then the programme will stay open
    
    
      return 0;
    }                                                                                              //ends the entire programme

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    God that is a bit long.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Perhaps it is too long for the experts.

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Sometimes programs get big...you should take a look at some of my MFC Programs heh. 1 Suggestion tho, you could put the "Would you like to continue" line after the If statements, you don't need them on every case. Other than that, it looks straight.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Thanks,is there anyway of splitting that into programmes. The working out voltage section as a whole programme?

  6. #6
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    why not? Copy Paste it.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  7. #7
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    1 question, why are you calling it physics? It looks like an electronics/electricity program to me.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    How do you link in the programme? Some sample code would be useful, I am a nube.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Quote Originally Posted by durban
    1 question, why are you calling it physics? It looks like an electronics/electricity program to me.
    I am sorry it was just a name i gave to it becuae electronics was a section in our Physics course but it is saved as ElecClaculator on my PC

  10. #10
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    you can't link, you will need to start a new project and copy paste what you want to it. If you mean you want multiple files you will have to make a new file with whatever IDE you're using and link it with #include. Then you will need to make a function to which you can call from the main section.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Is it possible to create a function to do all the voltage stuff, ie you can just type if voltage then do the voltage function?

  12. #12
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    yes, for more information on making functions look up a C++ Functions tutorials. I think there are some on this site.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Closing a programme with cin.get
    By Dontgiveup in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2009, 02:35 PM
  2. How to view the programme before it disappears
    By yousuf in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 08:12 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Gui Programme does'nt Compiles from DOS
    By Shadowhunt in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2003, 08:05 AM
  5. February 1, 2019...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 98
    Last Post: 08-03-2002, 07:24 AM