vector sourcing information problem

This is a discussion on vector sourcing information problem within the C++ Programming forums, part of the General Programming Boards category; Okay in this vector array program I got it to print out the values in the input file number and ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    25

    vector sourcing information problem

    Okay in this vector array program I got it to print out the values in the input file number and price. How would i go about havng a question in the bottom somewhere asking a user for an item number , meaning first - 10 entrys in the input file.

    for example here is my txt file.

    100 13.43
    101 10.99
    102 3.45 (lets say i want the user to be able to type a number
    of 3 and it will go and display this info.)
    103 5.72
    104 6.90
    105 3.99
    106 2.78
    107 15.98
    108 4.34
    109 2.87
    110 3.67

    Here is my code so far:

    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    void main()
    {
    	vector<int> number;
    	vector<double> price;
    
    	int num;
    	double pri;
    
    	ifstream inFile("product.txt", ios::in);
    
    	if(!inFile)
    	{
    		cerr<<"File could not be opened!"<<endl;
    		exit(1);
    	}
    
    	while(!inFile.eof())
    	{
    		inFile >> num >> pri;
    		number.push_back(num);
    		price.push_back(pri);
    	}
    
    	cout << setw(10)<< "Product no." << setw(10) << "Price" << "\n";
    	for(int i=0; i < number.size(); i++)
    		cout << setw(10) << number[i] << setw(10) << price[i] << "\n";
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > void main()
    I see others have already told you, so here's another nag
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    > while(!inFile.eof())
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    > How would i go about havng a question in the bottom somewhere asking a user for an item number
    You mean like
    cout << "Which one"';
    cin >> thisOne;
    ?
    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
    Join Date
    Jul 2006
    Posts
    25
    salem

    one the nag on the main() i have to do it that way for my class.

    I know you are well respected in this forum , at least i see you post alot of stuff. But could you at least point a person in the right direction once in awhile instead of just nagging.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    The right direction begins with proper coding. If you code compiles and is well formated, I doubt the teacher will care the return value of main, if he finds void main() ok. What teacher at what school... perhaps we can email him and point him in the right direction too.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    point taken. just following the book. i still need help with my problem.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Well post the code that you have, if you got the point of what Salem said, then you should be able to show something that is a lot close to what you need... show an attempt at asking the user the question, and taking the responce and trying to use it.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    This must be a bad time of the day for you. I asked the question at the top, not if my main and eof was correct. I can do those corrections , after i get on the right track on my problem.

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    i am asking if the data is set with a vector. How would i ask the question and have the program source the right values from the file.

    I know how to do cout , cin. I am asking past that???

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Quote Originally Posted by Salem
    > How would i go about havng a question in the bottom somewhere asking a user for an item number
    You mean like
    cout << "Which one"';
    cin >> thisOne;
    ?
    Salem gave you the answer to your question a while ago. It turns out that cin extraction, like Salem demonstrates, is one of the easiest ways to get data from the user. You only need to choose an appropriate data type for what you want.

    Then you can push it onto a vector. Maybe
    Code:
    int temp;
    vector<int> myvector;
    while (cin >> temp) {
       myvector.push_back(temp);
    }
    Last edited by whiteflags; 07-15-2006 at 12:35 PM.

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, obviously number and price are related, so it's not good style to make them paralell vectors. So.

    Code:
    struct Entry
    {
        double price
        int number
    
        Entry(int p, int n) : price(p), number(n) { }
    };
    Code:
    vector<Entry> v;
    
    while(infile >> tempnumber >> tempprice)
    {
        v.push_back(Entry(tempnumber, tempprice));
    }
    Code:
    cout << v[usernumber].number << v[usernumber].price;

  11. #11
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    If I understood well, you need the user to input the number of something and the program will print the price for it. Well, there are some different ways, I will give you the most simple way I can imagine right now:
    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
        vector<int> v;
        v.reserve( 10 );
        
        for(int i=0;i<=9;i++)
            v.push_back(i);
        
        int value;
        
        cin >> value;
        
        vector<int>::iterator position =
            find( v.begin(), v.end(), value);
    
        cout << "The value is " << *position << endl;
    
        return 0;
    }
    As you can see, I use find to find the position of the value I want. Once you have the iterator, you must use it to discover the number of the position:
    Code:
    int int_position = position - v.begin();
    //use the int_position to access price vector
    If it is not what you want, then ignore this post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Problem with my file opener
    By kzar in forum C Programming
    Replies: 7
    Last Post: 04-20-2005, 04:20 PM
  3. C++ Share Interface Problem
    By Morphios in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2003, 07:30 PM
  4. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM

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