-
Please Help!!!
Hey guys I am a newbie with C++, and I am trying to write a program that calculates the roots for the quadratic equation, with one restriction. The value for a must be bigger than 0. I wrote the program compile it and it is working, but when I go the .exe file and open it, the program starts it allows me to enter the values for a,b, and c and then when it should display the discriminant the console windows just exits, no error messages nothing. I tried changing “int main ()” to “void main ()” without effect. When I go to debug and start debugging at the debug window a message is displayed :
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
The thread 0xC10 has exited with code 0 (0x0).
The program 'C:\Documents and Settings\HQZ\My Documents\Documents\In Progress\CSC 2311\Debug\Project 1.exe' has exited with code 0 (0x0).
And here is the actual source code:
Code:
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
int main()
{
double a, b, c, discr, x_1, x_2, discr_1;
cout<<endl;
cout<<"ax^2 + bx + c"<<endl<<endl;
cout<<"Please enter the values for a, b, and c using the format above."<<endl<<endl;
cout<<"Please enter value for a: ";
cin>> a;
while (a <= 0)
{
cout<<endl<<"The value for a must be larger than 0"<<endl;
cout<<endl<<"Please enter a new value for a: ";
cin>> a;
}
cout<<"Please enter value for b: ";
cin>> b;
cout<<"Please enter value for c: ";
cin>> c;
discr = b*b - 4*a*c;
cout<<endl<<"The discriminant for the equation is: "<<discr<<endl<<endl;
if (discr <=0)
{
cout<<"The value for the expression is not in the real numbers!"<<endl<<endl;
}
else
discr_1 = sqrt(discr);
x_1 = -b+discr_1;
x_2 = -b-discr_1;
cout<<"The two roots of the quadratic equation are:"<<endl<<endl<<endl;
cout<<setw(30)<<x_1<<endl;
cout<<setw(30)<<x_2<<endl<<endl<<endl;
return 0;
}
Thanks :confused:
-
first,
Code:
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
bad bad non standard code!!
instead use
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
could one of the mods sticky that? it's one of the most frequent problems on this site.
second, you actual problem relates to visual studio's way of displaying console code. Try running the program from the command line or put a cin.get() at the end of the program. This is a frequently asked question
-
You do realize that if discr <= 0 then, discr_1 is now undefined, and then it goes ahead and merrily tries to computer x_1 and x_2 which is not recommended...
If you want to abort early, either slap a return 0; statement in after you find that discr <= 0, or change your if-else block to catch it better.
Also, if the calculation succeeds, you immediately process the results, and then return 0, which will close the console window immediately.
You should probably put something in that says:
Code:
char ch;
cout << "Enter any character to continue: ";
cin >> ch;
This is a quick example however, but basically waits for someoen to type something in before immediately exiting.
Edit: and yeah iostream.h and iomanip.h are especially bad, as they are old, use the newer and better standard, (w/o the h).
-
your problem doesn't lie with any coding error as such, its just that your program is running straight through from where you enter a value for c - it executes all the code that follows, but before you get a chance to view the program's output. what you need is to make the program wait for you to press a key before it can quit - read this faq:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
also, your method for calculating the roots is wrong - the quadratic formula is:
Code:
x = (-b (+ or -) sqrt(b^2 - 4ac))/2a
that lot should sort you out
-
Richie T thanks for the 2a I totally forgot about it, and to the rest of you for trying to help, unfortunately neither one of the ways listed above helped. Somehow if I hit the execute button on the visual Studio the program runs fine, but if I go to the .exe file location and double click to start it the program suddenly close after the values for a,b, and c are entered and neither one of the methods above worked. Any other ideas?
-
List your new code then. You did make the change to discr_1 right? Also you corrected the quad formula as well right?
-
you need two cin.get() 's at the end.
The newline character is still sitting in the input buffer after
cin>> c;
the newline gets consumed by your first call to cin.get()
The next cin.get() will wait for more input.
-