Hello Forum/ Simple Program help

This is a discussion on Hello Forum/ Simple Program help within the C++ Programming forums, part of the General Programming Boards category; Hello forum friends I just started learning C++ on my own and naturally its a bit tricky but the challlenge ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    Question Hello Forum/ Simple Program help

    Hello forum friends

    I just started learning C++ on my own and naturally its a bit tricky but the challlenge is kind of the point right. anyways I wrote "hello world" and than a simple program to multiply two numbers and then I decided to apply it to the quadratic equation and now I am stuck.

    Right now the compiler gets to the
    Code:
    return ((-b)-(sqrt(pow(b,2)-4*a*c)))/(2*a)
    line before telling me that b is undefined in this function.

    Here is my program so far:
    Code:
    #include <iostream>
    using namespace std;
    int x1();
    int x2();
    int sqrt;
    int main()
    {
    float a, b, c;
    cout<<"let's solve a quadratic equation.\n\n";
    cout<<"What is the coefficient of the first term? ";
    cin>>a;
    cin.ignore();
    cout<<"What is the coefficient of the second term? ";
    cin>>b;
    cin.ignore();
    cout<<"What is the constant? ";
    cin>>c;
    cin.ignore();
    cout<<"X=";
    int x1;
    }
    int x1
    {
        return ((-b)-(sqrt(pow(b,2)-4*a*c)))/(2*a)
    }
    int x2
    {
        return ((-b)+(sqrt(pow(b,2)-4*a*c)))/(2*a)
    }
    I think the problem is mostly that I don't know how to properly format the equation itself in the variable to be returned ( hope that makes sense). It also looks like I will run into some problems with the cin.get/cin.ignore stuff too but the main thing I am struggling with is just getting that equation to spit out the answer.

    I will keep playing with it but any help would be appriciated

    Thank You

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    It looks like you did not define the functions x1 and x2 correctly. You should be declaring some parameters (a, b and c) for these functions, and then passing the local variables a, b, and c as arguments when you call these functions.

    Incidentally, it might make more sense to define a function to compute the discriminant, and then pass the discriminant to x1 and x2, perhaps after checking whether the discriminant is negative.

    Oh, and remember to indent your code reasonably and consistently.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    the problem involved not including <math.h> mostly (which I now know exists) and also that I was doing things with the x1 and x2 that didn't make sense and I while I still dont know why I have fixed it

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    int x1();
    int x2();
    int main()
    {
    float a, b, c;
    cout<<"let's slove a quadratic equation.\n\n";
    cout<<"What is the coefficient of the first term? ";
    cin>>a;
    cin.ignore();
    cout<<"What is the coefficient of the second term? ";
    cin>>b;
    cin.ignore();
    cout<<"What is the constant? ";
    cin>>c;
    cin.ignore();
    cout<< ((-b)-(sqrt(pow(b,2)-4*a*c)))/(2*a)<<","<<((-b)-(sqrt(pow(b,2)-4*a*c)))/(2*a);
    cin.get();
    }
    will now run the quadratic equation properly

    but thanks for the tip about the discriminant laserlight thats a good idea and I am gonna start on that next.
    Last edited by beyondhuman; 02-27-2009 at 02:18 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Quote Originally Posted by beyondhuman
    the problem involved not including <math.h> mostly (which I now know exists)
    C++ borrowed/stole <math.h> from C and calls it <cmath> with some additions and changes to the functions provided. As such, I suggest that you #include <cmath> instead.

    Quote Originally Posted by beyondhuman
    I was doing things with the x1 and x2 that didn't make sense and I while I still dont know why I have fixed it
    It looks like you "fixed it" by not defining those functions, so essentially you declared the functions x1 and x2 but never did anything else with them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 03:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 03:46 PM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21