Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,080 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,578 people online right now. Registration is fast and FREE... Join Now!




beginner help with relational and logical operators

 
Reply to this topicStart new topic

beginner help with relational and logical operators

mostlyhuman
14 Apr, 2008 - 05:35 AM
Post #1

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 3

I understand now that there is a more efficient way to write this program, but although my way is a bit convoluted i still dont understand why it doesnt work. The program is meant for the user to type 10 numbers and then it will display the highest number and then the second highest number. If you enter numbers 1-10 in sequence for the 10 inputs, the highest number will display correctly as 10, but the second highest number will read as zero. I dont understand why this statement isnt working. I was trying to say if the number is greater than the current largest number then assign this as the new largest number otherwise if the number is less than the current largest number AND greater than the current second largest, then assign the number to the second number. Seems like it should work but the statement below always produces a zero for the second number. Thanks in advance for any help!

(this is the code fragment i am having issues with, the full code is below)

CODE
if (number > largest)

          largest = number;

                  else                  

          if (number < largest && number > next)

          next = number;


(Full Code)

CODE
int main(int argc, char *argv[])

{

    int counter=1,number=0,next=0,largest=0;

    

    while (counter < 11)

    {

          cout << "Enter number (" << counter << " of 10): ";

          cin >> number;

          

          if (number > largest)

          largest = number;

                  else                  

          if (number < largest && number > next)

          next = number;

                    

          counter++;

    }

    cout << "The largest number is: " << largest << endl;

    cout << "The second largest number is: " << next << endl;    

    

    system("PAUSE");

    return EXIT_SUCCESS;

}

User is offlineProfile CardPM
+Quote Post

KYA
RE: Beginner Help With Relational And Logical Operators
14 Apr, 2008 - 05:42 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,784



Thanked: 91 times
Dream Kudos: 1200
My Contributions
try:

cpp

if ((number < largest) && (number > next))

User is online!Profile CardPM
+Quote Post

baavgai
RE: Beginner Help With Relational And Logical Operators
14 Apr, 2008 - 06:05 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Your logic is fine. However, theres a gotcha here. Depending on the order in which you enter the numbers there's a chance you'll throw next away.

Here's the code with some comments:
cpp

int main(int argc, char *argv[]) {
int next=0,largest=0;
for(int counter=1; counter < 11; counter++) {
int number;
cout << "Enter number (" << counter << " of 10): ";
cin >> number;

if (number > largest) {
// it's the biggest!
// which means, the current bigest is the next biggest.
next = largest;
largest = number;
} else if (number > next) {
// we already know it's not the biggest
//but if even bigger than next
next = number;
}
}
cout << "The largest number is: " << largest << endl;
cout << "The second largest number is: " << next << endl;
system("PAUSE");
return EXIT_SUCCESS;
}


Hope this helps.

User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:35PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month