•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,560 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,470 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1568 | Replies: 19
![]() |
•
•
Join Date: Oct 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
Hello all,
My assignment is to write a quadratic equation solver. I have written the function, and it compiles well. However that is not the issue. The requirements are that it follows a command line interface specification as follows:
<program name> -r <quadratic equation>
In other words, the user is to enter "-r" followed by the equation, and the program will then output the roots as two numbers on the next line, and that's it. That means I can't have any other expressions like "Enter the value of a..." or "Enter the value of b..." and then cin my b variable. How do I write the piece of code so that all the user has to do is type "-r" and then the equation, and this will call my function and output the results? Any suggestions or guides..?
My assignment is to write a quadratic equation solver. I have written the function, and it compiles well. However that is not the issue. The requirements are that it follows a command line interface specification as follows:
<program name> -r <quadratic equation>
In other words, the user is to enter "-r" followed by the equation, and the program will then output the roots as two numbers on the next line, and that's it. That means I can't have any other expressions like "Enter the value of a..." or "Enter the value of b..." and then cin my b variable. How do I write the piece of code so that all the user has to do is type "-r" and then the equation, and this will call my function and output the results? Any suggestions or guides..?
•
•
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation:
Rep Power: 6
Solved Threads: 97
I find that C++ textbooks in general completely ignore the topic of command line arguments, so I won't tell you to look in the book.
Here are a few links that may help you to understand how to do this:
http://www.cprogramming.com/tutorial/lesson14.html
http://malun1.mala.bc.ca:8080/~wesse...otes/args.html
The short version is that argv[] is an array of C-style strings (char array based, null terminated) where the first (index 0) is the program name.
You need to check the next string to verify it is "-r" before your program continues to then read the a, b, c values from the remaining strings.
Val
Here are a few links that may help you to understand how to do this:
http://www.cprogramming.com/tutorial/lesson14.html
http://malun1.mala.bc.ca:8080/~wesse...otes/args.html
The short version is that argv[] is an array of C-style strings (char array based, null terminated) where the first (index 0) is the program name.
You need to check the next string to verify it is "-r" before your program continues to then read the a, b, c values from the remaining strings.
Val
I am in mourning for my country.
•
•
Join Date: Oct 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
Thanks Val. That will work finely. However, when I wrote my code for my quadratic, I neglected something quite important, actually. Please bear in mind that I am a beginner at this. Let me see if I can explain.
The first argument will automatically be the program name, followed by the command the use will type in. I organized it such that when the use types in "-r", the program will call my function to calculate the roots. Here is my problem. I'm not sure how to write the code such that the program will automatically read the values of a, b and c and do the required computation. My function (which I've aptly named Root_Quadratic") basically acknowledges integers a, b and c and then via an if loop computes the roots and displays the roots. But the step to get my program to KNOW which values are a, b and c, I'm not sure. Remember that I'm writing the entire quadratic equation as my third argument (argv[2]). Once again, some guides will be quite helpful here. I know I'm missing something quite simple, but I just cant figure it out.
The first argument will automatically be the program name, followed by the command the use will type in. I organized it such that when the use types in "-r", the program will call my function to calculate the roots. Here is my problem. I'm not sure how to write the code such that the program will automatically read the values of a, b and c and do the required computation. My function (which I've aptly named Root_Quadratic") basically acknowledges integers a, b and c and then via an if loop computes the roots and displays the roots. But the step to get my program to KNOW which values are a, b and c, I'm not sure. Remember that I'm writing the entire quadratic equation as my third argument (argv[2]). Once again, some guides will be quite helpful here. I know I'm missing something quite simple, but I just cant figure it out.
•
•
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation:
Rep Power: 6
Solved Threads: 97
It would help if you would post the code you have. As I understand what you're trying to do, you will have to treat your inputs from the command line as separate arguments ( argv[2], argv[3] and argv[4]). Each space delimited sequence on the command line is a separate argument.
And keep in mind, these inputs from the command line are strings. You will have to convert them to the numeric values they represent. If you are using only integer values, the the atoi( ) function will be handy.
Val
And keep in mind, these inputs from the command line are strings. You will have to convert them to the numeric values they represent. If you are using only integer values, the the atoi( ) function will be handy.
Val
I am in mourning for my country.
•
•
Join Date: Oct 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
The code is basic. (I dont have it with me. My apologies.)
Not that I'm recalling from memory, so there may be some stuff I'm forgetting, but I believe I have the gist of it. Ignore the syntactical errors I made. My issue is that the function declares a, b and c as integers and so on. But what do I need to write again such that when the command -r at argv[1] and the quadratic equation at argv[2] are typed and this function is called, the program will automatically know which values of the quadratic equation the user typed are a,b and c.
Think of it this way:
It's similar to other simple programs which find the root of a quadratic with the exception that there is no
cout<< "Enter the value of a:" <<endl;
cout<< "Enter the value of b:" <<endl;
cout<< "Enter the value of c:" <<endl;
And then the user enters these values and the program uses a formula (similar to that above) and computes two roots and then displays these roots. My code is similar WITH THE EXCEPTION that the user is not prompted to enter these values hence the program must know which value is which so at to manipulate these values in the formula. That's the problem.
Gerron.
void Root_Quadratic ();
int a;
int b;
int c;
double r1;
double rt2;
double d_crim;
d_crim = pow(b,2) - (4 * a * c);
r1 = (-b + sqrt(d_crim)) / (2 * a);
r2 = (-b - sqrt(d_crim)) / (2 * a);
cout << r1 << r2 << endl;Not that I'm recalling from memory, so there may be some stuff I'm forgetting, but I believe I have the gist of it. Ignore the syntactical errors I made. My issue is that the function declares a, b and c as integers and so on. But what do I need to write again such that when the command -r at argv[1] and the quadratic equation at argv[2] are typed and this function is called, the program will automatically know which values of the quadratic equation the user typed are a,b and c.
Think of it this way:
It's similar to other simple programs which find the root of a quadratic with the exception that there is no
cout<< "Enter the value of a:" <<endl;
cout<< "Enter the value of b:" <<endl;
cout<< "Enter the value of c:" <<endl;
And then the user enters these values and the program uses a formula (similar to that above) and computes two roots and then displays these roots. My code is similar WITH THE EXCEPTION that the user is not prompted to enter these values hence the program must know which value is which so at to manipulate these values in the formula. That's the problem.
Gerron.
•
•
Join Date: Oct 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
Maybe I can write an algorithm (well sort of) to explain what I\ve got so far, in terms of how this thing works.
int main (int argc, char* argv[]) <-- obvious by now
//I'm thinking to place the coefficients a, b and c into an array of size 3 and when the command is called, the program will use the values from here to work out the roots.
//Initialize variables here
int coeff[3];
int quadratic;
if(strcmp (argv[1], "-help") == 0) {
cout << help menu <<endl; (the help menu is not available to me right now, so
we'll just work with the bad syntax
}
if(strcmp (argv[1], "-r") == 0) {
argv[2] = quadratic ??
(at this point I would insert the function to find the roots of the quadratic??)
Herein lies two problems.
1. The problem I mentioned before about the values a, b and c.
2. Does the line if placed in bold make any sense?
int main (int argc, char* argv[]) <-- obvious by now
//I'm thinking to place the coefficients a, b and c into an array of size 3 and when the command is called, the program will use the values from here to work out the roots.
//Initialize variables here
int coeff[3];
int quadratic;
if(strcmp (argv[1], "-help") == 0) {
cout << help menu <<endl; (the help menu is not available to me right now, so
we'll just work with the bad syntax
}
if(strcmp (argv[1], "-r") == 0) {
argv[2] = quadratic ??
(at this point I would insert the function to find the roots of the quadratic??)
Herein lies two problems.
1. The problem I mentioned before about the values a, b and c.
2. Does the line if placed in bold make any sense?
•
•
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation:
Rep Power: 6
Solved Threads: 97
1. The problem I mentioned before about the values a, b and c.
2. Does the line if placed in bold make any sense?
1 - Consider what the argv array of strings looks like if I enter the following on the command line:
C:> find_roots -r 3 15 9 <enter>
argv[0] = "find_roots"
argv[1] = "-r"
argv[2] = "3"
argv[3] = "15"
argv[4] = "9"
Each of these is a C-style string- an array of characters have have a NULL terminator ( '\0' ) after the content (that is, argv[1] has content '-' 'r' '\0' )
If you want to put the three number arguments into your coefficient array (integers) you will need to convert each of the strings to the numeric value they represent. The atoi( ) function can do this for you (look it up.) Once you've placed the values in the array, be sure that you pass it as a parameter to your function.
2 No, this line makes no sense. In the first place, you should not change what's in your argv[] array strings - you never know how big the arrays are that hold the data. And the line, as you wrote, doesn't seem like it would do anything approaching what you want. Rather, once you've put the argument values into the coefficient array, simply call your function, like
And, for completeness sake, you might have the function return a value that indicates if it could successfully find roots. What happens if the value of A is 0? Or if the discriminant is negative?
When it comes to dealing with imaginary numbers, I usually leave that to my imaginary friend.
Val
2. Does the line if placed in bold make any sense?
1 - Consider what the argv array of strings looks like if I enter the following on the command line:
C:> find_roots -r 3 15 9 <enter>
argv[0] = "find_roots"
argv[1] = "-r"
argv[2] = "3"
argv[3] = "15"
argv[4] = "9"
Each of these is a C-style string- an array of characters have have a NULL terminator ( '\0' ) after the content (that is, argv[1] has content '-' 'r' '\0' )
If you want to put the three number arguments into your coefficient array (integers) you will need to convert each of the strings to the numeric value they represent. The atoi( ) function can do this for you (look it up.) Once you've placed the values in the array, be sure that you pass it as a parameter to your function.
2 No, this line makes no sense. In the first place, you should not change what's in your argv[] array strings - you never know how big the arrays are that hold the data. And the line, as you wrote, doesn't seem like it would do anything approaching what you want. Rather, once you've put the argument values into the coefficient array, simply call your function, like
root_quadratic( coefficient, r1, r2 ); Structure your function so that the roots you seek are placed in the r1 and r2 parameters.And, for completeness sake, you might have the function return a value that indicates if it could successfully find roots. What happens if the value of A is 0? Or if the discriminant is negative?
When it comes to dealing with imaginary numbers, I usually leave that to my imaginary friend.
Val
I am in mourning for my country.
•
•
Join Date: Oct 2007
Posts: 10
Reputation:
Rep Power: 2
Solved Threads: 0
Thanks Val. I understand when you have just the COEFFICIENTS of a, b and c being entered by the user. However, the specs required the ENTIRE quadratic equation to be entered by the user i.e. the inclusion of a single variable be it x or p or q or whatever. Any suggestions?
On another note, can one suggest how to go about writing functions to integrate and differentiate a quadratic equation/expression. This would involve both coefficient and variable, so once again any suggestions.
On another note, can one suggest how to go about writing functions to integrate and differentiate a quadratic equation/expression. This would involve both coefficient and variable, so once again any suggestions.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Hardware Interrupts & 100% CPU usage (Windows NT / 2000 / XP / 2003)
- Problems with ubuntu (Window and Desktop Managers)
- Simple sql questions (broken commands and retrieving columns) (Database Design)
- Need help with a vb.net project (VB.NET)
- Machine restarts (Troubleshooting Dead Machines)
- Applet won't load (Java)
- help getting !true bool to break out of program (C++)
- It Boggles the Mind Man... (Networking Hardware Configuration)
- Programming Professor (Software Developers' Lounge)
Other Threads in the C++ Forum
- Previous Thread: need help adding positive and negative numbers
- Next Thread: Can this code be converted to TCL from C++ ..



Linear Mode