Thread: Please Help

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Please Help

    Code:
    #include "stdafx.h"
    #include <iostream>
     
    using namespace std;
    
    int main()
    {
        double num1, num2, num3;
        double temp;
    	char ad;
    
        cout << "Enter three numbers: ";
        cin >> num1 >> num2 >> num3;
        cout << endl;
    
    	cout << "Please choose if you want in ascending (a) or decending (d): ";
    	cin >> ad;
    	cout << endl;
    	
    
         
    
        //Now num1 is less than or equal to num2
    	if (ad = 'a'){
    	
    		if (num1 > num2)
        {
            temp = num1;
            num1 = num2;
            num2 = temp;
        }
    	cout << "The numbers in the ascending order are: ";
    
    		if (num3 <= num1)
    			cout << num3 << " " << num1 << " " << num2 << endl;
    		else if (num1 <= num3 && num3 <=  num2)
    			cout << num1 << " " << num3 << " " << num2 << endl;
    		else
    			cout << num1 << " " << num2 << " " << num3 << endl;
    	}
    	else if (ad = 'd'){
    		if (num1 < num2)
        {
            temp = num1;
            num1 = num2;
            num2 = temp;
        }
    	
    	cout << "The numbers in the decending order are: ";
    	
    		if (num3 >= num1)
    			cout << num3 << " " << num1 << " " << num2 << endl;
    		else if (num1 >= num3 && num3 >=  num2)
    			cout << num1 << " " << num3 << " " << num2 << endl;
    		else
    			cout << num1 << " " << num2 << " " << num3 << endl;
    	}
    
    	else{
    		cout << "You have entered an invalid option!";
    	}
    	system ("PAUSE");
    	return 0;
    }
    Above is the code which I have used. I know my logic for the descending part is wrong... but the problem is that now even if i choose option d for descending, it still displays ascending. please help

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    any1 can help please? need help urgent.. ok i checked the logic is correct to sort.. but can some1 tell me where i went wrong at the part to choose 'a' or 'd'? If i press d, it still shows ascending only

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Right here:
    Code:
    	if (ad = 'a'){
    = is the assignment operator, so this will always be true.
    == is a comparison operator.

    I used to make this mistake all the time. For a while I put a big

    ==

    in marker at the top of my keyboard
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Thank you sooooo much.. I have been sitting here for 2 hours trying to figure out why. Ok will keep that in mind. I should do the same as you too lol. Anyway, can you help me with 1 more problem. With that code above, if no value was entered. How do I coded it such that it will display and error message and repeat the question again.

    I tried
    Code:
    if (num1 == /0)
    it does not work.. do you know how do i identify a null set?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There ain't no such a thing in C++. What are you trying to do?

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    ic.. cos with my current code above, some1 can still enter a alphabet and the program will still return a number. I want an error message to come out instead of a number

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Do you understand the ASCII table?

    ASCII Table / Extended ASCII Codes

    This is how, eg, 'a' == 97. You can use that to test if an input is within an acceptable range and respond accordingly. However, you will have to use a different method of inputing data than just cin to a double, because that skips the step where you would do the verification.

    For example, you could cin to a string, and extract the numbers from the string. Possibly C io & string functions are better for this than C++, such a scanf(), which will reject input that does not match type. Best way is probably to cin a string of all the numbers, then use sscanf() (notice, two ss) on the string.c_str().
    Last edited by MK27; 03-28-2010 at 05:41 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    okie.. tyvvvvmm u saved my day

Popular pages Recent additions subscribe to a feed