Help!A simple question...

This is a discussion on Help!A simple question... within the C++ Programming forums, part of the General Programming Boards category; hi,i am just a novice to cpp programming. here is a simple program to store 25 floating-numbers using a vector ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Hangzhou,China
    Posts
    7

    Help!A simple question...

    hi,i am just a novice to cpp programming.
    here is a simple program to store 25 floating-numbers using a vector and then output all the elements in the vector.
    when i excute it , some problem arise and i don't know why
    Could anyone tell me why???thx~~~

    Code:
    // This program asks user to input 25 floating numbers and then output them
    
    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;
    
    int main(){
    	vector<float> v;
    	int i=0;
    	while(i<3){
    		cin >> v[i] ;
    		i++;
    	}
    	cout << "\n";
    
    	cout << v[0] << endl;
    	
    	for(int j=0;j<v.size();j++){
    		cout << v[j] << endl;
    	}
    
    	return 0;
    }//

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,529
    > This program asks user to input 25 floating numbers and then output them
    > while(i<3){
    Comment says 25, code says 3
    Is that the problem?

    If not, try being specific and say what the problem really is, rather than just "a problem"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Ignoring the whole 25 vs 3 debate for the moment (assuming 3 elements for purposes of the examples below)...

    You haven't allocated any space for the vector. You need to reserve space for 3 elements before you can start adding them to the vector using that syntax. You can reserve space when you declare the vector or you can call the reserve member function at a later time (but before you start adding the elements). You can also just cin the value from the user into a temp float variable and then call the push_back member function on that variable:

    Code:
    vector<float> v(3);  // Reserves space for 3 floats
    int i = 0;
    while(i<3){
        cin >> v[i] ;
        i++;
    }
    
    or...
    
    vector<float> v;
    v.reserve(3);// Reserves space for 3 floats
    int i = 0;
    while(i<3){
        cin >> v[i] ;
        i++;
    }
    
    or...
    
    vector<float> v;
    int i = 0;
    float temp;
    while(i<3){
        cin >> temp;
        v.push_back(temp);
        i++;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Quote Originally Posted by hk_mp5kpdw
    Ignoring the whole 25 vs 3 debate for the moment (assuming 3 elements for purposes of the examples below)...

    You haven't allocated any space for the vector. You need to reserve space for 3 elements before you can start adding them to the vector using that syntax. You can reserve space when you declare the vector or you can call the reserve member function at a later time (but before you start adding the elements). You can also just cin the value from the user into a temp float variable and then call the push_back member function on that variable:
    This is not correct. There is a difference between reserve and resize. The reserve function affects the capacity of the vector (how much memory is reserved) and the resize function affects the size of the vector (how many elements exist).

    The first example sets the vector's initial size to 3, with all three values being default constructed (to 0.0f in this case). The capacity could be anything 3 or higher. It works because in the loop you are changing the value from 0.0f to whatever the user inputs.

    The second example sets the initial size of the vector to 0, then makes sure there is enough memory for 3 elements. It does not actually create 3 elements, so when you try to change the value of the elements in the loop, you will get undefined behavior. If you check the size of the vector after the loop it should still be 0.

    The third example does not set the initial size or reserve space. By using push_back, memory is allocated as necessary, and new objects are created at the time you push them back. This example works because push_back creates new elements, whereas operator[] just tries to access existing elements.

    To fix the second example, change reserve to resize, and do the same in the explanation.

    As for the OP, I'd suggest the first example, or a combination of the last two - using reserve and push_back.
    Last edited by Daved; 03-15-2005 at 02:41 PM.

  5. #5
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    You could try asking for the numbers.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Yep, the second example is wrong, should use resize and not reserve, my mistake... I was rushing out the door when I posted.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21