Need help with an array problem

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

Join Date: Apr 2006
Posts: 3
Reputation: avalloc is an unknown quantity at this point 
Solved Threads: 0
avalloc avalloc is offline Offline
Newbie Poster

Need help with an array problem

 
0
  #1
Apr 27th, 2006
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):



  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need help with an array problem

 
0
  #2
Apr 27th, 2006
>> 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
  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]
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