943,769 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1448
  • C++ RSS
Sep 29th, 2008
0

Vector input

Expand Post »
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slayman89 is offline Offline
12 posts
since Feb 2008
Sep 29th, 2008
0

Re: Vector input

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 29th, 2008
0

Re: Vector input

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
C++ Syntax (Toggle Plain Text)
  1. for (int i = 0; i <= list.size(); i++)
  2. cout << list[i]

this results in me typing in 2 and the program terminating
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slayman89 is offline Offline
12 posts
since Feb 2008
Sep 29th, 2008
0

Re: Vector input

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 29th, 2008
0

Re: Vector input

when it finishes does it print out the vector because i can input but i cannot get any output
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slayman89 is offline Offline
12 posts
since Feb 2008
Sep 29th, 2008
0

Re: Vector input

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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 29th, 2008
0

Re: Vector input

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
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slayman89 is offline Offline
12 posts
since Feb 2008
Sep 29th, 2008
0

Re: Vector input

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 29th, 2008
0

Re: Vector input

ahh no problem thanks alot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slayman89 is offline Offline
12 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Matrix help
Next Thread in C++ Forum Timeline: .find in hashmap





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


Follow us on Twitter


© 2011 DaniWeb® LLC