Thread: error <,,.>

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Which returns an integer, probably unsigned (or more exactly, std::size_t). It's not a reference so it can't be assigned...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    soo...I must chage size() whit what?

    Code:
    if (exams.at(i).total.size() < exams.at(i).total.size()+1)      // ascending order simply changes to <
    				{ 
                        temp = exams.at(i).total.size();             // swap elements
                        exams.at(i).total.size() = exams.at(i).total.size()+1;
                        exams.at(i).total.size()+1 = temp;
                        flag = 1;               // indicates that a swap occurred.
                    }

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It depends on what you want to swap, but I am guessing that you just want to swap exams.at(i).total with exams.at(i+1).total
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    i do like this but error...

    C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(68) : error C2106: '=' : left operand must be l-value


    Code:
     #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;
    vector <int> total;
    };
    
    void SwapMembers (int items[], int index1, int index2)
    {
    	int temp;
    	temp=items[index1];
    	items[index1]=items[index2];
    	items[index2]=temp;
    }
    
    	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++) 
    			{
    				if (exams.at(i).total.size() < exams.at(i).total.size()+1)      // ascending order simply changes to <
    				{ 
                        temp = exams.at(i).total.size();             // swap elements
                        exams.at(i).total.at(j) = exams.at(i).total.at(j)+1;
                        exams.at(i).total.at(j)+1 = temp;
                        flag = 1;               // indicates that a swap occurred.
                    }
    
    		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
    	  	cout<<" "<< exams.at (i).total.at(j)<<"\t";
    			}
    		}
    	}
    					
    cin.get();
    return 0;
    }
    }

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you know what is the difference between exams.at(i).total.at(j)+1 and exams.at(i).total.at(j+1)?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    No i dont know the diffreence (j)+1 and (j+1)...

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    oo..i understand now..but i change (j_)+1 with (j+1) but its abnormal program.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    exams.at(i).total.at(j) gives you the int at the jth position in the total vector member of the exam at the ith position in the exams vector (that was quite a mouthful).

    exams.at(i).total.at(j)+1 is the sum of that int and 1.

    exams.at(i).total.at(j+1) is the next int after that int.

    Clearly, you cannot swap that int with the sum of itself and 1. But you can swap that int with the next int.

    oo..i understand now..but i change (j_)+1 with (j+1) but its abnormal program.
    A std::out_of_range exception would be thrown for the last element, since at the last index, there is no next element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    i do like this but abnormal

    Code:
    	if (exams.at(i).total.at(j) < exams.at(i).total.at(j+1) )     // ascending order simply changes to <
    				{ 
                        temp = exams.at(i).total.at(j);             // swap elements
                        exams.at(i).total.at(j) = exams.at(i).total.at(j+1);
                        exams.at(i).total.at(j+1) = temp;
                        flag = 1;               // indicates that a swap occurred.
                    }

  10. #25
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    are your indexes in the range?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #26
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    indexes in the range? i dont understand

  12. #27
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    I have this programming but I want to sort the total in descending order...
    How to make an examid together with total after has sort the output?

    how to use hash and map in this programming?
    Code:
    #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;
    vector <int> total;
    };
    
    void SwapMembers (int items[], int index1, int index2)
    {
    	int temp;
    	temp=items[index1];
    	items[index1]=items[index2];
    	items[index2]=temp;
    }
    
    	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++) 
    			{
    				if (exams.at(i).total.at(j) < exams.at(i).total.at(j+1) )     // ascending order simply changes to <
    				{ 
                        temp = exams.at(i).total.at(j);             // swap elements
                        exams.at(i).total.at(j) = exams.at(i).total.at(j)+1;
                        exams.at(i).total.at(j)+1 = temp;
                        flag = 1;               // indicates that a swap occurred.
                    }
    
    		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
    	  	cout<<" "<< exams.at (i).total.at(j)<<"\t";
    			}
    		}
    	}
    					
    cin.get();
    return 0;
    }
    }

  13. #28
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if i or j+1 are out of range - at will through an exeption,

    you can wrap your code in try/catch block and printout the exeption description to be sure, or just reread your code to see if you step out of the size of the vector
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #29
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you continue your work even if file failed to open?

    Code:
    j<exams.at(i).total.size()
    so what it will make of your j+1 index?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #30
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    So I must change it to exams.at(i).total.size()+.....?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small -> big -> bigger -> bigger than bigger -> ?
    By happyclown in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-11-2009, 12:12 PM
  2. Dev-C++ -> Tools - > Editor -> Syntax
    By Yuri2 in forum C++ Programming
    Replies: 19
    Last Post: 07-03-2006, 07:48 AM
  3. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 02:06 PM
  4. electricity > AC circuits > tesla coil
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 03-14-2002, 02:16 PM