Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Can Someone Help Figure out what I did wrong?

Code
#include <iostream>

class Olaw{

    private:
    double dblcurrent;
    double dblresistance;
    double dblvoltage;
    double dbldata[];
    char chacase;
    public:
        Olaw Olaw{

        void setr (double dblr){
            dblresistance = dblr;
        }
        void setc (double dblc){
            dblcurrent = dblc;
        }
        void setv (double dblv){
            dblvoltage = dblv;
        }
        double getr (){
            return dblresistance;
        }
        double getc (){
            return dblcurrent;
        }
        double getv(){
            return dblvoltage;
        }
        void calcr(){
            dblresistance = dblvoltage / dblcurrent;

        }
        void calcc(){
            dblcurrent = dblresistance * dblvoltage;

        }
        void calcv(){
            dblvoltage = dblresistance * dblcurrent;

        }
        void input(){

        if (chacase == 'C'){
            out << "Please Enter the Resistance" << endl;
            cin >> dbldata[0];
            setr(dbldata[0]);

            cout << "Please Enter the Current" << endl;
            cin >> dbldata[1];
            setc(dbldata[1]);
        }

        if (chacase == 'B'){

            out << "Please Enter the Resistance" << endl;
            cin >> dbldata[0];
            setr(dbldata[0]);

            cout << "Please Enter the Voltage" << endl;
            cin >> dbldata[1];
            setv(dbldata[1]);
        }

        if (chacase == 'A'){

            cout << "Please Enter the Current" << endl;
            cin >> dbldata[0];
            setc(dbldata[0]);

            cout << "Please Enter the Voltage" << endl;
            cin >> dbldata[1];
            setv(dbldata[1]);
        }
    }

        double output{
        switch(chacase){
            case 'A':
                input();
                calcr();
            cout << "Your Resistance is:" << getr() endl;
            break;
            case 'B':
                input();
                calcc();
            cout << "Your Current is:" << getc() endl;
            break;
            case 'C':
                input();
                calcv();
            cout << "Your Voltage is:" << getv() endl;
            break;    

        }

}
};
There is a lot wrong. What is double output{ ? is were you trying to create a function?
Some languages I know are Delphi, Pascal, Java, C, C++, BASH, AWK, GEL, Ruby, Python, Lazarus, HTML, PHP, SQL, and C#.
If you can't see some of the many things wrong with this code then you are either a) too lazy, b) didn't bother to look, or c) just trying to write out a bunch of code that you don't understand and should probably learn the basics first. I would say this is d) all of the above however.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Code
double dbldata[];
This, you need to initialize the array to some value..... if you do not want to, try using dynamic memory. Also why not google your compiler's errors?
read about arrays and dynamic memory
cplusplus.com/ doc/tutorial/arrays/
cplusplus.com/ doc/tutorial/dynamic/
^ cannot post links yet (delete space between the backslash and "doc")
Ok, I fixed up your program so it actually works - there is a lot of problems with this code. There are many errors, but on the bigger scale you created a class and never created the object itself. For example, you created this 0law class, but there is no object. There is no main method either.

You also tried creating an array dbdatap[] - you need to declare the size, unless you are going to dynamically allocate memory. It doesn't seem like you understand the basics of objects and classes, private and public, and I have a hard time believing you wrote this.

Anyways, here is the code - It works for a one time run through - if you want to make it work many times, you have to fix a few things in particular, like assigning data the the same elements - you have the inputs for cin as dbdata[0] and [1], but if you run it multiple times you are going to get an area - you need to add an increment-or to step through the elements of the array and find an empty "slot" for new data.
Code
#include <iostream>
#include <cstring>
using namespace std;

class Olaw{        // This class is responsible for calculations and data for voltage program

    private:
    double dblcurrent;        // Holds the current
    double dblresistance;    // Holds the resistance
    double dblvoltage;        // Holds the voltage
    double dbldata[100];    // This array wasn't declared properly, it was empty [] - you have the option of declaring it like this
    // if you are going to dynamically allocate memory, but make sure you delete it after.
    char chacase;            // Holds user choice

    public:

        // Mutators, change data - you might want to make them constants too

        void setCharCase(char choice)    // Assigns user choice to chacase
        {
            chacase = choice;
        }
      void setr (double dblr)            
        {
      dblresistance = dblr;
      }
      void setc (double dblc)
        {
      dblcurrent = dblc;
      }
      void setv (double dblv)
        {
      dblvoltage = dblv;
      }
        // Accesor functions
      double getr ()
        {
      return dblresistance;
      }
      double getc ()
        {
      return dblcurrent;
      }
      double getv()
        {
      return dblvoltage;
      }
        // Calculator functions
      void calcr()
        {
      dblresistance = dblvoltage / dblcurrent;
      }
      void calcc()
        {
      dblcurrent = dblresistance * dblvoltage;
      }
      void calcv()
        {
      dblvoltage = dblresistance * dblcurrent;
      }
        // Get user input
      void input()
        {

             if (chacase == 'C')
            {
             cout << "Please Enter the Resistance: ";
      cin >> dbldata[0];
      setr(dbldata[0]);
            cout << endl;

      cout << "Please Enter the Current: ";
      cin >> dbldata[1];
      setc(dbldata[1]);
            cout << endl;
             }

             if (chacase == 'B')
            {

      cout << "Please Enter the Resistance: ";
      cin >> dbldata[0];
      setr(dbldata[0]);
            cout << endl;

      cout << "Please Enter the Voltage: " << endl;
      cin >> dbldata[1];
      setv(dbldata[1]);
            cout << endl;
             }

             if (chacase == 'A')
            {

      cout << "Please Enter the Current: ";
      cin >> dbldata[0];
      setc(dbldata[0]);
            cout << endl;

      cout << "Please Enter the Voltage: ";
      cin >> dbldata[1];
      setv(dbldata[1]);
            cout << endl;
            }
        }

};

int main()
{
    Olaw lawObject;    // Create a 0law object instance
    char choice;
    cout << "Welcome to the Voltage program!\n"
         << "Press A and then enter to calculate resistance" << endl
         << "Press B and then enter to calculate current" << endl
         << "Press C and then enter to calculate volate" << endl
         << "Press Z to quit" << endl
         << endl << endl
         << "Please enter your choice: ";

        cin >> choice;
        choice = toupper(choice);

        lawObject.setCharCase(choice); // Assign choice to chachoice

      switch(choice)
        {
      case 'A':    
      lawObject.input();
      lawObject.calcr();
      cout << "Your Resistance is: " << lawObject.getr() << endl;
      break;
      case 'B':
      lawObject.input();
      lawObject.calcc();
      cout << "Your Current is: " << lawObject.getc() << endl;
      break;
      case 'C':
      lawObject.input();
      lawObject.calcv();
      cout << "Your Voltage is: " << lawObject.getv() << endl;
      break;  
            case 'Z':
                exit(0);

      }
        cin.get();
        cin.ignore();
    return 0;
}


"If you can't see some of the many things wrong with this code then you are either a) too lazy, b) didn't bother to look, or c) just trying to write out a bunch of code that you don't understand and should probably learn the basics first. I would say this is d) all of the above however."

I doubt he even wrote this.
^ Nice, but you should probably just tell him what he did wrong and leave it at that, this is most likely homework so i do not suggest you do it for him.
If it is an assignment, and he didn't have a clue where to start, I have a hard time his teacher will believe this is his work.
(Dec 1, 2014 12:46 AM)Phil Program Wrote:  Ok, I fixed up your program so it actually works - there is a lot of problems with this code. There are many errors, but on the bigger scale you created a class and never created the object itself. For example, you created this 0law class, but there is no object. There is no main method either.

You also tried creating an array dbdatap[] - you need to declare the size, unless you are going to dynamically allocate memory. It doesn't seem like you understand the basics of objects and classes, private and public, and I have a hard time believing you wrote this.

Anyways, here is the code - It works for a one time run through - if you want to make it work many times, you have to fix a few things in particular, like assigning data the the same elements - you have the inputs for cin as dbdata[0] and [1], but if you run it multiple times you are going to get an area - you need to add an increment-or to step through the elements of the array and find an empty "slot" for new data.
Code
#include <iostream>
#include <cstring>
using namespace std;

class Olaw{        // This class is responsible for calculations and data for voltage program

    private:
    double dblcurrent;        // Holds the current
    double dblresistance;    // Holds the resistance
    double dblvoltage;        // Holds the voltage
    double dbldata[100];    // This array wasn't declared properly, it was empty [] - you have the option of declaring it like this
    // if you are going to dynamically allocate memory, but make sure you delete it after.
    char chacase;            // Holds user choice

    public:

        // Mutators, change data - you might want to make them constants too

        void setCharCase(char choice)    // Assigns user choice to chacase
        {
            chacase = choice;
        }
      void setr (double dblr)            
        {
      dblresistance = dblr;
      }
      void setc (double dblc)
        {
      dblcurrent = dblc;
      }
      void setv (double dblv)
        {
      dblvoltage = dblv;
      }
        // Accesor functions
      double getr ()
        {
      return dblresistance;
      }
      double getc ()
        {
      return dblcurrent;
      }
      double getv()
        {
      return dblvoltage;
      }
        // Calculator functions
      void calcr()
        {
      dblresistance = dblvoltage / dblcurrent;
      }
      void calcc()
        {
      dblcurrent = dblresistance * dblvoltage;
      }
      void calcv()
        {
      dblvoltage = dblresistance * dblcurrent;
      }
        // Get user input
      void input()
        {

             if (chacase == 'C')
            {
             cout << "Please Enter the Resistance: ";
      cin >> dbldata[0];
      setr(dbldata[0]);
            cout << endl;

      cout << "Please Enter the Current: ";
      cin >> dbldata[1];
      setc(dbldata[1]);
            cout << endl;
             }

             if (chacase == 'B')
            {

      cout << "Please Enter the Resistance: ";
      cin >> dbldata[0];
      setr(dbldata[0]);
            cout << endl;

      cout << "Please Enter the Voltage: " << endl;
      cin >> dbldata[1];
      setv(dbldata[1]);
            cout << endl;
             }

             if (chacase == 'A')
            {

      cout << "Please Enter the Current: ";
      cin >> dbldata[0];
      setc(dbldata[0]);
            cout << endl;

      cout << "Please Enter the Voltage: ";
      cin >> dbldata[1];
      setv(dbldata[1]);
            cout << endl;
            }
        }

};

int main()
{
    Olaw lawObject;    // Create a 0law object instance
    char choice;
    cout << "Welcome to the Voltage program!\n"
         << "Press A and then enter to calculate resistance" << endl
         << "Press B and then enter to calculate current" << endl
         << "Press C and then enter to calculate volate" << endl
         << "Press Z to quit" << endl
         << endl << endl
         << "Please enter your choice: ";

        cin >> choice;
        choice = toupper(choice);

        lawObject.setCharCase(choice); // Assign choice to chachoice

      switch(choice)
        {
      case 'A':    
      lawObject.input();
      lawObject.calcr();
      cout << "Your Resistance is: " << lawObject.getr() << endl;
      break;
      case 'B':
      lawObject.input();
      lawObject.calcc();
      cout << "Your Current is: " << lawObject.getc() << endl;
      break;
      case 'C':
      lawObject.input();
      lawObject.calcv();
      cout << "Your Voltage is: " << lawObject.getv() << endl;
      break;  
            case 'Z':
                exit(0);

      }
        cin.get();
        cin.ignore();
    return 0;
}


"If you can't see some of the many things wrong with this code then you are either a) too lazy, b) didn't bother to look, or c) just trying to write out a bunch of code that you don't understand and should probably learn the basics first. I would say this is d) all of the above however."

I doubt he even wrote this.

Not bad, but your getter functions should be const member functions which return just the values. Ex:
Code
double getr() const
    {
      return dblresistance;
    }
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Oh true...my mistake, I shouldn't have put that under mutators/transformers. Thanks for the feedback :P
thanks alot, this was an assignment it helps to really see what I did wrong. The assignment I got was a little simpler then this, but I decided to use what I already read ahead in my studies to finish it. But now this puts alot of things in perspective thanks.

(Dec 1, 2014 12:46 AM)Phil Program Wrote:  Ok, I fixed up your program so it actually works - there is a lot of problems with this code. There are many errors, but on the bigger scale you created a class and never created the object itself. For example, you created this 0law class, but there is no object. There is no main method either.

You also tried creating an array dbdatap[] - you need to declare the size, unless you are going to dynamically allocate memory. It doesn't seem like you understand the basics of objects and classes, private and public, and I have a hard time believing you wrote this.

Anyways, here is the code - It works for a one time run through - if you want to make it work many times, you have to fix a few things in particular, like assigning data the the same elements - you have the inputs for cin as dbdata[0] and [1], but if you run it multiple times you are going to get an area - you need to add an increment-or to step through the elements of the array and find an empty "slot" for new data.
Code
#include <iostream>
#include <cstring>
using namespace std;

class Olaw{        // This class is responsible for calculations and data for voltage program

    private:
    double dblcurrent;        // Holds the current
    double dblresistance;    // Holds the resistance
    double dblvoltage;        // Holds the voltage
    double dbldata[100];    // This array wasn't declared properly, it was empty [] - you have the option of declaring it like this
    // if you are going to dynamically allocate memory, but make sure you delete it after.
    char chacase;            // Holds user choice

    public:

        // Mutators, change data - you might want to make them constants too

        void setCharCase(char choice)    // Assigns user choice to chacase
        {
            chacase = choice;
        }
      void setr (double dblr)            
        {
      dblresistance = dblr;
      }
      void setc (double dblc)
        {
      dblcurrent = dblc;
      }
      void setv (double dblv)
        {
      dblvoltage = dblv;
      }
        // Accesor functions
      double getr ()
        {
      return dblresistance;
      }
      double getc ()
        {
      return dblcurrent;
      }
      double getv()
        {
      return dblvoltage;
      }
        // Calculator functions
      void calcr()
        {
      dblresistance = dblvoltage / dblcurrent;
      }
      void calcc()
        {
      dblcurrent = dblresistance * dblvoltage;
      }
      void calcv()
        {
      dblvoltage = dblresistance * dblcurrent;
      }
        // Get user input
      void input()
        {

             if (chacase == 'C')
            {
             cout << "Please Enter the Resistance: ";
      cin >> dbldata[0];
      setr(dbldata[0]);
            cout << endl;

      cout << "Please Enter the Current: ";
      cin >> dbldata[1];
      setc(dbldata[1]);
            cout << endl;
             }

             if (chacase == 'B')
            {

      cout << "Please Enter the Resistance: ";
      cin >> dbldata[0];
      setr(dbldata[0]);
            cout << endl;

      cout << "Please Enter the Voltage: " << endl;
      cin >> dbldata[1];
      setv(dbldata[1]);
            cout << endl;
             }

             if (chacase == 'A')
            {

      cout << "Please Enter the Current: ";
      cin >> dbldata[0];
      setc(dbldata[0]);
            cout << endl;

      cout << "Please Enter the Voltage: ";
      cin >> dbldata[1];
      setv(dbldata[1]);
            cout << endl;
            }
        }

};

int main()
{
    Olaw lawObject;    // Create a 0law object instance
    char choice;
    cout << "Welcome to the Voltage program!\n"
         << "Press A and then enter to calculate resistance" << endl
         << "Press B and then enter to calculate current" << endl
         << "Press C and then enter to calculate volate" << endl
         << "Press Z to quit" << endl
         << endl << endl
         << "Please enter your choice: ";

        cin >> choice;
        choice = toupper(choice);

        lawObject.setCharCase(choice); // Assign choice to chachoice

      switch(choice)
        {
      case 'A':    
      lawObject.input();
      lawObject.calcr();
      cout << "Your Resistance is: " << lawObject.getr() << endl;
      break;
      case 'B':
      lawObject.input();
      lawObject.calcc();
      cout << "Your Current is: " << lawObject.getc() << endl;
      break;
      case 'C':
      lawObject.input();
      lawObject.calcv();
      cout << "Your Voltage is: " << lawObject.getv() << endl;
      break;  
            case 'Z':
                exit(0);

      }
        cin.get();
        cin.ignore();
    return 0;
}


"If you can't see some of the many things wrong with this code then you are either a) too lazy, b) didn't bother to look, or c) just trying to write out a bunch of code that you don't understand and should probably learn the basics first. I would say this is d) all of the above however."

I doubt he even wrote this.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by theN20Dec 3, 2020 02:31 PM 9 322 Dec 3, 2020 02:31 PM
Last Post: tsuki
[Python] by OliFlawAug 23, 2017 03:13 AM 7 158 Aug 23, 2017 03:13 AM
Last Post: OliFlaw
[Python] by JakeMay 23, 2017 08:12 AM 3 124 May 23, 2017 08:12 AM
Last Post: szuszi2