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

  1. #16
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    if you dont mind can you write down the code ?

  2. #17
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I will give you this much because you also need to learn how to check your code to be sure it is doing what you want it to do, don't just assume it is working just because it looks right.
    Code:
        /*
        string line;
        
        getline(myfile,line);
        outfile<<line<<endl;
        
        while(getline(myfile,line))
        {
            myvector.push_back(line);
        }
        */
        while (myfile >> firstName >> lastName >> rental >> balance)
        {
            myvector.push_back(firstName + " " + lastName + " " + rental + " " + balance);
        }
        for (auto i = myvector.begin(); i != myvector.end(); ++i)
        std::cout << *i << ' ';
    it was not working so I fixed that.
    Code:
    // your code was only storing this
    
    userx@slackwhere:~/bin
    $ ./temp
    starting
     anilo steve 0 0 userx@slackwhere:~/bin
    $ ./temp
    
    / my fix gives you this to work with. 
    starting
    Smith Joe 15 100.50 Doe John 10 95.20 Anderson Paul 30 20.00 O'Donell Miriam 10 24.30 Foster Sam 30 15.00 Zom Pete 10 20.00 Mock Chilly 100 30 smitty Chris 200 200 xu Conor 1 200 anilo steve 0 0 userx@slackwhere:~/bin
    $
    Last edited by userxbw; 12-11-2017 at 11:49 AM.

  3. #18
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    i have to submit the assignment and i just have like 2 hours please i would be so great if you can help me out

  4. #19
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    i trying but i couldn't do it

  5. #20
    Banned
    Join Date
    Aug 2017
    Posts
    861
    did you see post 18, and I'd have to whip out my google skills to even try to complete this in under two hours and I do not have a paypal account.

    it is all in how you store your information, I do believe if I am correct that code I wrote turns it into one big string, then you'd have to split it again to get the last name in order to sort it.
    Last edited by userxbw; 12-11-2017 at 11:59 AM.

  6. #21
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    i dont know how im gonna finish it

  7. #22
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    What if i give the whole code and you can just do the sorting code ?

  8. #23
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    It's time you just failed already.
    If you put as much effort into study as you put into begging, you'd have this solved.
    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.

  9. #24
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by ahmed_tannu View Post
    What if i give the whole code and you can just do the sorting code ?
    I got the whole code. how do you think I changed it?

  10. #25
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    I did everything opening up from a file outputting it to other file but the thing i just dont get is the sorting and the first line of the input file which is the headers : Lastname, firstname, daysofrental, and balance

  11. #26
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cmath>
    #include <stdlib.h>
    
    
    
    
    using namespace std;
    
    
    const int MAX_SIZE = 50;
    const int NAME_SIZE = 30;
    const int COLUMNROW = 4;
    typedef char STRING_30[NAME_SIZE];
    typedef STRING_30 LIST_S30[MAX_SIZE];
    typedef STRING_30 LIST_COL[COLUMNROW];
    typedef int LIST_I[MAX_SIZE];
    typedef float LIST_F[MAX_SIZE];
    
    
    
    
    int main(int argc, char *argv[])
    {
    	int	choice;
    	char drive[2];
    	char disk_file[15];
    	char file[9];
    	float sum = 0;
    	float tot_balance = 0;
    	float indx;
    
    
    	ofstream outfile;
    
    
    	system ("cls"); 
    
    
        ifstream inf;
        inf.open("C:\\Users\\ahmed\\Downloads\\invoice1_test1 (1).txt");
        
    
    
    	LIST_S30 lastname, firstname;
        LIST_I daysofrent;
        LIST_F balancedue;
        LIST_COL headers;
        int highest = 0; 
        int highbal = 0; 
        int listcount = 0;
       
        while ( listcount <= 10)
        {
              if (listcount == 0)
              {
                 inf >> headers
    [listcount] >> headers
    [listcount] >> headers
    [listcount] >> headers
    [listcount];
              }
              else
              {
                  inf >> lastname
    [listcount-1] >> firstname
    [listcount-1] >> daysofrent
    [listcount-1] >> balancedue
    [listcount-1];
              }
              listcount ++;
        
        }
        
    	listcount=0;
        
    	while ( listcount < 10)
        {
              outfile <<  lastname
    [listcount] << ", " << firstname
    [listcount];
              outfile << " " << daysofrent
    [listcount] << " " << balancedue
    [listcount] << endl;
              listcount ++;
        }
        
        for (int i=0; i < 10; i++ ){
        	tot_balance += balancedue [i]; 
        }
        
        for (int i = 1; i < 10; i++) {
        	if (daysofrent [highest] < daysofrent [i]) {
        		highest = i;
        	}
        }
        
        for (int i = 0; i < 10; i++) {
        	if (balancedue [highbal] < balancedue [i]) {
        		highbal = i;
        	}
        }
    
    
      outfile.open ("C:\\Users\\ahmed\\Desktop\\output.txt.txt");
    		
      outfile << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
        
        listcount=0;
        
        while ( listcount < 10)
        {
              outfile <<  lastname
    [listcount] << ", " << firstname
    [listcount];
              outfile << daysofrent
    [listcount] << " " << balancedue
    [listcount] << endl;
              listcount ++;
        }
        
        
    	outfile << endl; 
        
    	outfile << left << "Total Balance Due for File: $ " << tot_balance << endl; 
        
            for (int i = 0; i < 10; i++) {
        		if (daysofrent [highest] == daysofrent [i]){
        		outfile << "Customer with the Highest Number of Rental Days: " << lastname [i] << ", " << firstname [i] << endl;
        		}
       	 	}
        
        	for (int i=0; i < 10; i++) {
        		if (balancedue [highbal] == balancedue [i]) {
        			outfile << "Customer With the Highest Balance Due: " << lastname [i] << ", " << firstname [i] << endl;
        		}
        	}
        	
      outfile.close();
      cout << endl << endl;    
      system ("pause");
      return 0;
      }

  12. #27
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    Here what i did so far

  13. #28
    Banned
    Join Date
    Aug 2017
    Posts
    861
    here is what i did in my program I wrote awhile back and if you by chance figure out how to use it to your advantage, and the teacher asks you how did you figure that one out, well then that answer you give him or her is up to you isn't it?

    Code:
    //holds the path and file name
    std::vector<std::string> dfile;
    
    
    void sort_V()
    {
        std::sort(opts.dfile.begin(), opts.dfile.end(), compareNoCase);
    }
    // Comparison; not case sensitive.
    bool compareNoCase (std::string first, std::string second)
    {
      unsigned long int  i = 0;
      while ((i < first.length()) && (i < second.length()))
      {
        if (tolower (first[i]) < tolower (second[i])) return true;
        else if (tolower (first[i]) > tolower (second[i])) return false;
        i++;
      }
    
      if (first.length() < second.length()) return true;
      else return false;
    }
    now you have to ask yourself, does that sort meet the requirements for the two sorts you have to pick from?

    On your next assignment you get, learn how to google to ask how to's and work with small parts of your code until you get that to work first then move on to the next step in your program that needs to be done. Then if you have issues feel free to post in here or wherever for help and advice not will you do my homework I got two hours to turn it in. and not even show anything kind of work on your part in what you have been doing while waiting for an answer.
    Last edited by userxbw; 12-11-2017 at 12:18 PM.

  14. #29
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    thanks for the advice

  15. #30
    Registered User
    Join Date
    Nov 2017
    Posts
    19
    i have other assignments which is due within an hour im finishing off that

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