Need help with this code

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Dartz150, Feb 18, 2014.

  1. Dartz150
    OP

    Dartz150 GBATemp Official Lolicon Onii-chan™

    Member
    1,406
    845
    May 5, 2010
    Mexico
    On a Strange Journey
    Well, a friend of mine is learning how to program in C++, I'm too, but I'm still a noob. She needs to make a cuadratic calculator, but she tells me that the program, when compiled, gives an error "permission denied". She uses dev c++ btw. Here, can you take a look to the code?, I will really apreciate if someone can figure out the problem:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    #include <conio.h>
    using namespace std;
    int main(int argc, char *argv[])
    {
        int a = 0;
        int b=0;
        int c = 0 ;
        int n= 4;
        double x1=0;
        double x2=0;
        cout <<"ingresa el valor de
    a"<< endl;
        cin >>a;
        cout <<"imngresa el valor de
    d"<< endl;
        cin >>b;
        cout<<" ingresa el valor de
    c"<< endl;
        cin >> c;
     
    /***aplicacion de la formula
    general*/
        int resultadoFormula =
    (b*b)-(n*a*c);
        if( a ==0)
    {
     
          cout <<"el resultado en 0"<<
    endl;
     
    }
     
     
     
    else
    {
        x1=
    (-b+sqrt(resultadoFormula))/(2*a);
        x2 =
    (-b-sqrt(resultadoFormula))/(2*a);
        if(x1 == x2)
          {
              cout << "La unica solucion
    es:" << x1;
            }
            else
            {
              cout << "La primera
    solucion es: " << x1 << endl;
              cout << "La segunda
    solucion es: " << x2 << endl;
     
            }
        }
     
     
        system("PAUSE");
     
        return 0;
    }
    
    Thanks in advance :D

    EDIT: fixed the code
     
  2. Oswarlan

    Oswarlan GBAtemp Regular

    Member
    148
    35
    May 28, 2013
    United States
    Permission denied when trying to compile or when trying to run? If when trying to compile, make sure you are allowed to write to the directory you compile to. Also, a friend of mine had a similar problem, with their antivirus blocking compilation and preventing writing the file, so maybe try disabling that temporarily.
     
  3. Dartz150
    OP

    Dartz150 GBATemp Official Lolicon Onii-chan™

    Member
    1,406
    845
    May 5, 2010
    Mexico
    On a Strange Journey
    Yup, I think the same, thanks, I'll tell her to dissable th Antivirus or try another directory.

    EDIT: The problem is still there, anyone can have a look to the code?
     
  4. trumpet-205

    trumpet-205 Embrace the darkness within

    Member
    4,363
    542
    Jan 14, 2009
    United States
    My C++ skill is rusty, so give me a while to look over the code.

    In the mean time, tell your friend NOT to use system("PAUSE"), here is an article telling you why you should avoid it.
    http://www.gidnetwork.com/b-61.html
     
    Dartz150, Costello and Oswarlan like this.
  5. Oswarlan

    Oswarlan GBAtemp Regular

    Member
    148
    35
    May 28, 2013
    United States
    I've seen many cases of people bashing system("pause") yet none ever gave a good reason to not use it. You have. Thanks.

    Also, the code wasn't the issue for the OP, it, minus being in another language, looks fine.
     
    Dartz150 likes this.
  6. trumpet-205

    trumpet-205 Embrace the darkness within

    Member
    4,363
    542
    Jan 14, 2009
    United States
    Like Oswarlan have said, code itself is fine. I also went ahead and compile it using Orwell Dev-C++ / TDM-GCC and it ran fine.
     
    Dartz150 likes this.
  7. FAST6191

    FAST6191 Techromancer

    pip Reporter
    23,204
    8,967
    Nov 21, 2005
    My C++ was never great so I can not even claim I am rusty, to that end I may well be embarrassing myself.

    Still why are a,b and c ints? Why is 4 a variable, I do not even see a way it makes the program more extensible?

    You have bounds checking for 0 but I see nothing for imaginary/complex answers (solve X^2+1 for example).
    You have no input checking either, or at least no user level checking and messages.

    Again my C++ was never great and it could turn out C++ will implicitly convert (you are already handling floats so I do not think the speed/memory penalties will be an issue doing it from the start and doubles should be more than capable of the necessary accuracy), 4 being a variable is stylistic at best (though I would possibly acknowledge that as a global or something) and for all I know the default C++ maths stuff/maths.h will handle complex numbers without breaking a sweat.
     
    Dartz150 likes this.
  8. Dartz150
    OP

    Dartz150 GBATemp Official Lolicon Onii-chan™

    Member
    1,406
    845
    May 5, 2010
    Mexico
    On a Strange Journey
    Thank you very much guys, this was a lot of help. I have compiled code too and it ran flawlesly too, the problem must be my friend's PC, so I'll let her to compile the code on mine. FAST6191 I'll tell my friend to look to your advices, I bet they will be useful for her, thanks. Next time I will have more knowledge about programing so the next guy who will request advice will be myself, maybe in the chatroom :P.

    Thanks again :).