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

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




need help with 2 simple programs learning while and for loops

 
Reply to this topicStart new topic

need help with 2 simple programs learning while and for loops, im just learning c++ so these should be easy :)

mostlyhuman
11 Apr, 2008 - 01:38 PM
Post #1

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 3

1st problem: This program was intended to validate that the user entered a 1 or a 2, whenever i run the program the loop will not end even if i enter 1 or 2 and i dont understand why.

CODE
int main(int argc, char *argv[])
{
    int number=0;
    
    while (number != 1 || number != 2)
    {
          cout << "Enter a 1 or a 2: ";
          cin >> number;
    }
    
    cout << "Yay you entered a 1 or a 2!!" << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


2nd problem: This program was intended to have the user enter 10 numbers and then display the highest and second highest numbers, the highest number shows correctly but the second number is always zero and i dont understand why.

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;

}



Thanks in advance for any help!

This post has been edited by mostlyhuman: 11 Apr, 2008 - 01:41 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Need Help With 2 Simple Programs Learning While And For Loops
11 Apr, 2008 - 02:39 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Your first problem is because of your while statement condition here...

cpp

while (number != 1 || number != 2)


This is saying if the number is not 1 OR the number is not 2. If you type in 1, then number != 2 right? So the whole statement becomes true. If you type in 2, then number != 1 right? So the whole thing is true. I think you mean to use And which is &&. If number != 1 and number != 2, continue the loop.

Your second problem doesn't always show zero, it just doesn't correctly record the second highest number. Of course this may depend on the input you give it why it is showing zero, but what you need to do is when you go to set the largest number the first time, also set the value of next to the same number. So after your first number both largest and next will be the same value.

This makes sense doesn't it? Your highest and second highest values are the same values because you have only put in one number. Now ideally what you could have done is that whenever you find a value that is higher than the largest put the largest into next first and then set largest to the new number.

But either way works.

Enjoy!

"At DIC we be the largest and next largest code ninjas... now just how large we are not going to say, but lets just say we could be on the show 'The biggest loser'." decap.gif
User is offlineProfile CardPM
+Quote Post

mostlyhuman
RE: Need Help With 2 Simple Programs Learning While And For Loops
11 Apr, 2008 - 02:51 PM
Post #3

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 3

CODE
while (number != 1 || number != 2)


Okay cool i get this now, its evaluating to true either way.

On the other problem although there is a more coherent way to write the program, i still don't understand why the way i wrote it doesnt work.

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

          next = number;


I was trying to say, if the number is smaller than the current largest number, AND if the number is greater than the current next number, then assign number to next. Thanks again for any clarification smile.gif

This post has been edited by mostlyhuman: 11 Apr, 2008 - 03:09 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:28PM

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