Need help with this code

DSoryu

GBA/NDS Maniac
OP
Member
Joined
May 5, 2010
Messages
2,356
Trophies
2
Location
In my house
XP
4,757
Country
Mexico
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
 

Oswarlan

Well-Known Member
Member
Joined
May 28, 2013
Messages
178
Trophies
1
XP
2,204
Country
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.
 

DSoryu

GBA/NDS Maniac
OP
Member
Joined
May 5, 2010
Messages
2,356
Trophies
2
Location
In my house
XP
4,757
Country
Mexico
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.
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?
 

Oswarlan

Well-Known Member
Member
Joined
May 28, 2013
Messages
178
Trophies
1
XP
2,204
Country
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

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.
 
  • Like
Reactions: DSoryu

FAST6191

Techromancer
Editorial Team
Joined
Nov 21, 2005
Messages
36,798
Trophies
3
XP
28,321
Country
United Kingdom
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.
 
  • Like
Reactions: DSoryu

DSoryu

GBA/NDS Maniac
OP
Member
Joined
May 5, 2010
Messages
2,356
Trophies
2
Location
In my house
XP
4,757
Country
Mexico
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 :).
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    HoTuan @ HoTuan: how to mod switch ?