0
int main()
{
vector<double> list;
bool more = true;
while (more)
{
  double x;
  int yes;
  cout << "Do you have numbers? yes=1 or no=2" "\n";
  cin >> yes;
  if(yes == 1)  
  {
   cout << "Enter number""\n";
   cin >> x;
   list.push_back(x);
   yes = 0;
   }
 if (yes == 2)
    more = false;
}

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

3
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by slayman89
0

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.

0

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

for (int i = 0; i <= list.size(); i++)
  cout << list[i]

this results in me typing in 2 and the program terminating

0

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.

0

when it finishes does it print out the vector because i can input but i cannot get any output

0

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

#include <iostream>
#include <vector> 

using std::cin;
using std::cout;
using std::endl;
using std::vector;

int main()
{
  vector<double> myVector;
  bool more = true;
  while (more)
  {
    double x;
    int yes;
    cout << "Do you have numbers? yes=1 or no=2" "\n";
    cin >> yes;
    if(yes == 1)  
    {
      cout << "Enter number""\n";
      cin >> x;
      myVector.push_back(x);
      yes = 0;
    }
    if (yes == 2)
      more = false;
  }

  cout << "Printing out vector values " << endl; 
  for (unsigned int i = 0; i < myVector.size(); i++)
    cout << myVector[i] << endl;

  return 0;
}
0

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

#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

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

thank you

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.