Thread: the code I wrote gives me an error once it compiles. I am unable to submit a proper o

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    19

    the code I wrote gives me an error once it compiles. I am unable to submit a proper o

    Code:
    
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;
    int main()
    {
    ifstream myfile ("C:\\Users\\sshaw2\\Documents\\Untitled3.txt");
    ofstream outfile ("C:\\Users\\sshaw2\\Documents\\output.txt");
    std::vector<string> myvector;
    string line;
    getline(myfile,line);
    outfile<<line<<endl;
    while(getline(myfile,line))
    {
    myvector.push_back(line);
    }
    sort(myvector.begin(),myvector.end(),greater<string>());
    // variable to to store the data
    float totalbalanceDue =0.0;
    int highestRental = 0;
    float highestDue = 0.0;
    string customerName = ""; // for highest rental customer
    string customerName1 = ""; // for hishest due customer
    while(!myvector.empty())
    {
    string line = myvector.back();
    outfile<<line<<endl;
    myvector.pop_back();
    istringstream iss(line);
    int count = 0;
    string lastName;
    string firstName;
    string rental;
    string balance;
    iss>>lastName;
    iss>>firstName;
    iss>>rental;
    iss>>balance;
    stringstream ss1(rental);
    int x= 0;
    ss1>>x;
    stringstream ss2(balance);
    float y=0.0;
    ss2>>y;
    //logic for customer having highest dues
    if(y>highestDue)
    {
    highestDue = y;
    customerName1 = firstName+ " " + lastName;
    }
    else if(y==highestDue)
    {
    customerName1 = customerName1 + ", " +firstName+" "+lastName;
    }
    // logic for customer having highest rental
    if(x>highestRental)
    {
    highestRental = x;
    customerName = firstName+ " " + lastName;
    }
    else if(x==highestRental)
    {
    customerName = customerName + ", " +firstName+" "+lastName;
    }
    }
    outfile <<endl;
    outfile <<endl;
    outfile <<"Total Balance Due for file:";
    outfile <<totalbalanceDue<<endl;
    outfile <<"Customer with highest number of rental days: ";
    outfile <<customerName<<endl;
    outfile <<"Customer with highest balance due are: ";
    outfile <<customerName1;
    myfile.close();
    outfile.close();
    return 0;
    }
    
    Attached Images Attached Images the code I wrote gives me an error once it compiles. I am unable to submit a proper o-pa4-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    Try "copy as text" in your IDE, or "paste as text" in your browser.

    So the code we see isn't that awful mess of unindented italics.
    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.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,671
    Unlikely to happen. I suspected they've copied/pasted the samet question from here (sshaw2?) The Code I Wrote Gives Me An Error Once It Compile... | Chegg.com and don't want to pay to get the answer.

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    i Dont want to use vector is there any other way ?

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Compiles just fine for me, no data files so
    Code:
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        ifstream myfile ("C:\\Users\\sshaw2\\Documents\\Untitled3.txt");
        ofstream outfile ("C:\\Users\\sshaw2\\Documents\\output.txt");
        
        int count = 0;
        int x= 0;
        float y=0.0;
            
        string lastName;
        string firstName;
        string rental;
        string balance;
            
        std::vector<string> myvector;
        string line;
        getline(myfile,line);
        outfile<<line<<endl;
        
        while(getline(myfile,line))
        {
            myvector.push_back(line);
        }
        sort(myvector.begin(),myvector.end(),greater<string>());
    
        // variable to to store the data
        float totalbalanceDue =0.0;
        int highestRental = 0;
        float highestDue = 0.0;
        string customerName = ""; // for highest rental customer
        string customerName1 = ""; // for hishest due customer
    
        while(!myvector.empty())
        {
            string line = myvector.back();
            
            outfile<<line<<endl;
            
            myvector.pop_back();
            
            istringstream iss(line);
                
            iss>>lastName;
            iss>>firstName;
            iss>>rental;
            iss>>balance;
        
            stringstream ss1(rental);
        
            ss1>>x;
        
            stringstream ss2(balance);
        
            ss2>>y;
        
            //logic for customer having highest dues
            if(y>highestDue)
            {
                highestDue = y;
                customerName1 = firstName+ " " + lastName;
            }
            else if(y==highestDue)
            {
                customerName1 = customerName1 + ", " +firstName+" "+lastName;
            }
        
            // logic for customer having highest rental
            if(x>highestRental)
            {
                highestRental = x;
                customerName = firstName+ " " + lastName;
            }
            else if(x==highestRental)
            {
                customerName = customerName + ", " +firstName+" "+lastName;
            }
        } // END WHILE
        
            outfile <<endl;
            outfile <<endl;
            
            outfile <<"Total Balance Due for file:";
            outfile <<totalbalanceDue<<endl;
        
            outfile <<"Customer with highest number of rental days: ";
            outfile <<customerName<<endl;
        
            outfile <<"Customer with highest balance due are: ";
            outfile <<customerName1;
        
        myfile.close();
        outfile.close();
    return 0;
    }
    compile results
    Code:
    g++  -std="gnu++11"  -Wall -o "temp" "temp.cpp" (in directory: /home/userx/bin)
    temp.cpp: In function 'int main()':
    temp.cpp:39:7: warning: unused variable 'count' [-Wunused-variable]
       int count = 0;
           ^
    Compilation finished successfully.
    Last edited by userxbw; 12-10-2017 at 09:54 PM.

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    is there any other way to sort the file by lastname ?

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    strcmp sort it alphabetically.

    post a small sample of your data file, if it is really big, if not post all of it. just need a little bit to work with. not too little though. how's that for using an that word little , arbitrarily?
    Last edited by userxbw; 12-11-2017 at 10:01 AM.

  8. #8
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    like I have to open up the text file and then sort it by the last name and then it output to another file.

    The specification is as follows:
    1. Sort the input file by the last name, and create a new sorted file called “sorted.txt”
    2. In the new file you created in 1. , also include the following information:
    a. Total balance due, which would be the sum of all records
    b. The customer with the highest number of rental days.
    c. The customer with the highest balance due.

    I posted the picture of the problem i have to solve. If you can help it will be very kind

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    that's the input file
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    I have to sort it by either straight selection sort or simple insertion sort. please help me out

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by ahmed_tannu View Post
    I have to sort it by either straight selection sort or simple insertion sort. please help me out
    then why did you ask
    is there any other way to sort the file by lastname ?
    when you already know you have to sort it either one way or another. why are you being coy?

    let me google them two methods and see what I can do.

  12. #12
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    thanks for helping me sir

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    Sorting – Straight Selection Sort
     
    
    • Begin from the first element, taking i=0 to n-1.
    • Find the minimum number in the first scan.
    • Interchange ith element with the minimum number.
    • Start searching minimum element again from i+1 to n.
    • The number of scans is equal to the number of elements.
    • Stop scan when the last element remains to be replaced with min.
    • Display the output which is now sorted.
    for that number part, using strcmp and its return value how would you set that up to move them accordingly. knowing the alphabet

    Selection sort - Wikipedia


    Code:
    Return Value from strcmp()
    
          
    Return Value Remarks
    0 if both strings are identical (equal)
    negative if the ASCII value of first unmatched character is less than second.
    positive integer if the ASCII value of first unmatched character is greater than second.
    simple insertion sort.

    Insertion Sort - GeeksforGeeks

    and

    Insertion sort - Wikipedia

    same thing using strcmp
    Last edited by userxbw; 12-11-2017 at 11:13 AM.

  14. #14
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    oh yeah. so is there a way you can demonstrate the problem I have with a code

  15. #15
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by ahmed_tannu View Post
    oh yeah. so is there a way you can demonstrate the problem I have with a code
    I gave you your solutions to the problem you just need to learn how to use them to your advantage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to more intuitively wrote more elegant c++ code?
    By Tesp in forum C++ Programming
    Replies: 13
    Last Post: 07-26-2016, 03:58 PM
  2. I wrote a piece of code, can't find where is the bug....
    By meili100 in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2007, 11:25 AM
  3. Replies: 4
    Last Post: 09-16-2006, 07:11 PM
  4. Program Compiles but does not read proper file....
    By michigan353 in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2005, 08:19 PM
  5. code compiles without error, but
    By kashifk in forum C Programming
    Replies: 9
    Last Post: 04-11-2003, 08:07 AM

Tags for this Thread