-
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
-
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
-
Right here:
= 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 ;)
-
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
it does not work.. do you know how do i identify a null set?
-
There ain't no such a thing in C++. What are you trying to do?
-
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
-
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().
-
okie.. tyvvvvmm u saved my day