plz tell me wherez my mistake in dis program!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 9
Reputation: Rose Aashii is an unknown quantity at this point 
Solved Threads: 0
Rose Aashii Rose Aashii is offline Offline
Newbie Poster

plz tell me wherez my mistake in dis program!!

 
0
  #1
Apr 16th, 2005
below is my code the whole program is for prompting user to enter 10 nos. b/w 20 to 100 it will while is there to check wheter the user entr the corect number or not, it will also check that the entered number is unique and was not previously entered by the user. at end of program simply it will display the non dublicate nos. whole program is correct but at end it is not displaying correct result. plz check the code n tell me wherz my mistake.
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<iostream.h>
  4. main()
  5. {
  6. int nTmp,j;
  7. //declares variable bflag th be of type bool & intializes bflag to false
  8. //bool is a data type whose value may be false or true
  9. bool bFlag=false;
  10. int a[10];
  11. cout<<"entr 10 numbers b/w 20 and 100"<<endl;
  12. for(int i=0;i<10;i++)
  13. {
  14. cout<<"Enter Value"<<endl;
  15. cin>>nTmp;
  16. while(!bFlag)
  17. {
  18. ////Here we check first Condition
  19. // no between 20 and 100
  20. while((nTmp<20)||(nTmp>100))
  21. {
  22. cout<<"enter no b/w 20 and 100"<<endl;
  23. cin>>nTmp;
  24. }
  25. j=0;
  26. while(j<=i)
  27. {
  28. if(a[j++]==nTmp)
  29. {
  30. bFlag=false;
  31.  
  32. }
  33. bFlag=true;
  34. }
  35. if(bFlag==true)
  36. {
  37. //save variable
  38. a[i]=nTmp;
  39. }
  40. }
  41. //again make it false
  42. bFlag=false;
  43. }
  44. cout<<" the non dublicate nos are "<<a[i]<<endl;
  45. getch();
  46. }
Code formatted and tags added. -Narue

Rose
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 9
Reputation: Rose Aashii is an unknown quantity at this point 
Solved Threads: 0
Rose Aashii Rose Aashii is offline Offline
Newbie Poster

Re: plz tell me wherez my mistake in dis program!!

 
0
  #2
Apr 16th, 2005
my prob is that i need to print only dat numbers that r not repeated.like if i entered 50, 60, 80, 90, 99, 56, 50,98, 45, 99 so here 50 and 99 are reapeating so in the output ony 8 nos that r 50, 60, 80, 90, 99, 56, 98, 45 such that the nos that r repeating should not be displayed as u observe it in output.this is not diaplyind the ouput which i want.

for(i=0;i<10;i++)
cout<<" the non dublicate nos are "<<a[i]<<endl;
rose
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 745
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: plz tell me wherez my mistake in dis program!!

 
0
  #3
Apr 16th, 2005
>whole program is correct but at end it is not displaying correct result.
Then the whole program is not correct. :rolleyes:

>#include<stdio.h>
This isn't needed.

>main()
This is illegal C++. Implicit int is not allowed, so you need to say:
  1. int main()
>for(int i=0;i<10;i++)
This is not portable because you access i after the loop. i should be declared outside of the loop to be correct.

Compare this with what you have:
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int nTmp,i,j;
  8. bool bFlag=false;
  9. int a[10];
  10. cout<<"entr 10 numbers b/w 20 and 100"<<endl;
  11. for(i=0;i<10;i++)
  12. {
  13. while(!bFlag)
  14. {
  15. cout<<"Enter Value"<<endl;
  16. cin>>nTmp;
  17. ////Here we check first Condition
  18. // no between 20 and 100
  19. while((nTmp<20)||(nTmp>100))
  20. {
  21. cout<<"enter no b/w 20 and 100"<<endl;
  22. cin>>nTmp;
  23. }
  24. for(j=0;j<i;j++)
  25. {
  26. if(a[j]==nTmp)
  27. {
  28. cout<<nTmp<<" already exists"<<endl;
  29. break;
  30. }
  31. }
  32. if(j==i)
  33. {
  34. //save variable
  35. a[i]=nTmp;
  36. bFlag=true;
  37. }
  38. }
  39. //again make it false
  40. bFlag=false;
  41. }
  42.  
  43. cout<<" the non dublicate nos are "<<endl;
  44. for(i=0;i<10;i++)
  45. cout<<a[i]<<endl;
  46. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC