Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Stuck on C++ Function, need some guidence
New Posts  All Forums:Forum Nav:

Stuck on C++ Function, need some guidence

post #1 of 15
Thread Starter 
My CS teacher is pretty picky so I have to call a function from another function,if you notice in my findCityData function I'm supposed to be calling my searchFunction function so it can perform the binary search and return the subscript that I have named "position". I'm having trouble getting it to do all of this so if someone could look at my code and give me some suggestions that would be great.
Code:





#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Function prototypes
void fillArray (string city[], double budget[], double revenue[], int &NumCities);
void printarrays (string city[], double budget[], double revenue[], int NumCities);
void FindHiBudget (string city[], double budget[], int NumCities);
void AboveAvgRevenue (string city[], double revenue[], int NumCities);
void OverBudget (string city[], double budget[], double revenue[], int NumCities);
int searchFunction (string city[], int NumCities, string target);
void findCityData (string city[], double budget[], double revenue[], int NumCities);
//ADD YOUR PROTOTYPES HERE

const int ArraySize = 30;

int main ()
{
string city[ArraySize];
double budget[ArraySize];
double revenue[ArraySize];
int NumCities;


//We will now call on all of these functions
fillArray (city, budget, revenue, NumCities);
cout << left << fixed << setprecision(0);
printarrays (city, budget, revenue, NumCities);

   //ADD THE CALL TO YOUR FindHiBudget function
   FindHiBudget (city, budget, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR AboveAvgRevenue function
AboveAvgRevenue (city, revenue, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR OverBudget function
OverBudget (city, budget, revenue, NumCities);
    cout << "======================================================" << endl;



findCityData (city, budget, revenue, NumCities)
cout << "======================================================" << endl;



return 0;

}

void fillArray (string city[], double budget[], double revenue[], int &NumCities)
{
ifstream indata;
NumCities = 0;

//Now we will open the file

indata.open("lab1.txt");
if (! indata)
{
cerr << "Error" << endl;

}


getline(indata, city[NumCities]);

//We will now run a loop to get al of the data from the file and assign it to the proper array
while (!indata.eof()&& NumCities < ArraySize)
{
indata >> budget[NumCities];
indata >> revenue[NumCities];

indata.ignore();
        NumCities++;
getline(indata, city[NumCities]);

}
return;
}

void printarrays (string city[], double budget[], double revenue[], int NumCities)
{

cout << setw(20) << "City" << setw(20) << "Revenue" << setw(20) << "Budget" << endl;
cout << setw(20) << "----" << setw(20) << "-------" << setw(20) << "------" << endl;


//We will now print al of the data in a table
for (int i = 0; i < NumCities; i++)
{
cout << setw(20) << city[i] << setw(20) << revenue[i] << setw(20) << budget[i] << endl;
}
}

//========================================================
// ADD YOUR FindHiBudget function here
//========================================================
void FindHiBudget (string city[], double budget[], int NumCities)
{
int i = 0;
int HiBudget = 0;
int temp = 0;

for (i = 0; i < NumCities; i++)
{
if (budget[i] > HiBudget)
{
HiBudget = budget[i];
temp = i;
}


}
cout << "======================================================" << endl;
cout << "The city with the highest budget is " << city[temp] << endl;
}



//========================================================
// ADD YOUR AboveAvgRevenue function here
//========================================================
void AboveAvgRevenue (string city[], double revenue[], int NumCities)
{
double average = 0;
int sum = 0;
int i = 0;

for (i = 0; i < NumCities; i++)
{
sum += revenue[i];
}

average = sum / NumCities;
cout << "The average revenue is " << average << endl;
cout << "The cities with the above average revenue are: " << endl;

for (i = 0; i < NumCities; i++)
{
if (revenue[i] > average)
cout << city[i] << endl;
}
}







//========================================================
// ADD YOUR OverBudget function here
//========================================================
 void OverBudget (string city[], double budget[], double revenue[], int NumCities)
{
int i = 0;

cout << "The cities that are over budget are: " << endl;

for (i = 0; i < NumCities; i++)
{
if (budget[i] > revenue[i])
cout << city[i] << " by " << budget[i] - revenue[i] << endl;
}
}



void findCityData (string city[], double budget[], double revenue[], int NumCities)
{ 
string target;
cout << "Enter the name of the city or Q to quit:";
cin >> target;
while (target !=-1)
{
searchFunction (city, NumCities, target);
cout << city[position] << " has a budget of " << budget[position] << " and revenue totaling " << revenue[position] << endl;
cout << "Enter the name of the city or Q to quit:";

}




}












int searchFunction (string city[], int NumCities, string target)
{


int first = 0,           // First array element
       last = NumCities - 1, // Last array element
       middle,              // Mid point of search
       position = -1;       // Position of search value
   bool found = false;      // Flag

   while (!found && first <= last)
   {
      middle = (first + last) / 2;  // Calculate mid point
      if (city[middle] == target)   // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (city[middle] > target) // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;          // If value is in upper half
   }

   cout << position << endl;
   return position;
}







Code:
Here's what the output is SUPPOSED to look like. 
City                Revenue             Budget
----                -------             ------
Baltic              38000200            35000000
Baltimore           89000300            58500000
Bexley              28481063            25478963
Bloomdale           38410275            58412475
Blue Ash            35898625            25896325
Bluffton            64859332            47856932
Bolivar             33661245            49858745
Boston Heights      91701052            35698452
Botkins             97898024            75895324
Bowling Green       78955154            86952354
Bradford            89590323            96587423
Bradner             19690445            25687445
Bratenahl           14460032            21456932
Brecksville         62701723            45698523
Bremen              56988531            57985231
Brooklyn            85325278            95321478
Brooklyn Heights    72151754            52147854
Brookville          27569478            23565478
Brunswick           98478665            85474565
======================================================
The city with the highest budget is Bradford
======================================================
The average revenue is 59148500
The cities with above average revenue are:
Baltimore
Bluffton
Boston Heights
Botkins
Bowling Green
Bradford
Brecksville
Brooklyn
Brooklyn Heights
Brunswick
======================================================
The cities over budget are:
Bloomdale by 20002200
Bolivar by 16197500
Bowling Green by 7997200
Bradford by 6997100
Bradner by 5997000
Bratenahl by 6996900
Bremen by 996700
Brooklyn by 9996200
======================================================
Enter the name of the city or Q to quit:Baltimore
Baltimore has a budget of 58500000 and revenue totaling 89000300
Enter the name of the city or Q to quit:r
r not found.
Enter the name of the city or Q to quit:Q
===============After Sorting By Budget================
City                Revenue             Budget
----                -------             ------
Bratenahl           14460032            21456932
Brookville          27569478            23565478
Bexley              28481063            25478963
Bradner             19690445            25687445
Blue Ash            35898625            25896325
Baltic              38000200            35000000
Boston Heights      91701052            35698452
Brecksville         62701723            45698523
Bluffton            64859332            47856932
Bolivar             33661245            49858745
Brooklyn Heights    72151754            52147854
Bremen              56988531            57985231
Bloomdale           38410275            58412475
Baltimore           89000300            58500000
Botkins             97898024            75895324
Brunswick           98478665            85474565
Bowling Green       78955154            86952354
Brooklyn            85325278            95321478
Bradford            89590323            96587423
==============After Sorting By Revenue ===============
City                Revenue             Budget
----                -------             ------
Bratenahl           14460032            21456932
Bradner             19690445            25687445
Brookville          27569478            23565478
Bexley              28481063            25478963
Bolivar             33661245            49858745
Blue Ash            35898625            25896325
Baltic              38000200            35000000
Bloomdale           38410275            58412475
Bremen              56988531            57985231
Brecksville         62701723            45698523
Bluffton            64859332            47856932
Brooklyn Heights    72151754            52147854
Bowling Green       78955154            86952354
Brooklyn            85325278            95321478
Baltimore           89000300            58500000
Bradford            89590323            96587423
Boston Heights      91701052            35698452
Botkins             97898024            75895324
Brunswick           98478665            85474565
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
post #2 of 15
inb4movetoprogramminghelpsection
2009-2010 Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x4 555BE @ 3.96GHz MSI 790FX-GD70 2x Sapphire 5850s 2x4GB Corsair XMS3 DDR3-1333 
Hard DriveOptical DriveOSMonitor
2x OCZ VertexII RAID0 + 2xSamsung F1s RAID0 Lite-On BD-ROM/DVD-RW WinXP Pro x64/Win 7 Pro x64/Kubuntu 9.10 x64 Acer G24 Ferrari 
KeyboardPowerCaseMouse
merc SteelSeries Enermax Evolution 1050W Azza Solano 1000 Microsoft Sidewinder X5 
  hide details  
Reply
2009-2010 Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x4 555BE @ 3.96GHz MSI 790FX-GD70 2x Sapphire 5850s 2x4GB Corsair XMS3 DDR3-1333 
Hard DriveOptical DriveOSMonitor
2x OCZ VertexII RAID0 + 2xSamsung F1s RAID0 Lite-On BD-ROM/DVD-RW WinXP Pro x64/Win 7 Pro x64/Kubuntu 9.10 x64 Acer G24 Ferrari 
KeyboardPowerCaseMouse
merc SteelSeries Enermax Evolution 1050W Azza Solano 1000 Microsoft Sidewinder X5 
  hide details  
Reply
post #3 of 15
Thread Starter 
Quote:
Originally Posted by jck;12236782 
inb4movetoprogramminghelpsection

I thought about putting it there but there's a school help section in off topic, even though I didn't post it there either.
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
post #4 of 15
You'd get a lot better help if you posted it in the coding section.
Sandy the waffle
(16 items)
 
Data server
(14 items)
 
HTPC
(14 items)
 
CPUMotherboardGraphicsGraphics
i7 2600k Gigabyte Z68X-UD5-B3 eVGA GTX580 eVGA GTX580 
RAMHard DriveHard DriveCooling
Corsair Vengence Samsung 840 Evo WD1001FALS Phobya UC-2 
OSMonitorPowerCase
Windows 10 Pro 22" LG HX1000 Corsair 900D 
Mouse
DeathAdder 
CPUMotherboardGraphicsRAM
i3 3220 S1200BTSR onboard KVR1333D3E9SK2/8G 
Hard DriveHard DriveHard DriveHard Drive
Patriot Inferno 60gb 8x WD5002ALX 4x ST2000DM001 2x WD10EALS 2x WD10EARS 
Hard DriveOSPowerCase
WD5000AAKS Windows Server 2008 R2 Antec TP550 RPC-4116 
OtherOther
Areca ARC-1222 Dell Perc5/i 
CPUMotherboardGraphicsRAM
i5 750 Gigabyte H55N-USB3 Asus 5770 Direct CU II 8GB Kingston HyperX 
Hard DriveCoolingOSPower
250GB Momentus XT Accelero S1 Plus Windows 7 SilverStone ST45SF 
Case
Silverstone FT03B-MINI 
  hide details  
Reply
Sandy the waffle
(16 items)
 
Data server
(14 items)
 
HTPC
(14 items)
 
CPUMotherboardGraphicsGraphics
i7 2600k Gigabyte Z68X-UD5-B3 eVGA GTX580 eVGA GTX580 
RAMHard DriveHard DriveCooling
Corsair Vengence Samsung 840 Evo WD1001FALS Phobya UC-2 
OSMonitorPowerCase
Windows 10 Pro 22" LG HX1000 Corsair 900D 
Mouse
DeathAdder 
CPUMotherboardGraphicsRAM
i3 3220 S1200BTSR onboard KVR1333D3E9SK2/8G 
Hard DriveHard DriveHard DriveHard Drive
Patriot Inferno 60gb 8x WD5002ALX 4x ST2000DM001 2x WD10EALS 2x WD10EARS 
Hard DriveOSPowerCase
WD5000AAKS Windows Server 2008 R2 Antec TP550 RPC-4116 
OtherOther
Areca ARC-1222 Dell Perc5/i 
CPUMotherboardGraphicsRAM
i5 750 Gigabyte H55N-USB3 Asus 5770 Direct CU II 8GB Kingston HyperX 
Hard DriveCoolingOSPower
250GB Momentus XT Accelero S1 Plus Windows 7 SilverStone ST45SF 
Case
Silverstone FT03B-MINI 
  hide details  
Reply
post #5 of 15
while(target != -1)
what are you trying to achieve with this?
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
post #6 of 15
Code:



#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Function prototypes
void fillArray (string city[], double budget[], double revenue[], int &NumCities);
void printarrays (string city[], double budget[], double revenue[], int NumCities);
void FindHiBudget (string city[], double budget[], int NumCities);
void AboveAvgRevenue (string city[], double revenue[], int NumCities);
void OverBudget (string city[], double budget[], double revenue[], int NumCities);
int searchFunction (string city[], int NumCities, string target);
void findCityData (string city[], double budget[], double revenue[], int NumCities);
//ADD YOUR PROTOTYPES HERE

const int ArraySize = 30;

int main ()
{
string city[ArraySize];
double budget[ArraySize];
double revenue[ArraySize];
int NumCities;


//We will now call on all of these functions
fillArray (city, budget, revenue, NumCities);
cout << left << fixed << setprecision(0);
printarrays (city, budget, revenue, NumCities);

   //ADD THE CALL TO YOUR FindHiBudget function
   FindHiBudget (city, budget, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR AboveAvgRevenue function
AboveAvgRevenue (city, revenue, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR OverBudget function
OverBudget (city, budget, revenue, NumCities);
    cout << "======================================================" << endl;



findCityData (city, budget, revenue, NumCities);
cout << "======================================================" << endl;



return 0;

}

void fillArray (string city[], double budget[], double revenue[], int &NumCities)
{
ifstream indata;
NumCities = 0;

//Now we will open the file

indata.open("lab1.txt");
if (! indata)
{
cerr << "Error" << endl;

}


getline(indata, city[NumCities]);

//We will now run a loop to get al of the data from the file and assign it to the proper array
while (!indata.eof()&& NumCities < ArraySize)
{
indata >> budget[NumCities];
indata >> revenue[NumCities];

indata.ignore();
        NumCities++;
getline(indata, city[NumCities]);

}
return;
}

void printarrays (string city[], double budget[], double revenue[], int NumCities)
{

cout << setw(20) << "City" << setw(20) << "Revenue" << setw(20) << "Budget" << endl;
cout << setw(20) << "----" << setw(20) << "-------" << setw(20) << "------" << endl;


//We will now print al of the data in a table
for (int i = 0; i < NumCities; i++)
{
cout << setw(20) << city[i] << setw(20) << revenue[i] << setw(20) << budget[i] << endl;
}
}

//========================================================
// ADD YOUR FindHiBudget function here
//========================================================
void FindHiBudget (string city[], double budget[], int NumCities)
{
int i = 0;
int HiBudget = 0;
int temp = 0;

for (i = 0; i < NumCities; i++)
{
if (budget[i] > HiBudget)
{
HiBudget = budget[i];
temp = i;
}


}
cout << "======================================================" << endl;
cout << "The city with the highest budget is " << city[temp] << endl;
}



//========================================================
// ADD YOUR AboveAvgRevenue function here
//========================================================
void AboveAvgRevenue (string city[], double revenue[], int NumCities)
{
double average = 0;
int sum = 0;
int i = 0;

for (i = 0; i < NumCities; i++)
{
sum += revenue[i];
}

average = sum / NumCities;
cout << "The average revenue is " << average << endl;
cout << "The cities with the above average revenue are: " << endl;

for (i = 0; i < NumCities; i++)
{
if (revenue[i] > average)
cout << city[i] << endl;
}
}







//========================================================
// ADD YOUR OverBudget function here
//========================================================
 void OverBudget (string city[], double budget[], double revenue[], int NumCities)
{
int i = 0;

cout << "The cities that are over budget are: " << endl;

for (i = 0; i < NumCities; i++)
{
if (budget[i] > revenue[i])
cout << city[i] << " by " << budget[i] - revenue[i] << endl;
}
}



void findCityData (string city[], double budget[], double revenue[], int NumCities)
{ 
int pos;
string target;
cout << "Enter the name of the city or Q to quit:";
cin >> target;
while(target.size() > 0)
{
pos = searchFunction (city, NumCities, target);
cout << city[pos] << " has a budget of " << budget[pos] << " and revenue totaling " << revenue[pos] << endl;
cout << "Enter the name of the city or Q to quit:";

}




}












int searchFunction (string city[], int NumCities, string target)
{


int first = 0,           // First array element
       last = NumCities - 1, // Last array element
       middle,              // Mid point of search
       position = -1;       // Position of search value
   bool found = false;      // Flag

   while (!found && first <= last)
   {
      middle = (first + last) / 2;  // Calculate mid point
      if (city[middle] == target)   // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (city[middle] > target) // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;          // If value is in upper half
   }

   cout << position << endl;
   return position;
}
compiled in visual c++ express with no errors
what i changed:
Code:
int pos;
pos = searchFunction (city, NumCities, target);
cout << city[pos] << " has a budget of " << budget[pos] << " and revenue totaling " << revenue[pos] << endl;
since you return a value, not a variable.
Code:
while(target.size() > 0)
since it's a string.
Edited by newbie1911 - 2/2/11 at 12:57pm
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
post #7 of 15
Thread Starter 
Quote:
Originally Posted by newbie1911;12237080 
while(target != -1)
what are you trying to achieve with this?
Exit the loop, I don't really know. Good question.

Quote:
Originally Posted by newbie1911;12237142 
Code:



#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Function prototypes
void fillArray (string city[], double budget[], double revenue[], int &NumCities);
void printarrays (string city[], double budget[], double revenue[], int NumCities);
void FindHiBudget (string city[], double budget[], int NumCities);
void AboveAvgRevenue (string city[], double revenue[], int NumCities);
void OverBudget (string city[], double budget[], double revenue[], int NumCities);
int searchFunction (string city[], int NumCities, string target);
void findCityData (string city[], double budget[], double revenue[], int NumCities);
//ADD YOUR PROTOTYPES HERE

const int ArraySize = 30;

int main ()
{
string city[ArraySize];
double budget[ArraySize];
double revenue[ArraySize];
int NumCities;


//We will now call on all of these functions
fillArray (city, budget, revenue, NumCities);
cout << left << fixed << setprecision(0);
printarrays (city, budget, revenue, NumCities);

   //ADD THE CALL TO YOUR FindHiBudget function
   FindHiBudget (city, budget, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR AboveAvgRevenue function
AboveAvgRevenue (city, revenue, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR OverBudget function
OverBudget (city, budget, revenue, NumCities);
    cout << "======================================================" << endl;



findCityData (city, budget, revenue, NumCities);
cout << "======================================================" << endl;



return 0;

}

void fillArray (string city[], double budget[], double revenue[], int &NumCities)
{
ifstream indata;
NumCities = 0;

//Now we will open the file

indata.open("lab1.txt");
if (! indata)
{
cerr << "Error" << endl;

}


getline(indata, city[NumCities]);

//We will now run a loop to get al of the data from the file and assign it to the proper array
while (!indata.eof()&& NumCities < ArraySize)
{
indata >> budget[NumCities];
indata >> revenue[NumCities];

indata.ignore();
        NumCities++;
getline(indata, city[NumCities]);

}
return;
}

void printarrays (string city[], double budget[], double revenue[], int NumCities)
{

cout << setw(20) << "City" << setw(20) << "Revenue" << setw(20) << "Budget" << endl;
cout << setw(20) << "----" << setw(20) << "-------" << setw(20) << "------" << endl;


//We will now print al of the data in a table
for (int i = 0; i < NumCities; i++)
{
cout << setw(20) << city[i] << setw(20) << revenue[i] << setw(20) << budget[i] << endl;
}
}

//========================================================
// ADD YOUR FindHiBudget function here
//========================================================
void FindHiBudget (string city[], double budget[], int NumCities)
{
int i = 0;
int HiBudget = 0;
int temp = 0;

for (i = 0; i < NumCities; i++)
{
if (budget[i] > HiBudget)
{
HiBudget = budget[i];
temp = i;
}


}
cout << "======================================================" << endl;
cout << "The city with the highest budget is " << city[temp] << endl;
}



//========================================================
// ADD YOUR AboveAvgRevenue function here
//========================================================
void AboveAvgRevenue (string city[], double revenue[], int NumCities)
{
double average = 0;
int sum = 0;
int i = 0;

for (i = 0; i < NumCities; i++)
{
sum += revenue[i];
}

average = sum / NumCities;
cout << "The average revenue is " << average << endl;
cout << "The cities with the above average revenue are: " << endl;

for (i = 0; i < NumCities; i++)
{
if (revenue[i] > average)
cout << city[i] << endl;
}
}







//========================================================
// ADD YOUR OverBudget function here
//========================================================
 void OverBudget (string city[], double budget[], double revenue[], int NumCities)
{
int i = 0;

cout << "The cities that are over budget are: " << endl;

for (i = 0; i < NumCities; i++)
{
if (budget[i] > revenue[i])
cout << city[i] << " by " << budget[i] - revenue[i] << endl;
}
}



void findCityData (string city[], double budget[], double revenue[], int NumCities)
{ 
int pos;
string target;
cout << "Enter the name of the city or Q to quit:";
cin >> target;
while(target.size() > 0)
{
pos = searchFunction (city, NumCities, target);
cout << city[pos] << " has a budget of " << budget[pos] << " and revenue totaling " << revenue[pos] << endl;
cout << "Enter the name of the city or Q to quit:";

}




}












int searchFunction (string city[], int NumCities, string target)
{


int first = 0,           // First array element
       last = NumCities - 1, // Last array element
       middle,              // Mid point of search
       position = -1;       // Position of search value
   bool found = false;      // Flag

   while (!found && first <= last)
   {
      middle = (first + last) / 2;  // Calculate mid point
      if (city[middle] == target)   // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (city[middle] > target) // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;          // If value is in upper half
   }

   cout << position << endl;
   return position;
}
compiled in visual c++ express with no errors
what i changed:
Code:
int pos;
pos = searchFunction (city, NumCities, target);
cout << city[pos] << " has a budget of " << budget[pos] << " and revenue totaling " << revenue[pos] << endl;
since you return a value, not a variable.
Code:
while(target.size() > 0)
since it's a string.

Thank you, I'll give that a try.


Edit: It worked, I threw a cin << target; at the end of the while loop so it wasn't an endless loop.
Edited by skatingrocker17 - 2/2/11 at 2:19pm
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
post #8 of 15
Quote:
Originally Posted by skatingrocker17;12238023 
Exit the loop, I don't really know. Good question.




Thank you, I'll give that a try.


Edit: It worked, I threw a cin << target; at the end of the while loop so it wasn't an endless loop.

didn't notice that, only paid attention to the errors.
in that case it will exit when you enter a blank string. if you wan't it to quit on Q then replace it with
Code:
while(*target.c_str() != 'Q')
or
Code:
while(*target.c_str() != 'Q' || *target.c_str() != 'q')
to be a bit more user friendly.
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
post #9 of 15
Thread Starter 
When I enter Q I get...
Quote:
Enter the name of the city or Q to quit:Q
has a budget of 2463187436706012200000000000000000 and revenue totaling 0
Enter the name of the city or Q to quit:
It still seems to be entering the loop.
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
post #10 of 15
Quote:
Originally Posted by skatingrocker17;12239326 
When I enter Q I get...

It still seems to be entering the loop.

weird. it worked for me.. anyways try
Code:
while(target.compare("Q") != 0 || target.compare("q") != 0)
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Stuck on C++ Function, need some guidence