ppl
March 27th, 2007, 08:33 PM
Hi, as a newbie in C++ , I was trying to write a simple exercise program(question 8 ,C 02 in Thinking in C++). Here is my program:
//: creat a vector<float> and put 25 float point numbers into it using for loop and display the vector
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<float> f_vector;
float number;
for(int i = 0; i < 5; i++){
cout << "Pleas input a float point number: ";
cin >> number;
f_vector.push_back(number);
}
for(int j = 0; j < f_vector.size(); j++)
cout << "The number :" << j << " is " << f_vector[j] << endl;
}
However, the program didn't work as I expect. It works when I input float numbers, but when I input things other than float points numbers, it will output something like this:
Pleas input a float point number: p
Pleas input a float point number: Pleas input a float point number: Pleas input a float point number: Pleas input a float point number: The number :0 is -
2.96384e-05
The number :1 is -2.96384e-05
The number :2 is -2.96384e-05
The number :3 is -2.96384e-05
The number :4 is -2.96384e-05
As you could see , the program will finish when I input any one character other than 5 float point numbers. Don't understand the reasons of that kind behavior of my program. As I read from Thinking in C++:
"The iostreams operator used with cin is >>. This operator waits for the same kind of input as its argument. For example, if you give it an integer argument, it waits for an integer from the console."
Shouldn't my program wait for a float point number to process, and how comes it finish the for loop when I only input once? How could I avoid that?
And another question I was curious when I writing C++ program is when I write something like :" int intNum;" or "float floatInput;" Is that variable initialize automatically by C++ itself, or it is something you can not depend on, you'd better manually do it by yourself?
Please help, thanks in advance.
//: creat a vector<float> and put 25 float point numbers into it using for loop and display the vector
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<float> f_vector;
float number;
for(int i = 0; i < 5; i++){
cout << "Pleas input a float point number: ";
cin >> number;
f_vector.push_back(number);
}
for(int j = 0; j < f_vector.size(); j++)
cout << "The number :" << j << " is " << f_vector[j] << endl;
}
However, the program didn't work as I expect. It works when I input float numbers, but when I input things other than float points numbers, it will output something like this:
Pleas input a float point number: p
Pleas input a float point number: Pleas input a float point number: Pleas input a float point number: Pleas input a float point number: The number :0 is -
2.96384e-05
The number :1 is -2.96384e-05
The number :2 is -2.96384e-05
The number :3 is -2.96384e-05
The number :4 is -2.96384e-05
As you could see , the program will finish when I input any one character other than 5 float point numbers. Don't understand the reasons of that kind behavior of my program. As I read from Thinking in C++:
"The iostreams operator used with cin is >>. This operator waits for the same kind of input as its argument. For example, if you give it an integer argument, it waits for an integer from the console."
Shouldn't my program wait for a float point number to process, and how comes it finish the for loop when I only input once? How could I avoid that?
And another question I was curious when I writing C++ program is when I write something like :" int intNum;" or "float floatInput;" Is that variable initialize automatically by C++ itself, or it is something you can not depend on, you'd better manually do it by yourself?
Please help, thanks in advance.