Thread: help on something

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    help on something

    I can't for the life of me figure out how to fix this, please assist.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include "genlib.h"
    
    main()
    {
    	int inputs[3], a, b, c, d, x, x1, x2, i, lastDigit;
    	char *os, *noSol = "No solution\n", *cont = 'y';
    
    	while (cont == 'Y' || cont == 'y')
    	{
    		printf("This program solves a quadratic equation\n");
    		for (i = 1; i <= 3; i++)
    		{
    			
    			lastDigit = i % 10;
    			if (i >= 4 && i <= 20)
    				os = "th";
    			if (i == 1 || lastDigit == 1)
    				os = "st";
    			else if (i == 2 || lastDigit == 2)
    				os = "nd";
    			else if (i == 3 || lastDigit == 3)
    				os = "rd";
    			else
    				os = "th";
    			
    
    			printf("Enter your %d%s number: ", i, os);
    			scanf("%d", &inputs[i - 1]);
    		}
    
    		a = inputs[0];
    		b = inputs[1];
    		c = inputs[2];
    
    		while (1)
    		{
    			if (a == 0)
    			{
    				if (b == 0)
    				{
    					printf(noSol);
    					break;
    				}
    				else
    				{
    					x = -c / b;
    					printf("The equation is not quadratic and the solution is %d\n", x);
    					break;
    				}
    			}
    			else
    			{
    				d = pow(b, 2) - 4 * a * c;
    				if (d < 0)
    				{
    					printf(noSol);
    					break;
    				}
    				else if (d == 0)
    				{
    					x1 = -b / 2 * a;
    					printf("One solution: %d\n", x1);
    					break;
    				}
    				else 
    				{
    					x1 = (-b + sqrt(d)) / 2 * a;
    					x2 = (-b - sqrt(d)) / 2 * a;
    					printf("Two solutions: %d and %d\n", x1, x2);
    					break;
    				}
    			}
    		}
    
    		printf("Would you like to run the program again? ( Y / N )\n");
    		scanf("%s", &cont);
    	}
    	system("pause");
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What might be the problem?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    I think I see the problem, but no idea if what I see is what you are getting. It helps to say what the problem is.

    But use a debugger, GDB is free GDB: The GNU Project Debugger you can cycle line by line of your code and see if it is doing what you want.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Hmm, you have printf statements with no format string, you try to scanf into a char literal, you declared main incorrectly, there are multiple places where integer division could mess you up, ect. Maybe you could post a specific concern, however before you do that I suggest you re-read through whatever text book you are using. Here are some other good sources:
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed