need help with this code (heh im a beginner so should be a simple fix)

B

Bshealey786

Okay im doing my final project for my first computer science class(its
my major, so it will be my first of many), but anyway im a beginner so
im not to great with C++ yet. Anyway this is the error msg that im
getting: "Error executing cl.exe"

this is the code that I have, but I know whats causing it, ill just
show you the whole thing first though

//file: Quadratic

#include <iostream.h>
#include <math.h>
#include <string>

void main ()

{

char name [10];
char runagain;
double discriminant;
double root1;
double root2;
double a;
double b;
double c;


cout << "What is your name? " << endl;
cin.getline (name,10);
cout << "Hello " << name << " we are now ready to begin. Lets compute
the roots of your quadratic equation" << endl;

while (char runagain = "y")

{

cout << "A-----> ";
cin >> a;
cout << "B-----> ";
cin >> b;
cout << "C-----> ";
cin >> c;


cout << "The equation is " << a << " * x * x + "<< b << " * x + "<< c
<< endl;



if (a==0 && b==0)
{
cout << " Linear equation, root at " << - c / b << endl;
}
else
{
discriminant = b * b - 4 * a * c;
if (discriminant < 0);
cout << "imaganary roots" << endl;
}


if (discriminant == 0)
{
root1 = - b / 2 * a ;
root2 = root1;
cout << "Two equal real roots: ";
cout << "Root 1 = " << root1 << " and Root 2 " << root2;
}

else
{
root1 = ( - b + sqrt (discriminant) / (2 * a ) );
root2 = ( - b - sqrt (discriminant) / (2 * a ) );
cout << "Two distinct real roots: " << endl;
cout << "Root 1 = " << root1 << " and Root 2 = " << root2 << endl;
}

cout << "Would you like to run this program again, please enter y for
yes, and n for no" << endl;
cin >> runagain;

}

cout << "Have a good day";

}

Okay the program worked, but then I have to put a loop in there and
thats when I starting getting the error, so im sure its my loop
statement thats causing the problem, ive tinkered around with it and
still cant figure out how to fix it. Someone said I needed to add "do"
after my while loop line, but then I get the same error plus one that
says "syntax error: identifier 'cout'". Hopefully this is an easy fix
and someone will be able to help me out, thanks in advance!
 
A

Alf P. Steinbach

* Bshealey786:
Okay im doing my final project for my first computer science class(its
my major, so it will be my first of many), but anyway im a beginner so
im not to great with C++ yet. Anyway this is the error msg that im
getting: "Error executing cl.exe"

this is the code that I have, but I know whats causing it, ill just
show you the whole thing first though

//file: Quadratic

#include <iostream.h>

#include <math.h>
#include <string>

void main ()

'main' must have result type 'int'.
 
B

Bshealey786

lol doing that gave me 26 errors, and I guess we are working with old
versions of c++, bc ive been told thats not what you use anymore, but
thats how it works on ours at school. Thanks though, it worked before
I added in the loop, so the loop has to be causing the error
 
J

joe.els

Here is a number of ideas

Instead of
char name[10];
cin.getline (name,10);
use
#include <string>
std::string name;
cin >> name;

The line
while (char runagain = "y")
will not compile and if it could it would loop forever
You're assigning a char-array (the double quotes) to a char, it should
be
while (char runagain = 'y')
'y' has a decimal value of 121, which is a no zero value and would
resolve to true, hence the loop will loop forever.
Perhaps you wanted to say
char runagain = 'y';
while (runagain == 'y')

The lines
if (a==0 && b==0)
{
cout << ... << - c / b << ...
This asserts a divide by zero condition.
the if-statement checks that b are equal to 0 and then you divide by b.

Forget about the c++ syntax and just check your arithmetic logic first
to avoid faulty logic like the example above.

Joe
 
P

Puppet_Sock

Bshealey786 said:
Okay im doing my final project for my first computer science class(its
my major, so it will be my first of many), but anyway im a beginner so
im not to great with C++ yet. Anyway this is the error msg that im
getting: "Error executing cl.exe"

My telepathy bill didn't show up this month. Maybe you could
give some tiny indication of where this error occurs? I see
lots of output statements in your code. Did any of them work?
this is the code that I have, but I know whats causing it, ill just
show you the whole thing first though

//file: Quadratic

#include <iostream.h>

As others have said, you want <iostream>. That is, assuming your
compiler supports the standard. That means that the items you
refer to are then in a namespace, so things like cout will require
that you refer to them as std::cout or else read about the "using"
keyword.
#include <math.h>
#include <string>

Since you are using the standard <string> header, not string.h
and not <cstring> you need to be aware that this has pulled in
the stuff relating to the standard library class "string" and
not the C-related stuff about char arrays.

You want: C-related char array stuff - include <string.h> or <cstring>
You want: C++ standard class string - include said:
void main ()

This should be

int main(void)

or one of the other standard forms.
{

char name [10];
char runagain;

Hmmmm. Ok, runagain is here.
double discriminant;
double root1;
double root2;
double a;
double b;
double c;


cout << "What is your name? " << endl;
cin.getline (name,10);
cout << "Hello " << name << " we are now ready to begin. Lets compute
the roots of your quadratic equation" << endl;

while (char runagain = "y")

And there's another runagain. And it's not *tested* against 'y'
it's set equal to "y". Hmmm... Does this compile?

You want something like so:

runagain = 'y';
while(runagain =='y')

See, you need to initialize the variable, and you need to test
in the loop, not assign. And you don't want to declare another
variable called runagain.
{

cout << "A-----> ";
cin >> a;
cout << "B-----> ";
cin >> b;
cout << "C-----> ";
cin >> c;


cout << "The equation is " << a << " * x * x + "<< b << " * x + "<< c
<< endl;



if (a==0 && b==0)
{
cout << " Linear equation, root at " << - c / b << endl;
}

In a quadratic eqn ax^2 + bx +c=0, if a and b are both 0, it is not a
linear equation, it's c=0. Or, to smack you from the other direction,
if b is zero, what is -c/b? It's a run-time error is what it is.
else
{
discriminant = b * b - 4 * a * c;
if (discriminant < 0);
cout << "imaganary roots" << endl;
}


if (discriminant == 0)
{
root1 = - b / 2 * a ;
root2 = root1;
cout << "Two equal real roots: ";
cout << "Root 1 = " << root1 << " and Root 2 " << root2;
}

else
{
root1 = ( - b + sqrt (discriminant) / (2 * a ) );
root2 = ( - b - sqrt (discriminant) / (2 * a ) );
cout << "Two distinct real roots: " << endl;
cout << "Root 1 = " << root1 << " and Root 2 = " << root2 << endl;
}

You never checked for a negative discriminant. If it's negatvie, what
will happen when you take its square root? A run-time error is what.
cout << "Would you like to run this program again, please enter y for
yes, and n for no" << endl;
cin >> runagain;

}

cout << "Have a good day";

}

Okay the program worked,

I'm doubting it.
but then I have to put a loop in there and
thats when I starting getting the error, so im sure its my loop
statement thats causing the problem, ive tinkered around with it and
still cant figure out how to fix it. Someone said I needed to add "do"
after my while loop line, but then I get the same error plus one that
says "syntax error: identifier 'cout'". Hopefully this is an easy fix
and someone will be able to help me out, thanks in advance!

So, is your question "how do I do while loops?" If so, you need
to read your text and understand the syntax.
Socks
 
E

Earl Purple

You can use a for loop to initialise a variable then check its value.
So

for ( char runagain='y', runagain=='y'; )
{
// code block in here
}

Alternatively, as you are always going to run the loop once, you can
perform a do..while loop thus:

do
{
// codeblock
// obtain runagain here.
} while ( runagain == 'y' );

Note: it's a linear equation if and only if a is 0 and b is non-zero.
If b is 0 then the regular formula will work, although it can be
shortened to
x = +/- sqrt( -c/a )
 
B

benben

I assume cl.exe is the microsoft compiler so that's a compilation issue.

I don't know you but I always find it helpful to carefully read all those
compiler error messages and even read detail about that error in the
compiler manual.

Further, it you encountered an error and want us to help, it would make a
lot of sense copy and paste all the compiler messages so we know exactly
what's going on.

Ben
 
O

osmium

benben said:
I assume cl.exe is the microsoft compiler so that's a compilation issue.

I don't know you but I always find it helpful to carefully read all those
compiler error messages and even read detail about that error in the
compiler manual.

Further, it you encountered an error and want us to help, it would make a
lot of sense copy and paste all the compiler messages so we know exactly
what's going on.

As near as I can tell his error messages resulted from using a compiler that
was frozen in time and unable to predict the future syntax of the language.
There is real information in the thread, too, so presumably the OP has his
problem fixed by now.
 
P

pillbug

"Error executing cl.exe"

that isn't an error that cl emits. it's coming from nmake or make or
whatever you're using to execute the program. somehow your environment
changed after an edit, which led you to believe your code is at fault.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top