Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 461155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:56:47+00:00 2026-05-12T22:56:47+00:00

Howdy! I’m working on an old project from one of my programming courses and

  • 0

Howdy! I’m working on an old project from one of my programming courses and having some issues tracking down which line in my code is causing a syntax error.

When I attempt to compile the code, Visual Studio tells me that there is a syntax error towards the end of main on the line containing this function call:

sortData(carray, numRec, sortby);

I dont think that’s the case, because commenting out that call only moves the error to the next line of code.

I can’t tell what is causing the error and am hoping a veteran eye could help.

I’ve include all of the code as I suspect the error is being caused in one of the function calls.

ohbijuan

P.S. Also – Does anyone know if the compiler compiles top to bottom or whether it actually follows function calls during the compile process?

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

//customer struct
struct customer
{
    int id;
    string fname, lname;
    double totalQuantity, totalPurchases, totalProfit;
};

//Error Codes
const int INPUT_FILE_FAIL_NO_CONTINUE = 2;
const int INPUT_FILE_DATA_ERROR = 3;

//Global Constants
const int arraySize = 200;
const int numType = 5; //# of the types of coffee we currently offer

//Coffee prices per pound
const double colombWhole = 3.75;
const double colombRetail = 4.85;
const double konaWhole = 4.25;
const double konaRetail = 5.25;
const double ethiopWhole = 4.30;
const double ethiopRetail = 5.75;
const double jamaWhole = 5.25;
const double jamaRetail = 7.75;
const double brazWhole = 4.65;
const double brazRetail = 5.90;

//Function prototypes
int readData (ifstream &infile, customer carray[], int size);
//PRE:  The address of the ifstream object, the addres of the array of customer structs and the size of
//      the array is passed in
//POST: All of the customer data is read in AND the totalQuantity, totalPurchases and totalProfit for
//      each customer is calculated. The number of records read is returned.

void sortData (customer carray[], int recordcount, int sortfield);
//PRE:  The address of the array of customers, the number of customer records in the array and the field
//      on which to be sorted is passed into the function.
//POST: The array is sorted on the basis of the specified field in ascending order

int findMax (const customer carray[], int startRange, int recordcount, int sortfield);
//PRE:  The address of the array of customers, the field to sort on and a range of values is passed in
//POST: The address of the largest value is returned




int main()
{
    //Array of customer structs
    customer crecords[arraySize];

    //Initialize the members of our struct to zero
    for (int i = 0; i < arraySize; i++)
    {
        crecords[i].totalProfit = 0;
        crecords[i].totalPurchases = 0;
        crecords[i].totalQuantity = 0;
    }

    //Declare filestream objects
    ifstream ifile;
    ofstream ofile1, ofile2, ofile3;

    //user responses
    char pquit = 'Y';
    char specAnother; 
    int sortby;
    string ifilename;

    //Ask user for name of input file
    cout << "Please enter the name of the input file: " ;
    cin >> ifilename;

    //Attempt to open the input file
    ifile.open(ifilename.c_str());
    while (ifile.fail())
    {
        cout    << endl << "The input file could not be found. Would you like to specify " 
                << "another file?" << endl << "(Enter Y or N):";
        cin     >> specAnother; //Ask user if they want to specify another

        if (specAnother == 'N' || specAnother == 'n')
        {   
            exit(INPUT_FILE_FAIL_NO_CONTINUE);
        }
        else
        {
            cout    << endl << "Please enter the name of the new input file: " ;
            cin     >>  ifilename;
        }

        ifile.clear(); //Clear the flags, else the input file fail flag will perpetually return true
        ifile.open(ifilename.c_str());

    } 

    //File opened successfully, let's begin reading in data and also keep track of the # of 
    //records read
    int numRec = readData(ifile, crecords, arraySize); 
    cout << "Finished reading " << numRec << " records" << endl;


    do
    {
        //Ask user how they would like to sort the data for the report
        cout    << endl << "What would you like to sort on?" << endl;
        cout    << "1) Sort by POUNDS bought" << endl
                << "2) Sort by PURCHASE AMOUNT" << endl
                << "3) Sort by PROFIT" << endl;

        cin     >> sortby;
        if (sortby > 3 || sortby < 1)
        {
            cout    << "You entered an invalid sorting method, try again" << endl
                    << "Enter an option:";
            cin     >> sortby;
        }

        cout    << "You entered " << sortby << endl;
    }

    //Sort Data
    sortData(carray, numRec, sortby);
    return 0;
}

//Function Definitions
int readData (ifstream &infile, customer carray[], int size)
{
    int x = 0, coffeeType, quantity;
    double currentSale, internalCost, profitOnSale;
    while (infile >> carray[x].id && x < size)
    {
        infile >> carray[x].fname >> carray[x].lname;
        while (infile >> coffeeType && coffeeType != 0)
        {
            infile >> quantity;
            switch(coffeeType)
            {
            case 1:
                carray[x].totalQuantity += quantity;
                currentSale = quantity * colombRetail;
                carray[x].totalPurchases += currentSale;
                internalCost = quantity * colombWhole;
                profitOnSale = currentSale - internalCost;
                carray[x].totalProfit += profitOnSale;
                break;

            case 2:
                carray[x].totalQuantity += quantity;
                currentSale = quantity * konaRetail;
                carray[x].totalPurchases += currentSale;
                internalCost = quantity * konaWhole;
                profitOnSale = currentSale - internalCost;
                carray[x].totalProfit += profitOnSale;
                break;

            case 3:
                carray[x].totalQuantity += quantity;
                currentSale = quantity * ethiopRetail;
                carray[x].totalPurchases += currentSale;
                internalCost = quantity * ethiopWhole;
                profitOnSale = currentSale - internalCost;
                carray[x].totalProfit += profitOnSale;
                break;

            case 4:
                carray[x].totalQuantity += quantity;
                currentSale = quantity * jamaRetail;
                carray[x].totalPurchases += currentSale;
                internalCost = quantity * jamaWhole;
                profitOnSale = currentSale - internalCost;
                carray[x].totalProfit += profitOnSale;
                break;

            case 5:
                carray[x].totalQuantity += quantity;
                currentSale = quantity * brazRetail;
                carray[x].totalPurchases += currentSale;
                internalCost = quantity * brazWhole;
                profitOnSale = currentSale - internalCost;
                carray[x].totalProfit += profitOnSale;
                break;

            default:
                cout    <<"The input file contains an undeclared coffee type at record " << x 
                        <<"Program terminating!" << endl; 
                //return exit(INPUT_FILE_DATA_ERROR);

            }
        }

        x++;    //At this point, we have encountered our sentinel value of 0 that indicates the end of our
        //customer record. Let's move on to the next customer.        
    }

    return x;
}

int findMax (const customer carray[], int startRange, int recordcount, int sortfield)
{
    int maxLoc = startRange;
    switch(sortfield)
    {
    case 1:

        for (int i = startRange + 1; i <= recordcount; i++)
        {
                if (carray[maxLoc].totalQuantity < carray[i].totalQuantity)
                maxLoc = i;
        }

    case 2:

        for (int i = startRange + 1; i <= recordcount; i++)
        {
            if (carray[maxLoc].totalPurchases < carray[i].totalPurchases)
                maxLoc = i;
        }

    case 3:

        for (int i = startRange + 1; i <= recordcount; i++)
        {
            if (carray[maxLoc].totalProfit < carray[i].totalProfit)
                maxLoc = i;
        }    
    }

    return maxLoc;
}




void sortData (customer carray[], int recordcount, int sortfield)
{
    for (int i = 0; i < recordcount ; i++)
    {
        int max = findMax(carray, i, recordcount, sortfield);
        swap(carray[i], carray[max]);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:56:47+00:00Added an answer on May 12, 2026 at 10:56 pm

    Your problem is that you have this:

    do
    {
        // ...
    }
    

    without a trailing while (...); just before the call to sortData.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Howdy all. I'm working on a project that will (ideally) require the rendering of
Howdy! For a research project (in Java), I've got to find some way to
I would like to slice random letters from a string. Given s="howdy" I would
Howdy, I'm having a bit of an issue runnning a NAnt script that used
Howdy. I'm working on migrating an internal system to Django and have run into
Howdy all. I am trying to solve a problem which is apparently not uncommon
Howdy fellas, this one is gonna be a doozy: So for awhile I've been
Howdy, I am wanting to create a helper for an mvc project I am
Howdy -- Long time reader, first time questioner. :) I could really use some
Howdy guys, im having trouble finding help on creating a callback in certain situations.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.