Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Passing data between functions?
New Posts  All Forums:Forum Nav:

C++ Passing data between functions? - Page 2

post #11 of 36
Quote:
Originally Posted by Timlander View Post
No that is not an infinite loop. It read from the file perfectly fine before. I am getting to bed now as well.

EDIT: Oh wait a second! Duh I think I know whats up here. lol. I initially did the program on my laptop, then moved to my desktop. The sauce names files is only on my laptop. ROFL so yea that would make it an endless loop since it cant find the sauce names file! Ok tomorrow I will try again haha.
Yea I just realized you were looking for a file that wasn't there

I was pretty exhausted last night.
post #12 of 36
Thread Starter 
Quote:
Originally Posted by xtascox View Post
Yea I just realized you were looking for a file that wasn't there

I was pretty exhausted last night.
Yea. So i tried running the program earlier and it works but I guess there is something wrong with my pointers because the results when I go to display them are way wrong. You see any problems with the pointers?
post #13 of 36
Thread Starter 
Still need help figuring out why the pointers are not working....
post #14 of 36
I'll take a look when I get home from work.
post #15 of 36
Thread Starter 
Quote:
Originally Posted by xtascox View Post
I'll take a look when I get home from work.
Neh that's fine its due in 2 hours anyways...I just wont be able to use the pointers.
post #16 of 36
Thread Starter 
Well here is my revised and finish program. Don't mind the comments...they were required unfortunately....heck only 40% of the grade is the working program...the rest is all documentation, plans and other filler BS.....


Code:

#include<iostream>

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

void InputPrice(double prices[]);
void InputCases(int sold[]);
int DisplayReport(double prices[], int sold[]);
int Quit();

int main()
{
    //declarations
    int MenuAnswer=0;
    int sold[5];
    double prices[5];
    
    //display the Salsa menu to the user
    while(true)
    {
        system("CLS");
        cout << "\    *Welcome to the Chips and Salsa main menu.\
\    *Please choose from one of the following options:\
\
"
             << "\    \    1. Input Price\
"
             << "\    \    2. Input Cases Sold\
"
             << "\    \    3. Display Report\
"
             << "\    \    4. Quit\
";
             
        //put user answer into variable to use with switch/case
        cin >> MenuAnswer;
        system("CLS");
        
        //switch/case to use the appropriate function depending on which 
        //choice the user selected
        switch(MenuAnswer)
        {
              case 1: InputPrice(prices);
                      break;
              
              case 2: InputCases(sold);
                      break;
              
              case 3: DisplayReport(prices, sold);
                      break;
              
              case 4: Quit();
                      break;
              
              default: cout << "That is not a valid input, please try again...";   
              
        }   
    }     
        
    
}

int Quit()
{
    //make sure the user wants to quit
    string ans;
    system("CLS");
    cout << "\    Are you sure you want to quit?";
    cin >> ans;
    
    //if statement to decide weather to end the program or return to menu
    if (ans=="y"||ans=="yes"||ans=="Y"||ans=="Yes"||ans=="YES")
    {
        exit(0);
    }
    else
        return 0;
        
}   

void InputPrice(double prices[])
{
     //variable declarations
    string names[5];
    int i=0;
    ifstream inFile;
    
    //open the salsa name file
    inFile.open("SalsaNames.txt");
    
    //while loop to get all names until the end of the file is reached
    while( !inFile.eof()) 
    {
           getline(inFile, names[i]);   
           i++;
    }
    //asking the user for salsa pricing
    cout << "Please enter the price of each case of salsa:\
";
    //for loop to display the salsa names and get the prices for each one
    for(int k=0; k<i; k++)
    {
            cout << names[k] << ": ";
            cin >> prices[k];
            
    }
    system("PAUSE");
    //file must be closed
    inFile.close();
    
}

void InputCases(int sold[])
{
     //variable declaration
    string names[5];
    int i=0;
    ifstream inFile;
    
    //opening the salsa names file
    inFile.open("SalsaNames.txt");
    
    //while loop to get all names until the end of the file is reached
    while( !inFile.eof()) 
    {
           getline(inFile, names[i]);   
           i++;
    }
    //asking the user for cases sold
    cout << "Please enter the number of salsa cases sold:\
";
    //for loop to display the salsa names and get the cases sold for each one
    for(int k=0; k<i; k++)
    {
            cout << names[k] << ": ";
            cin >> sold[k];
            
    }
    system("PAUSE");
    //file must be closed
    inFile.close();
    
}

int DisplayReport(double prices[], int sold[]) 
{   
    //variable declarations  
    string names[5], namesOne[5];
    int i=0, totalSold=0, count=0, highest=0, lowest=0;
    double totalPrice=0;
    string highestOne, lowestOne;
    
    ifstream inFile;
    
    //open the salsa name file
    inFile.open("SalsaNames.txt");
    
    //while loop to get all names until the end of the file is reached
    while( !inFile.eof()) 
    {
           getline(inFile, names[i]);   
           i++;
    }   
    //file must be closed 
    inFile.close();
     
    //top of the sales report display
    cout << "           ***Salsa Sales Report***\
\
";
    cout << "Name              Cases Sold          Price Per Case\
";
    cout << "____________________________________________________\
";
    
    //for loop to fill the table with the corresponding names and numbers
    for (int i=0; i<5; i++)
    {
        cout << setw(6) << left << names[i] << right << setw(21) << sold[i] << setw(21) << setprecision(2) << fixed << right << "$" << prices[i] << endl << endl;
    
    }
    
    //calculations
    totalSold=sold[0]+sold[1]+sold[2]+sold[3]+sold[4];
    totalPrice=(prices[0]*sold[0])+(prices[1]*sold[1])+(prices[2]*sold[2])+(prices[3]*sold[3])+(prices[4]*sold[4]);
    
    //for loop to determin the highest value entered 
    highest=sold[0];
    for (count=1; count < 5; count++)
    {
        if ( sold[count] > highest)
           { 
              highest=sold[count];
              highestOne=names[count];
           }
    }
       
    //for loop to determin the lowest value entered
    lowest=sold[0];
    for (count=1; count < 5; count++)
    {
        if ( sold[count] < lowest)
           {
              lowest=sold[count];
              lowestOne=names[count];
           }
    }
    
    //display of the totals
    cout << "                        ---                 --------\
";
    cout << "Total Cases Sold:       " << setw(3) << totalSold << setw(19) << "Total Price: $" << setw(3) << setprecision(2) << fixed << totalPrice << endl;
    
    //display of the highest and lowest sold
    cout << "\
Highest Seller: " << highestOne;
    cout << "\
Lowest Seller: "<< lowestOne << endl;
    
    system("PAUSE");
    
    return 0;

post #17 of 36
Get in a habit of documentation because when you are actually coding and looking at other people's code, it's often MUCH easier to understand the code when you have the original programmer's comment as context.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 930 ASUS P6X58D Premium EVGA GTX 670 FTW Sig2 Corsair 3x2GB 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 2TB Samsung BD Windows 7 Ultimate 64bit Samsung PX2370 
PowerCase
Corsair AX750 CoolerMaster HAF 932 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 930 ASUS P6X58D Premium EVGA GTX 670 FTW Sig2 Corsair 3x2GB 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 2TB Samsung BD Windows 7 Ultimate 64bit Samsung PX2370 
PowerCase
Corsair AX750 CoolerMaster HAF 932 
  hide details  
Reply
post #18 of 36
If you want, post the code with the pointers or redirect me to where it is posted (I dunno which code is updated or not) and I'll see if I can spot any errors that would cause erroneous printing.

I'll run it and compile it myself and see if I can fix the problem, even if you can't turn it in, it'll be helpful to know why it wasn't working.
Edited by Tomiger - 1/27/11 at 1:53pm
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
post #19 of 36
Thread Starter 
Quote:
Originally Posted by nevets1219 View Post
Get in a habit of documentation because when you are actually coding and looking at other people's code, it's often MUCH easier to understand the code when you have the original programmer's comment as context.
Yea my professor sais that all the time. I mean it does make valid sense...it is just a big pain when I hardly have any time to write the program.

Quote:
Originally Posted by Tomiger View Post
If you want, post the code with the pointers or redirect me to where it is posted (I dunno which code is updated or not) and I'll see if I can spot any errors that would cause erroneous printing.

I'll run it and compile it myself and see if I can fix the problem, even if you can't turn it in, it'll be helpful to know why it wasn't working.
The one with pointers is THIS post.

Also by chance would you know how I could restart a loop I have going? I cant think of any ways to restart this loop. I need to add an if statement for data validation. something like if (!prices[k]>0) restart loop now else continue

Code:

//for loop to display the salsa names and get the prices for each one
    for(int k=0; k<i; k++)
    {
            cout << names[k] << ": ";
            cin >> prices[k];
            
    } 
post #20 of 36
Code:
if( ! (prices[k] > 0)
   k = -1;
else
   continue
To restart the loop, you essentially need to reset the counter variable, k. If you get it to -1 when that condition is true, the loop will then increment k as it always does, and then k will be 0.

I'll look over the other code now. So what exactly was being printed when you say it was garbage? I don't have the input files, so I can't test it directly.

By looking at the display function, are *prices and *sold arrays? If so, you can't just print out the array by saying

Code:
cout << prices;
If they are supposed to be arrays, you need to do a for loop to print out the contents of each array. Also, you'll need to pass in the size of each array so the for loop knows when to stop

Code:
for(int i =0; i < arraySize; i++)
   cout << prices[i];
If *prices and *sold are meant to be pointers to a int variable, then you need to consider the following: When you just say cout << prices, you're printing out the address of the prices variable, not the value to which it is pointing to. For example:

Code:
int i = 10;
int *prt = &i;
cout << prt;
This prints: 0xbffffa08

prt is a pointer that holds the address of i. Just printing the pointer variable like that will print an address. To successfully print it, I need to say

Code:
cout << *prt;

Edited by Tomiger - 1/27/11 at 2:29pm
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  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 › C++ Passing data between functions?