Vector input

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 12
Reputation: slayman89 is an unknown quantity at this point 
Solved Threads: 0
slayman89 slayman89 is offline Offline
Newbie Poster

Vector input

 
0
  #1
Sep 29th, 2008
  1. int main()
  2. {
  3. vector<double> list;
  4. bool more = true;
  5. while (more)
  6. {
  7. double x;
  8. int yes;
  9. cout << "Do you have numbers? yes=1 or no=2" "\n";
  10. cin >> yes;
  11. if(yes == 1)
  12. {
  13. cout << "Enter number""\n";
  14. cin >> x;
  15. list.push_back(x);
  16. yes = 0;
  17. }
  18. if (yes == 2)
  19. more = false;
  20. }

so far as i can tell this compiles fine, but once i have added the desired number of items for the vector and punch in "2" the program starts and does not go on to the next part after the while loop. I have commented out everything else outside of this loop but i cannot see the break.
Any help would be great
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
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: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Vector input

 
0
  #2
Sep 29th, 2008
What you posted looks ok, so the problem is after that while loop. If its prompting for more user input, such as a string, then you have to clear the input stream of the '\n' that's still in the input buffer from inputting an integer. See this thread for how to do that.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: slayman89 is an unknown quantity at this point 
Solved Threads: 0
slayman89 slayman89 is offline Offline
Newbie Poster

Re: Vector input

 
0
  #3
Sep 29th, 2008
it does not ask for anything.

After the loop i have been asking for it to print the vector out just for testing purposes by using
  1. for (int i = 0; i <= list.size(); i++)
  2. cout << list[i]

this results in me typing in 2 and the program terminating
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Vector input

 
0
  #4
Sep 29th, 2008
I ran your code and it works fine.

I would suggest a couple of things. "list" is a reserved STL container so you may want to call your variable something else.

And your loop should be
for (int i = 0; i < list.size(); i++)
ie < not <= .

You can debug it by putting breakpoints and walking through your code.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: slayman89 is an unknown quantity at this point 
Solved Threads: 0
slayman89 slayman89 is offline Offline
Newbie Poster

Re: Vector input

 
0
  #5
Sep 29th, 2008
when it finishes does it print out the vector because i can input but i cannot get any output
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Vector input

 
0
  #6
Sep 29th, 2008
yes it prints out the vector. here is my stdout output

Do you have numbers? yes=1 or no=2
1
Enter number
10.1234
Do you have numbers? yes=1 or no=2
1
Enter number
20.3456
Do you have numbers? yes=1 or no=2
2
Printing out vector values
10.1234
20.3456

Here is the code I have
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. using std::vector;
  8.  
  9. int main()
  10. {
  11. vector<double> myVector;
  12. bool more = true;
  13. while (more)
  14. {
  15. double x;
  16. int yes;
  17. cout << "Do you have numbers? yes=1 or no=2" "\n";
  18. cin >> yes;
  19. if(yes == 1)
  20. {
  21. cout << "Enter number""\n";
  22. cin >> x;
  23. myVector.push_back(x);
  24. yes = 0;
  25. }
  26. if (yes == 2)
  27. more = false;
  28. }
  29.  
  30. cout << "Printing out vector values " << endl;
  31. for (unsigned int i = 0; i < myVector.size(); i++)
  32. cout << myVector[i] << endl;
  33.  
  34. return 0;
  35. }
Last edited by stilllearning; Sep 29th, 2008 at 11:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: slayman89 is an unknown quantity at this point 
Solved Threads: 0
slayman89 slayman89 is offline Offline
Newbie Poster

Re: Vector input

 
0
  #7
Sep 29th, 2008
so i am new to this and my teacher isn't the greatest (this wasn't hmwk this was me trying to get the concept), but i was using
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4.  
  5. using namespace std;

I hate to sound dumb but do you have to initialize the vector, with "using std::vector;"

thank you
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Vector input

 
0
  #8
Sep 29th, 2008
No you don't.

"using namespace std; " is fine, and it will initialize everything you need.

Its just I prefer not including the entire namespace if I don't need it. Sorry to have confused you.
Last edited by stilllearning; Sep 29th, 2008 at 11:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: slayman89 is an unknown quantity at this point 
Solved Threads: 0
slayman89 slayman89 is offline Offline
Newbie Poster

Re: Vector input

 
0
  #9
Sep 29th, 2008
ahh no problem thanks alot
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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