Thread: running into an error with my program

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    23

    running into an error with my program

    Doing a program for class and I am running into an error any idea where the problem lies?

    Code:
    The problem says: [Error] expected unqualified-id before '{' token

    Programming Assignment:
    The client was pleased with their invoice program, and he'd like you to investigate anotherproblem he is running into. He has a text file which has a number of invoices, and would like tohave you read this file, sort it in alphabetical order by last name, and then write it out to a newfile. He would also like the following information on the sorted file: total of balance due, recordwith the customer with the most number of rental days, and the record with the customer withthe highest balance due. For example, the input file would like like the following:

    LastName FirstName DaysofRental BalanceDueSmith
    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

    The output file would look like the following:
    LastName FirstName DaysofRental BalanceDue
    Anderson Paul 30 20.00
    Doe John 10 95.20
    Foster Sam 30 15.00
    O'Donell Miriam 10 24.30
    Smith Joe 15 100.50
    Total Balance Due for file: 255.00
    Customer with the highest number of rental days are : Paul Anderson, Sam Foster
    Customer with the highest balance due are: Joe Smith

    AssumptionsAssume that the input file1) will never contain records with customers of the same last name.2) is always in the same format, and all records are separated by spaces3) Days in the file are integers4) Balance due will always be a floating point number.5) Names will always have the appropriate casing.6) There will be no interaction from the user. No prompting the user to type in a file name, filename will always have the name "invoice.txt".7) A file could have 1 or more customers with the same balance due, and number of rentaldays. If this is the case, you should write out all records that fit the criteria.8) Assume the file always has 10 records.

    INPUTS and OUTPUTSInputs:
    Text FileOutputs: Sorted Text File with additional lines for total balance due of file, customers withhighest number of rental days, and customers with highest balance due.


    Code:
    #include <iostream>#include <fstream>
    #include <string>
    #include <iomanip>
    #include <cstdlib>
    #include <vector>
    #include <algorithm>
    
    
    using namespace std;
    
    
    struct Invoice{  //structure that represents each invoice
        string lastName;
        string firstName;
        int daysOfRental;
        double balanceDue;
    }list[10];  //since we can have at most 10
    
    
    int main(void);
    {
        ifstream infile("invoice.txt");  //the file that we read the data from
        ofstream tofile("answer.txt"); //the file that we write the data to
        string line;
        getline(infile, line);        //we skip the line that has the names of columns
    
    
        for (int i = 0; i < 5; ++i)
        {
            infile >> list[i].lastName >> list[i].firstName >> list[i].daysOfRental >> list[i].balanceDue; //read each field of Invoice structure
        }
    
    
        for (int i = 0; i < 4; ++i){   //sort arrays of voices by last name
    
    
            for (int j = i + 1; j < 5; ++j)
            {
                string lastname1 = list[i].lastName;
                string lastname2 = list[j].lastName;
                int x = 0;
                for (int k = 0; k < (int)min(lastname1.size(), lastname2.size()); ++k)
                {
                    if (tolower(lastname1[k]) > tolower(lastname2[k]))
                    {
                        x = -1;
                        break;
                    }
    
    
                    else if (tolower(lastname1[k]) == tolower(lastname2[k]))continue;
                    else {
                        x = 1; break;
                    }
                }
    
    
                if (x == 0)
                {
                    if (lastname1.size() > lastname2.size())
                    {
                        x = -1;
                    }
                }
    
    
                if (x == -1)swap(list[i], list[j]);
            }
        }
    
    
    
    
        double totalBalance = 0;  //total balance of all invoices
        vector<pair<string, string> >highestDays;
        vector<pair<string, string> >highestBalance;
        double maxBalance = 0;
        double maxDays = 0;
    
    
        for (int i = 0; i < 5; ++i)
        {
            totalBalance += list[i].balanceDue;
            if (list[i].daysOfRental > maxDays)
            {
                highestDays.clear();
                highestDays.push_back({ list[i].firstName, list[i].lastName });
                maxDays = list[i].daysOfRental;
            }
    
    
            else if (list[i].daysOfRental == maxDays)
            {
                highestDays.push_back({ list[i].firstName, list[i].lastName });
            }
    
    
            if (list[i].balanceDue > maxBalance)
            {
                highestBalance.clear();
                highestBalance.push_back({ list[i].firstName, list[i].lastName });
                maxBalance = list[i].balanceDue;
            }
    
    
            else if (list[i].balanceDue == maxBalance)
            {
    
    
                highestBalance.push_back({ list[i].firstName, list[i].lastName });
    
    
            }
    
    
        }
    
    
        ////////write data to the output file
        for (int i = 0; i < 5; ++i)
        {
            tofile << list[i].lastName << " " << list[i].firstName << " " << list[i].daysOfRental << " " << list[i].balanceDue << endl;
        }
    
    
        tofile << endl;
        tofile << "Total Balance Due for file: " << totalBalance << endl;
        tofile << "Customer with the highest number of rental days are: ";
        for (int i = 0; i < highestDays.size(); ++i)
        {
            tofile << highestDays[i].first << " " << highestDays[i].second;
            if (i == highestDays.size() - 1)tofile << endl;
            else tofile << ", ";
        }
    
    
        tofile << "Customer with the highest balance due are: ";
        for (int i = 0; i < highestBalance.size(); ++i)
        {
            tofile << highestBalance[i].first << " " << highestBalance[i].second;
            if (i == highestBalance.size() - 1)tofile << endl;
            else tofile << ", ";
        }
    
    
        return 0;
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Error messages normally come with line numbers.
    Code:
    $ g++ -Wall foo.cpp
    foo.cpp:22:1: error: expected unqualified-id before ‘{’ token
     {
    Further, if you compile in an IDE which knows about your compiler, then it can usually place the cursor at the error message line to begin with.

    The answer?
    > int main(void);
    You have a ; at the end of this line!
    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
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Quote Originally Posted by Salem View Post
    Error messages normally come with line numbers.
    Code:
    $ g++ -Wall foo.cpp
    foo.cpp:22:1: error: expected unqualified-id before ‘{’ token
     {
    Further, if you compile in an IDE which knows about your compiler, then it can usually place the cursor at the error message line to begin with.

    The answer?
    > int main(void);
    You have a ; at the end of this line!
    Thank you sorry about that a silly error but when I try to compile it with code::blocks I get different errors now.

    Line 76,82,88,95 Error: expected Primary-expression before '{' token
    Last edited by Pastryking; 12-20-2016 at 08:57 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Pastryking View Post
    Thank you sorry about that a silly error but when I try to compile it with code::blocks I get different errors now.

    Line 76,82,88,95 Error: expected Primary-expression before '{' token
    Post your current code. Some of those line numbers don't correspond to anything in your original code.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Quote Originally Posted by Elkvis View Post
    Post your current code. Some of those line numbers don't correspond to anything in your original code.
    Sorry about that my bad.

    Code:
    #include <iostream>#include <fstream>
    #include <string>
    #include <iomanip>
    #include <cstdlib>
    #include <vector>
    #include <algorithm>
    
    
    using namespace std;
    
    
    struct Invoice{  //structure that represents each invoice
    	string lastName;
    	string firstName;
    	int daysOfRental;
    	double balanceDue;
    }list[10];  //since we can have at most 10
    
    
    int main(void)
    {
    	ifstream infile("invoice.txt");  //the file that we read the data from
    	ofstream tofile("answer.txt"); //the file that we write the data to
    	string line;
    	getline(infile, line);        //we skip the line that has the names of columns
    
    
    	for (int i = 0; i < 5; ++i)
    	{
    		infile >> list[i].lastName >> list[i].firstName >> list[i].daysOfRental >> list[i].balanceDue; //read each field of Invoice structure
    	}
    
    
    	for (int i = 0; i < 4; ++i){   //sort arrays of voices by last name
    
    
    		for (int j = i + 1; j < 5; ++j)
    		{
    			string lastname1 = list[i].lastName;
    			string lastname2 = list[j].lastName;
    			int x = 0;
    			for (int k = 0; k < (int)min(lastname1.size(), lastname2.size()); ++k)
    			{
    				if (tolower(lastname1[k]) > tolower(lastname2[k]))
    				{
    					x = -1;
    					break;
    				}
    
    
    				else if (tolower(lastname1[k]) == tolower(lastname2[k]))continue;
    				else {
    					x = 1; break;
    				}
    			}
    
    
    			if (x == 0)
    			{
    				if (lastname1.size() > lastname2.size())
    				{
    					x = -1;
    				}
    			}
    
    
    			if (x == -1)swap(list[i], list[j]);
    		}
    	}
    
    
    
    
    	double totalBalance = 0;  //total balance of all invoices
    	vector<pair<string, string> >highestDays;
    	vector<pair<string, string> >highestBalance;
    	double maxBalance = 0;
    	double maxDays = 0;
    
    
    	for (int i = 0; i < 5; ++i)
    	{
    		totalBalance += list[i].balanceDue;
    		if (list[i].daysOfRental > maxDays)
    		{
    			highestDays.clear();
    			highestDays.push_back({ list[i].firstName, list[i].lastName });
    			maxDays = list[i].daysOfRental;
    		}
    
    
    		else if (list[i].daysOfRental == maxDays)
    		{
    			highestDays.push_back({ list[i].firstName, list[i].lastName });
    		}
    
    
    		if (list[i].balanceDue > maxBalance)
    		{
    			highestBalance.clear();
    			highestBalance.push_back({ list[i].firstName, list[i].lastName });
    			maxBalance = list[i].balanceDue;
    		}
    
    
    		else if (list[i].balanceDue == maxBalance)
            {
    
    
    			highestBalance.push_back({ list[i].firstName, list[i].lastName });
    
    
            }
    
    
    	}
    
    
    	////////write data to the output file
    	for (int i = 0; i < 5; ++i)
    	{
    		tofile << list[i].lastName << " " << list[i].firstName << " " << list[i].daysOfRental << " " << list[i].balanceDue << endl;
    	}
    
    
    	tofile << endl;
    	tofile << "Total Balance Due for file: " << totalBalance << endl;
    	tofile << "Customer with the highest number of rental days are: ";
    	for (int i = 0; i < highestDays.size(); ++i)
    	{
    		tofile << highestDays[i].first << " " << highestDays[i].second;
    		if (i == highestDays.size() - 1)tofile << endl;
    		else tofile << ", ";
    	}
    
    
    	tofile << "Customer with the highest balance due are: ";
    	for (int i = 0; i < highestBalance.size(); ++i)
    	{
    		tofile << highestBalance[i].first << " " << highestBalance[i].second;
    		if (i == highestBalance.size() - 1)tofile << endl;
    		else tofile << ", ";
    	}
    
    
    	return 0;
    
    
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Code:
    $ g++ -Wall foo.cpp
    foo.cpp: In function ‘int main()’:
    foo.cpp:88:26: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
        highestDays.push_back({ list[i].firstName, list[i].lastName });
                              ^
    foo.cpp:88:65: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
        highestDays.push_back({ list[i].firstName, list[i].lastName });
                                                                     ^
    foo.cpp:95:26: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
        highestDays.push_back({ list[i].firstName, list[i].lastName });
                              ^
    foo.cpp:95:65: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
        highestDays.push_back({ list[i].firstName, list[i].lastName });
                                                                     ^
    foo.cpp:102:29: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
        highestBalance.push_back({ list[i].firstName, list[i].lastName });
                                 ^
    foo.cpp:102:68: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
        highestBalance.push_back({ list[i].firstName, list[i].lastName });
                                                                        ^
    foo.cpp:111:29: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
        highestBalance.push_back({ list[i].firstName, list[i].lastName });
                                 ^
    foo.cpp:111:68: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
        highestBalance.push_back({ list[i].firstName, list[i].lastName });
                                                                        ^
    foo.cpp:130:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for (int i = 0; i < highestDays.size(); ++i)
                        ^
    foo.cpp:133:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       if (i == highestDays.size() - 1)tofile << endl;
             ^
    foo.cpp:139:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for (int i = 0; i < highestBalance.size(); ++i)
                        ^
    foo.cpp:142:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       if (i == highestBalance.size() - 1)tofile << endl;
             ^
    Well the first handful of messages relate to the very new syntax of initialiser lists which you're using.
    1. Check that your compiler actually supports these new features.
    2. Possibly enable the new features with appropriate command line options (or project->compiler settings in your IDE).

    And 3, add the -Wall flag to trap other weirdness as well.
    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.

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    OP: in the sample .txt file you've posted the last name of the first person (presumably Smith?) is missing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error after running graph command in program
    By Rivas in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2009, 10:17 AM
  2. Error running program in cmd - cygwin.dll missing -
    By JFonseka in forum C Programming
    Replies: 5
    Last Post: 08-27-2007, 12:12 PM
  3. Error when running a simple program
    By swishiat.com in forum C Programming
    Replies: 11
    Last Post: 12-14-2003, 06:53 AM
  4. Error when running program
    By cheeves in forum C Programming
    Replies: 6
    Last Post: 12-05-2003, 08:35 PM
  5. Weird error when running program
    By Rizage in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2002, 06:41 AM

Tags for this Thread