944,092 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 870
  • C++ RSS
Apr 27th, 2006
0

Need help with an array problem

Expand Post »
I am working on a C++ program where you enter 6 integers and then use an array to store them. It then asks you to enter another integer and then reports if the last one is in the array. I have been working on it a while and I can't figure it out. I would greatly appreciate if anyone helped me figure this one out. This is what I have so far(its wrong, because when I run it, it goes into an infinite loop and ignores the yes or no answer for the first question of the program):



C++ Syntax (Toggle Plain Text)
  1. //Integer Reader
  2.  
  3. #include <iostream.h>
  4. #include <cstring.h>
  5.  
  6. int main ()
  7. {
  8.  
  9. float intarray[5];
  10. int num1, num2, num3, num4, num5, sub;
  11. sub=0;
  12. string reply, reply1, reply2, reply3, reply4;
  13.  
  14. cout<<"Do you want to start the program? Type yes or no and press enter."<<endl;
  15. cin>>reply;
  16.  
  17. while (reply,reply1,reply2,reply3,reply4!= "no")
  18.  
  19. {
  20. cout<<"Do you want to enter a number?"<<endl;
  21. cin>>reply>>num1;
  22.  
  23. cout<<"Do want to enter another number?"<<endl;
  24. cin>>reply1>>num2;
  25.  
  26. cout<<"Do want to enter another number?"<<endl;
  27. cin>>reply2>>num3;
  28.  
  29. cout<<"Do want to enter another number?"<<endl;
  30. cin>>reply3>>num4;
  31.  
  32. cout<<"Do want to enter another number?"<<endl;
  33. cin>>reply4>>num5;
  34. }
  35.  
  36.  
  37.  
  38. while (sub < 5)
  39. { cin>>intarray[sub];
  40. sub=sub+1;
  41. if (sub==5)
  42. cout<<"Bye"<<endl;
  43. else
  44. cout<<"Enter another integer."<<endl;
  45. }
  46.  
  47.  
  48. return 0;
  49. }
Last edited by Dave Sinkula; Apr 28th, 2006 at 12:09 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avalloc is offline Offline
3 posts
since Apr 2006
Apr 27th, 2006
0

Re: Need help with an array problem

>> while (reply,reply1,reply2,reply3,reply4!= "no")

that is not necessary. just create a for loop that counts from 0 to 5 -- no need to answer that question 5 times
C++ Syntax (Toggle Plain Text)
  1. for(int count = 0; count < 5; count++)
  2. {
  3. // blabla
  4. }

there is no need for num1, num2 ... num5 either. just enter the numbers directly into the array. add this to the above loop

cin >> intarray[i];

intarray needs to be declared as an array of 6 integers, not 5

int intarray[6]
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please Help C++ Connect due tonight by 12
Next Thread in C++ Forum Timeline: Lowest Common Denominator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC