| | |
Vector input
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 12
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
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
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.
•
•
Join Date: Feb 2008
Posts: 12
Reputation:
Solved Threads: 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
this results in me typing in 2 and the program terminating
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)
for (int i = 0; i <= list.size(); i++) cout << list[i]
this results in me typing in 2 and the program terminating
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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
ie < not <= .
You can debug it by putting breakpoints and walking through your code.
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.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
yes it prints out the vector. here is my stdout output
Here is the code I have
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)
#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; }
Last edited by stilllearning; Sep 29th, 2008 at 11:22 pm.
•
•
Join Date: Feb 2008
Posts: 12
Reputation:
Solved Threads: 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
I hate to sound dumb but do you have to initialize the vector, with "using std::vector;"
thank you
C++ Syntax (Toggle Plain Text)
#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
![]() |
Similar Threads
- open ended vector input (C++)
- Help with calculating avrg from input file. (C++)
- Need help starting program using vector (C++)
- Help with input (C)
- Text File Input and manipulation (C++)
Other Threads in the C++ Forum
- Previous Thread: Matrix help
- Next Thread: .find in hashmap
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






