Thread: it still dont work

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    it still dont work

    I wrote this program and it seems to work but I found out its only comparing the first two numbers. I need it to compare all five...what am I doing wrong now..I know its something with the if (A > E) statement.

    Bryan

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	
    	float A;
    	float B;
    	float C;
    	float D;
    	float E;
    	
    
    	cout << "Enter 1st Interger then press enter:" << endl;
    	cin >> A;
    	cout << "Enter 2nd Interger then press enter:" << endl;
    	cin >> B;
    	cout << "Enter 3rd Interger then press enter:" << endl;
    	cin >> C;
    	cout << "Enter 4th Interger then press enter:" << endl;
    	cin >> D;
    	cout << "Enter 5th Interger then press enter:" << endl;
    	cin >> E;
    
    
    	
    	if (A < E)
    	
    		cout << "Ascending Order" << endl;
    	
    	else if  (A > E)
    	
    		cout << "Descending Order" << endl;
    	 
    	
    	
    	else
    	{
    		cout << "Not in any order at all" << endl;
    	}
    return 0;
    }

  2. #2
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    If you don't put brackets after your if/else statements, it will only execute what is on the next line. Try either adding the brackets, or remove the blank line inbetween. Also, your tests won't prove to an absolute certainty that they are ascending or descending

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by HaLCy0n
    If you don't put brackets after your if/else statements, it will only execute what is on the next line.
    No. It only executes the next statement, which could be preceded with 50 lines of blank space, or no blank space.

    A float is not an integer.

    The problem with your program is that you assume everything is in order. If you want to check decending, make sure that A > B, B > C, C > D, and D > E.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Your spelling and grammar are atrocious.

    It'd be far more useful in the future to write a function that accepts an array or other container and determines if a list is in order. You could also use a loop to fill up that list of data. If you do those two things, you'd then have yourself some code that is versatile and reusable. In general while programming, try to think in broad terms. Instead of just finding out if a list of five integers is in order, you have the power to determine if a list of any size containing elements of any data type are in order. Whilst programming, think big.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Give him/her a break JoshDick. It's bad to aim too high when beginning a programming language, it's condusive to crashing and burning. Small successes should be sought first, then you can think about making versatile and useful programs. And what is so bad about the spelling and grammar? I saw only one spelling mistake, "Interger", and his grammar is the same as, if not better than, that of most posters in this forum.

    ygfperson's post was completely adequate here, but yours served only to confuse romeoz.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    well I changed it around and it works sometimes. I can get the ascending adn descending to work everytime but when I mix the numbers up it will show the not in any order statement but if I do it again it will just say press any key...am I doing something wrong?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	
    	float A;
    	float B;
    	float C;
    	float D;
    	float E;
    	
    
    	cout << "Enter 1st Interger then press enter:" << endl;
    	cin >> A;
    	cout << "Enter 2nd Interger then press enter:" << endl;
    	cin >> B;
    	cout << "Enter 3rd Interger then press enter:" << endl;
    	cin >> C;
    	cout << "Enter 4th Interger then press enter:" << endl;
    	cin >> D;
    	cout << "Enter 5th Interger then press enter:" << endl;
    	cin >> E;
    
    
    	
    	if (A < B)
    		if (B < C)
    			if (C < D)
    				if (D < E)
    	
    		cout << "Ascending Order" << endl;
    				
    	if (A > B)
    		if (B > C)
    			if (C > D)
    				if (D > E)
    			
    				
    		cout << "Descending Order" << endl;
    	 
    	
    	else 
    	{
    		cout << "Not in any order at all" << endl;
    	}
    return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Because you don't have brackets around your if statements, the else pairs with the last if it sees. E.g. the red text is an if-else pair.

    Code:
    	if (A < B)
    		if (B < C)
    			if (C < D)
    				if (D < E)
    	
    		cout << "Ascending Order" << endl;
    				
    	if (A > B)
    		if (B > C)
    			if (C > D)
    				if (D > E)
    			
    				
    		cout << "Descending Order" << endl;
    	 
    	
    	else 
    	{
    		cout << "Not in any order at all" << endl;
    	}
    Even adding brackets won't fix it truly, though. You want only three possible code paths; it's either ascending, descending, or neither. This means you should have an if/else if/else setup, and you don't want to nest the if statements.


    Code:
    	if (A < B && B < C && C < D && D < E)
    		cout << "Ascending Order" << endl;
    	else if (A > B && B > C && C > D && D > E)
    		cout << "Descending Order" << endl;
    	else
    		cout << "Not in any order at all" << endl;
    Last edited by Cat; 07-07-2003 at 03:47 PM.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    so let me get this straight...It worked because you compared all variables to each other...by adding the && signs after each one....that is what I just read in my book...I appreciate your help on this. thanks alot.. it works great now.

    Bryan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM