Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

c++ - Reading line of integers into a vector

Please have a look at the following code

int main()
{
    cout << "Enter numbers (-1 to stop entering; -2 to exit)" << endl;

    while(cin>>enterNumber)
    {
     numbers.push_back(enterNumber);
    }


    for(size_t size=0;size<numbers.size();size++)
    {
        cout << numbers[size] << endl;
    }
}

What I am trying to do here is something like this

  1. Type list of numbers (ex: 1 2 3 4 5 6 7 8 9 0 11)
  2. Read all of them into the vector
  3. Print them

In here, when I hit enter, nothing is happening! Seems like the loops didn't exit. How to print the values when I hit the enter?

UPDATE

I edited the code as advises given in answers.

int main()
{
    cout << "Enter numbers (-1 to stop entering; -2 to exit)" << endl;

    std::string line;
getline(std::cin, line);
std::istringstream iss(line);
while (iss >> enterNumber)
{
    numbers.push_back(enterNumber);
}


    for(size_t size=0;size<numbers.size();size++)
    {
        cout << numbers[size] << endl;
    }
}

but it gives another error now

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/excersice_6.exe
make[2]: Entering directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
mkdir -p build/Debug/Cygwin-Windows
rm -f build/Debug/Cygwin-Windows/Multiple.o.d
g++    -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/Multiple.o.d -o build/Debug/Cygwin-Windows/Multiple.o Multiple.cpp
Multiple.cpp: In function `int main()':
Multiple.cpp:22: error: variable `std::istringstream iss' has initializer but incomplete type
Multiple.cpp:60:3: warning: no newline at end of file
make[2]: *** [build/Debug/Cygwin-Windows/Multiple.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
nbproject/Makefile-Debug.mk:78: recipe for target `build/Debug/Cygwin-Windows/Multiple.o' failed
make[2]: Leaving directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
nbproject/Makefile-Debug.mk:61: recipe for target `.build-conf' failed
make[1]: Leaving directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed


BUILD FAILED (exit value 2, total time: 1s)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Read in a line from cin into a string using getline. Then put that string into an istringstream. Then read from that istringstream in place of where you're using cin now.

std::string line;
getline(std::cin, line);
std::istringstream iss(line);
while (iss >> enterNumber)
{
    numbers.push_back(enterNumber);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...