Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Let me know my mistake in arrays program!!

Let me know my mistake in arrays program!!

Scheduled Pinned Locked Moved C / C++ / MFC
help
13 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U User 1878431

    hye!!i need to know what's wrong with this program coding..it will prompt the user to entr 10 numbers b/w 20 and 100 for this i use if structure...here if structure is used for checking whether the user enter the correct number or not. i hav lil problem that it gives the corect result if the user enter incorrect number in start but if in the middle of the program the user enter incorrect number like 12 or any incorrect no. it deals it as a correct number. plz let me know where is my mistake.one more thing i need to know plz tell me which statement i will use to check that the entered number is unique and was not previously entered by the user. Coding is below.. #include #include main() { int a[10],i; cout<<"entr 10 numbers b/w 20 and 100"<>a[i]; if((a[i]<20)||(a[i]>100)) cout<<"entr no b/w 20 and 100"<>a[i]; } getch(); } rose

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #2

    The first problem is that you are incrementing the variable i even if the number entered is out of range. The other problem is that you have a second, unnecessary call to cin.


    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

    1 Reply Last reply
    0
    • U User 1878431

      hye!!i need to know what's wrong with this program coding..it will prompt the user to entr 10 numbers b/w 20 and 100 for this i use if structure...here if structure is used for checking whether the user enter the correct number or not. i hav lil problem that it gives the corect result if the user enter incorrect number in start but if in the middle of the program the user enter incorrect number like 12 or any incorrect no. it deals it as a correct number. plz let me know where is my mistake.one more thing i need to know plz tell me which statement i will use to check that the entered number is unique and was not previously entered by the user. Coding is below.. #include #include main() { int a[10],i; cout<<"entr 10 numbers b/w 20 and 100"<>a[i]; if((a[i]<20)||(a[i]>100)) cout<<"entr no b/w 20 and 100"<>a[i]; } getch(); } rose

      N Offline
      N Offline
      namaskaaram
      wrote on last edited by
      #3

      the code that u have written..... only checkz for the validity of th enumber the first time(coz u have used 'if' construct! replace it by 'while'!!!! it should be.....

      for(i=0;i<10;i++)
      {
      cin>>a[i];

      while((a[i]<20)||(a[i]>100))
      {
      cout<<"entr no b/w 20 and 100"<>a[i];
      }

      }

      to check if the number was entered or not before.....u have to check the array,then accepet or reject accordingly..... i hope thiz aint ur homework!!!!!..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

      M 1 Reply Last reply
      0
      • N namaskaaram

        the code that u have written..... only checkz for the validity of th enumber the first time(coz u have used 'if' construct! replace it by 'while'!!!! it should be.....

        for(i=0;i<10;i++)
        {
        cin>>a[i];

        while((a[i]<20)||(a[i]>100))
        {
        cout<<"entr no b/w 20 and 100"<>a[i];
        }

        }

        to check if the number was entered or not before.....u have to check the array,then accepet or reject accordingly..... i hope thiz aint ur homework!!!!!..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #4

        How can a number be less than 20 and greater than 100 at the same time? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD

        D N 2 Replies Last reply
        0
        • M Michael Dunn

          How can a number be less than 20 and greater than 100 at the same time? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #5

          Michael Dunn wrote: How can a number be less than 20 and greater than 100 at the same time? Only those numbers that have crossed over to the dark side. :cool:


          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          M 1 Reply Last reply
          0
          • D David Crow

            Michael Dunn wrote: How can a number be less than 20 and greater than 100 at the same time? Only those numbers that have crossed over to the dark side. :cool:


            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #6

            :laugh:


            Maximilien Lincourt Your Head A Splode - Strong Bad

            1 Reply Last reply
            0
            • U User 1878431

              hye!!i need to know what's wrong with this program coding..it will prompt the user to entr 10 numbers b/w 20 and 100 for this i use if structure...here if structure is used for checking whether the user enter the correct number or not. i hav lil problem that it gives the corect result if the user enter incorrect number in start but if in the middle of the program the user enter incorrect number like 12 or any incorrect no. it deals it as a correct number. plz let me know where is my mistake.one more thing i need to know plz tell me which statement i will use to check that the entered number is unique and was not previously entered by the user. Coding is below.. #include #include main() { int a[10],i; cout<<"entr 10 numbers b/w 20 and 100"<>a[i]; if((a[i]<20)||(a[i]>100)) cout<<"entr no b/w 20 and 100"<>a[i]; } getch(); } rose

              B Offline
              B Offline
              Bob Flynn
              wrote on last edited by
              #7

              Your loop structure is wrong. You should use a while loop to count the number of correct numbers (unique numbers between 20 and 100). input your candidate value while the number of unique numbers between 20 and 100 is less than 10 do the following if candidate value is valid then assign it to your array and incremment your valid numbers count get the next candidate value. to check the validity of your candidate value you have to do the range check as you already figured out, and then you have to search through your array (up to the current number of valid values) for any values that are equal. If you find a match then your value is not valid. This should give you some help. But as stated, your assignments will not be done for you. Try to put this description into code. And let me know if you have further trouble. Do it quick I probably will not be online much longer. Its the weekend and I have house work to do :(

              D 1 Reply Last reply
              0
              • B Bob Flynn

                Your loop structure is wrong. You should use a while loop to count the number of correct numbers (unique numbers between 20 and 100). input your candidate value while the number of unique numbers between 20 and 100 is less than 10 do the following if candidate value is valid then assign it to your array and incremment your valid numbers count get the next candidate value. to check the validity of your candidate value you have to do the range check as you already figured out, and then you have to search through your array (up to the current number of valid values) for any values that are equal. If you find a match then your value is not valid. This should give you some help. But as stated, your assignments will not be done for you. Try to put this description into code. And let me know if you have further trouble. Do it quick I probably will not be online much longer. Its the weekend and I have house work to do :(

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #8

                Bob Flynn wrote: Its the weekend and I have house work to do Are your weeds screaming at you too?


                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                B 1 Reply Last reply
                0
                • D David Crow

                  Bob Flynn wrote: Its the weekend and I have house work to do Are your weeds screaming at you too?


                  "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                  B Offline
                  B Offline
                  Bob Flynn
                  wrote on last edited by
                  #9

                  Yea, but not as loudly as my wife is to cut them.:doh:

                  1 Reply Last reply
                  0
                  • M Michael Dunn

                    How can a number be less than 20 and greater than 100 at the same time? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD

                    N Offline
                    N Offline
                    namaskaaram
                    wrote on last edited by
                    #10

                    oopz.....i just seemed to have just noticed the "&&"..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

                    1 Reply Last reply
                    0
                    • U User 1878431

                      hye!!i need to know what's wrong with this program coding..it will prompt the user to entr 10 numbers b/w 20 and 100 for this i use if structure...here if structure is used for checking whether the user enter the correct number or not. i hav lil problem that it gives the corect result if the user enter incorrect number in start but if in the middle of the program the user enter incorrect number like 12 or any incorrect no. it deals it as a correct number. plz let me know where is my mistake.one more thing i need to know plz tell me which statement i will use to check that the entered number is unique and was not previously entered by the user. Coding is below.. #include #include main() { int a[10],i; cout<<"entr 10 numbers b/w 20 and 100"<>a[i]; if((a[i]<20)||(a[i]>100)) cout<<"entr no b/w 20 and 100"<>a[i]; } getch(); } rose

                      T Offline
                      T Offline
                      ThatsAlok
                      wrote on last edited by
                      #11

                      In Continution with Mr Rateep Code :- for checking entry of Unique Number

                      int nTmp,j;
                      bool bFlag=false;
                      int a\[10\];
                      
                      for(int i=0;i<10;i++)
                      {
                      	cin>>nTmp;
                      	
                      	while(!bFlag)
                      	{
                      		////Here we check first Condition
                      		// no between 20 and 100
                      	while((nTmp<20)||(nTmp>100))
                      	{
                      cout<<"enter no b/w 20 and 100"<\>nTmp;
                      	}
                      		
                        j=0;
                       while(j<=i) 
                       {
                        if(a\[j++\]==nTmp)
                         {
                         bFlag=false;
                         //again ask for unique number
                          cout<<"You already entered this Number!enter unique no "<\>nTmp;
                         break;
                      }
                      
                      bFlag=true;
                      

                      }

                      if(bFlag==true)
                      {
                      //save variable
                      a[i]=nTmp;
                      }

                      }
                      //again make it false
                      bFlag=false;
                      }

                      cheers, Alok Gupta

                      N 1 Reply Last reply
                      0
                      • T ThatsAlok

                        In Continution with Mr Rateep Code :- for checking entry of Unique Number

                        int nTmp,j;
                        bool bFlag=false;
                        int a\[10\];
                        
                        for(int i=0;i<10;i++)
                        {
                        	cin>>nTmp;
                        	
                        	while(!bFlag)
                        	{
                        		////Here we check first Condition
                        		// no between 20 and 100
                        	while((nTmp<20)||(nTmp>100))
                        	{
                        cout<<"enter no b/w 20 and 100"<\>nTmp;
                        	}
                        		
                          j=0;
                         while(j<=i) 
                         {
                          if(a\[j++\]==nTmp)
                           {
                           bFlag=false;
                           //again ask for unique number
                            cout<<"You already entered this Number!enter unique no "<\>nTmp;
                           break;
                        }
                        
                        bFlag=true;
                        

                        }

                        if(bFlag==true)
                        {
                        //save variable
                        a[i]=nTmp;
                        }

                        }
                        //again make it false
                        bFlag=false;
                        }

                        cheers, Alok Gupta

                        N Offline
                        N Offline
                        ng kok chuan
                        wrote on last edited by
                        #12

                        gack... that's a hell of a long code just to check for unique numbers.. if all u're interested in is getting is getting unique numbers, why not use STL to do your work for u? look at this link: http://www.cppreference.com/cppset/[^] using a set stl, it automatically keeps only the unique numbers during an insertation. and by using the pair insert( const TYPE& val ); esp, the bool, u can check if the insertation took place. for the lazy coder (that's me), my insert statement is: if (!(mySet.insert(myVal)).second) // so i don't have to use declare a pair :) cout<<"insertation has failed as it is duplicate."<

                        T 1 Reply Last reply
                        0
                        • N ng kok chuan

                          gack... that's a hell of a long code just to check for unique numbers.. if all u're interested in is getting is getting unique numbers, why not use STL to do your work for u? look at this link: http://www.cppreference.com/cppset/[^] using a set stl, it automatically keeps only the unique numbers during an insertation. and by using the pair insert( const TYPE& val ); esp, the bool, u can check if the insertation took place. for the lazy coder (that's me), my insert statement is: if (!(mySet.insert(myVal)).second) // so i don't have to use declare a pair :) cout<<"insertation has failed as it is duplicate."<

                          T Offline
                          T Offline
                          ThatsAlok
                          wrote on last edited by
                          #13

                          ng kok chuan wrote: .. if all u're interested in is getting is getting unique numbers, why not use STL to do your work for u? You are correct Buddy!,but owner of this problem seems to be beginner in C++ as you see She getting difficulty in running Simple loop so we can't expect her to program using STL. Anyways i willtry to leran from this link! i too very weak in STL :sigh: cheers, Alok Gupta

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups