C++ newbie :(

This is a discussion on C++ newbie :( within the C++ Programming forums, part of the General Programming Boards category; Hi, I just started taking C Language class and my first project is due Friday. Was gonna ask for help ...

  1. #1
    vege^
    Guest

    C++ newbie :(

    Hi, I just started taking C Language class and my first project is due Friday. Was gonna ask for help cos it seems my whole program is ok except i dont know why the compiler won't let me use the sqrt() function :@

    Here is a "copy" of the program:
    #include <iostream.h>
    int main() {

    int a,b,c,d,e,f,g;
    cout << "*** Program to solve the ecuation Ax^2 + Bx + C = 0 ***\n"<< "Enter the value of the coefficients.\n";

    //coeficientes
    cout << "A=";
    cin >> a;
    cout << "B=";
    cin >> b;
    cout << "C=";
    cin >> c;

    cout << "*** The equation to solve is:" <<a<<"X^2 + "<<b<<"X + " <<c<<" = 0\n";

    d = (b*b + 4*a*c);
    e = (-(b));
    f = 2*a;
    g = sqrt(d);

    if (d<0)
    cout << "*** The equation has no real solutions.\n";

    if (d>0)
    cout << "*** The equation has two real solutions.\n";
    cout << "The solutions are: " << -b + sqrt(d)/f <<"and" << -b - sqrt(d)/f; endl;

    if (d=0)
    cout << "*** The equation has only one real solution.\n";
    cout << "The solution is: " << -b/f; endl;



    return 0;
    }

    Be aware it may be full of error but heck its my first try :/
    any help would be greatly appreciated

    btw, the proggy im using is Microsoft Visual C++ 6.0

  2. #2
    vege^
    Guest

    Post

    gah im such a newb

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Add this to to the top of your program and the sqrt() should work:
    Code:
    #include<math.h>

  4. #4
    vege^
    Guest
    ok after some alterations now it looks like this:

    Code:
    #include <iostream.h>
    #include <math.h>
    int main() {
    
    	float a,b,c,d,e,f,g;
    	cout << "*** Program to solve the ecuation AX^2 + BX + C = 0 ***\n"<< "Enter the value of the coefficients.\n";
    	
    	cout << "A=";
    	cin >> a;
    	cout << "B=";
    	cin >> b;
    	cout << "C=";
    	cin >> c;
    
    	cout << "*** The equation to solve is:" <<a<<"X^2 + "<<b<<"X + " <<c<<" = 0\n";
    
    	d = (b*b - 4*a*c);
    	e = (-(b));
    	f = 2*a;
    	g = e/f;
    
    if (d=0)
      cout << "*** The equation has only one real solution. \n" << "The solution is: " << g << "\n";
    	
    if (d<0)
      cout << "*** The equation has no real solutions.\n";
    
    if (d>0)
      cout << "*** The equation has two real solutions.\n" << "The solutions are: " << (-(b) + sqrt(d))/f <<" and " << (-(b) - sqrt(d))/f<<"\n";
    
    
    return 0;
    }
    what's troubling me is that if d = 0 (a=1 b=2 c=1), nothing happens, the program just states that *** The equation to solve is 1X^2 + 2X + 1 = 0, completely ignoring my "if (d=0) command

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You need to write "if (d==0)"

    With only one equal sign you are assigning d to equal zero. Then since it equals zero the next two if statements trigger!

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Please read the FAQ

    vege^, Now that you have your answer...

    CODE TAGS: Check out the link at the top of the board that says "Posting code? read this first!" Code tags make your code more readable.

    TITLE: Try to make your title more descriptive of your question. You should have titled your question "Square Root?" or just ()" A good title will get the attention of someone who can answer your question. The rumor is that some people don't read posts with vague titles.

    [EDIT]
    VERY GOOD! I see you used code tags in your next post!
    Last edited by DougDbug; 02-19-2003 at 04:06 PM.

  7. #7
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    = is the assignment operator
    Perhaps what you really want to use is ==

    EDIT: wow i'm slow
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Since you are a newbie, I recommend a few things. Use more descriptive variable names. a, b, and c, are fine for the equaltion, but d, e, f, and g can be named better names. Also, you may want to add in some doubles instead of ints so that your solutions account for floating point numbers. Use brackets when you want more than one thing in an if statement. Only the line right after the if, if done without brackets, will execute with the if. for example:

    Code:
    if (d>0)
    cout << "*** The equation has two real solutions.\n";
    cout << "The solutions are: " << -b + sqrt(d)/f <<"and" << -b - sqrt(d)/f; endl;
    the second cout will execute all the time.

    also if you are going to store sqrt(d) in g, you may want to do:
    Code:
    (e + g)/f
    OR
    (e / f) + (g / f)
    otherwise, you don't need the variables, in which you don't but if they are there, you might as well use them.

    The way you have your code right now will only divide sqrt(d) by f, and not -b. In which the equation is:
    -b +- sqrt(b*b - 4ac)
    --------------------------
    2a
    *note that it is (b*b - 4ac) not +4ac

    other than a few syntax errors, I hope this helps some.

    edit: wow, I'm slow. okay, you can disregard this post. looks like you have fixed some of the errors.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    alpha, i didnt quite get it when you explained the brackets after the if commands, you know, so it takes multiple lines instead of the next one.

    also, what names would you suggest for d,e,f,g?
    the early bird gets the worm but the second mouse gets the cheese

  10. #10
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    variable names like d, e, f and g are quite cryptic don't ya think?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Well, since it is a simple program, d, e, f, g; are fine, but more descriptive names are best when working with larger programs.
    i.e. discriminate instead of d, negative_b, two_a, etc.

    as for if statements:

    say you have the following if statement:
    Code:
    if(d == 0) {
    	std::cout << "Solution is: " << std::endl;
    	std::cout << "Two" << std::endl;
    	std::cout << "Example" << std::endl;
    }
    the code in the if will only execute when d equals zero.
    otherwise, if the code is like so:
    Code:
    if(d == 0) 
    	std::cout << "Solution is: " << std::endl;
    	std::cout << "Two" << std::endl;
    	std::cout << "Example" << std::endl;
    if d equals zero, all three lines will execute. if d does not equal zero, then the first line will not execute, but the next two lines will. so, if you want more than one line to execute with an if, then it is best to use brackets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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