vector to float

This is a discussion on vector to float within the C++ Programming forums, part of the General Programming Boards category; Code: I have done like this.. but still many error.. i have change ti float..then for array.total i change to ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    21

    vector to float

    Code:
    I have done like this.. but still many error..
    i have change ti float..then for array.total i change to size.total=0.
    
    rror C2228: left of '.push_back' must have class/struct/union type
    F:\460.cpp(37) : error C2228: left of '.clear' must have class/struct/union type
    F:\460.cpp(40) : error C2228: left of '.push_back' must have class/struct/union type
    F:\460.cpp(53) : error C2228: left of '.size' must have class/struct/union type
    F:\460.cpp(56) : error C2228: left of '.total' must have class/struct/union type
    F:\460.cpp(57) : error C2228: left of '.at' must have class/struct/union type
    Error executing cl.exe.
    
    
    Help with Code Tags C++ Syntax (Toggle Plain Text) 
    #include <iostream>   // std::cout#include <fstream>#include <iomanip>#include <string>    // std::string#include <vector>    // std::vector<>#include <algorithm> //std::for each()using namespace std; // import "std" namespace into global namespace struct exam{int examid;float total;};	int main() 	{	ifstream stream1("STA83SOLUTION.txt");		if ( !stream1.is_open())		{		cout << "While opening a file an error is encountered" << endl;		} 			else 			{			cout << "Fail Di buka....." << endl;			}	vector <exam> exams;	exam aExam;    int tempExamID;    int tempTotal;    stream1 >> tempExamID >> tempTotal;    aExam.examid = tempExamID;    aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes    while (stream1 >> tempExamID >> tempTotal)    {        if(tempExamID != aExam.examid)        {        exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector        aExam.total.clear();        aExam.examid = tempExamID;        }        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes    }    exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector    stream1.close(); // We have read the entire file, so time to close it.{	ofstream myfile;	myfile.open("411.txt"); 	int temp, flag;	if (myfile.is_open())	{		for (size_t i = 0; i < exams.size(); i++) 		{			for (size_t j = 0; j<exams.at(i).total.size(); j++) 			{ 	size.total = 0;		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student	  		}		}	} cin.get();return 0;}}#include <iostream>   // std::cout
    #include <fstream>
    #include <iomanip>
    #include <string>    // std::string
    #include <vector>    // std::vector<>
    #include <algorithm> //std::for each()
    using namespace std; // import "std" namespace into global namespace
    
    struct exam
    {
    int examid;
    float total;
    };
    	int main() 
    	{
    	ifstream stream1("STA83SOLUTION.txt");
    		if ( !stream1.is_open())
    		{
    		cout << "While opening a file an error is encountered" << endl;
    		} 
    			else 
    			{
    			cout << "Fail Di buka....." << endl;
    			}
    	vector <exam> exams;
    	exam aExam;
        int tempExamID;
        int tempTotal;
        stream1 >> tempExamID >> tempTotal;
        aExam.examid = tempExamID;
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
        while (stream1 >> tempExamID >> tempTotal)
        {
            if(tempExamID != aExam.examid)
            {
            exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
            aExam.total.clear();
            aExam.examid = tempExamID;
            }
            aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
        }
        exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
        stream1.close(); // We have read the entire file, so time to close it.
    {
    	ofstream myfile;
    	myfile.open("411.txt");
    	
    	int temp, flag;
    	if (myfile.is_open())
    	{
    		for (size_t i = 0; i < exams.size(); i++) 
    		{
    			for (size_t j = 0; j<exams.at(i).total.size(); j++) 
    			{
    
    	size.total = 0;
    		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
    	  		}
    		}
    	}
    					
    cin.get();
    return 0;
    }
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    aExam.total.push_back(tempTotal);

    What are you doing here?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    i have this data

    1: 40
    2: 30
    3:60


    and aExam.total.push_back(tempTotal); is to read a data....

    before thsi I use vector <int> total, and it succesfully read a data from file..........but I want to change to float to read a data..because after this I want to so a sorting together with
    number and total.

    like this

    3:60
    1: 40
    2: 30

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you have/want a vector of floats, why are you trying to push_back an int?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by nurulshidanoni View Post
    i have this data

    1: 40
    2: 30
    3:60


    and aExam.total.push_back(tempTotal); is to read a data....
    aExam.total is of type "float". It is a built-in type that has no member functions, like push_back.

    Now might be the time to get a good C++ book.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by medievalelks View Post
    aExam.total is of type "float". It is a built-in type that has no member functions, like push_back.

    Now might be the time to get a good C++ book.
    Good catch -- I didn't even try to read the code, formatted the way it was. So, never mind what I said before; why didn't you change vector<int> to vector<float>?

    Edit to add: And I still don't see why you need to change it to a float just to sort it. (Or was some of the data floating point to begin with?)
    Last edited by tabstop; 04-23-2008 at 09:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 04:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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