function problems

This is a discussion on function problems within the C++ Programming forums, part of the General Programming Boards category; I have just begun my intro to programming class, so please bear with me. When I set my precision I ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    1

    Unhappy function problems

    I have just begun my intro to programming class, so please bear with me. When I set my precision I receive an error of something like: cannot convert bool to std; I know it's got something to do with my first function. I thought the result of the function was stored as an integer, but apparently that's not right. How can I alter the function, given my limited knowledge, so I can setprecision. Any suggestions would be greatly appreciated. Thanks.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int getQuantity (int);
    float totalCost (int, const float);
    
    int main()
    {
    
        const float PC_COST = 995.00;
        const float MEM_COST = 89.95;
        const float ZIP_COST = 99.99;
        const float PRINT_COST = 119.50;
    
        int numberPCs, numberMem, numberZip, numberPrint,pc_quantity;
        int mem_quantity, zip_quantity, print_quantity;
        float  pc_total, mem_total, zip_total, print_total, totalOrder;
    
    
        cout<<"Enter the number of PC's:";
        pc_quantity= getQuantity(numberPCs);
        
        cout<<"Enter the number of memory cards:";
        mem_quantity= getQuantity(numberMem);
        
        cout<<"Enter the number of zip drives:";
        zip_quantity= getQuantity(numberZip);
        
        cout<<"Enter the number of printers:";
        print_quantity= getQuantity(numberPrint);
        
        cout<<"\n\n";
    
        cout<<"                 New Wave Computer Company "<<endl;
        cout<<"            ***********************************"<<endl;
        cout<<"Product"<<setw(25)<<" Cost each "<<setw(15)<<" Quantity "
            <<setw(15)<<" Total Cost "<<endl;
        cout<<"\n";
    
    
    
        pc_total= totalCost (pc_quantity, PC_COST);
        mem_total= totalCost (mem_quantity, MEM_COST);
        zip_total= totalCost (zip_quantity, ZIP_COST);
        print_total= totalCost (print_quantity, PRINT_COST);
        
        cout<<setiosflags(ios::fixed||ios::showpoint)<<setprecision(2);
        
        cout<<"Personal Computers"<<setw(11)<<PC_COST<<setw(15)<<pc_quantity
            <<setw(15)<<pc_total<<endl<<endl;
        cout<<"Memory Cards      "<<setw(11)<<MEM_COST<<setw(15)<<mem_quantity
            <<setw(15)<<mem_total<<endl<<endl;
        cout<<"Zip Drives        "<<setw(11)<<ZIP_COST<<setw(15)<<zip_quantity
            <<setw(15)<<zip_total<<endl<<endl;
        cout<<"Printers          "<<setw(11)<<PRINT_COST<<setw(15)<<print_quantity
            <<setw(15)<<print_total<<endl<<endl;
    
        totalOrder= pc_total + mem_total + zip_total + print_total;
    
        cout<<"Total Cost of Order"<<setw(40)<<totalOrder<<endl;
    
        return 0;
    
    }
    
    int getQuantity (int a)
    
    {
         int x;
         cin>>x;
         a=x;
         if (a >=0 && a <=100)
    
           return x;
         else
    
           return 0;
    
    }
    
    float totalCost (int b, const float a)
    
    {
    
        float totalCost = b * a;
    
           return totalCost;
    }

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Note that in function call:

    pc_quantity= getQuantity(numberPCs);
    you used variable numberPCs without defining it so results is unpredictable.
    So as in
    mem_quantity= getQuantity(numberMem);
    zip_quantity= getQuantity(numberZip);
    print_quantity= getQuantity(numberPrint);
    You must use initialized variables:

    Now is starting "Dirty Harry" on TV so I must be off!
    Good Luck!

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Code:
      pc_total= totalCost (pc_quantity, PC_COST);
        mem_total= totalCost (mem_quantity, MEM_COST);
        zip_total= totalCost (zip_quantity, ZIP_COST);
        print_total= totalCost (print_quantity, PRINT_COST);
    i really dont find any advantage from making new variables like
    pc_total , mem_total , zip_total , print_total as you make the program uses more memory without any use just waste of memory

    i recommend that you dont define these variables as u dont need it

    afterwards do the following
    Code:
    cout<<setiosflags(ios::fixed||ios::showpoint)<<setprecision(2);
        
        cout<<"Personal Computers"<<setw(11)<<PC_COST<<setw(15)<<pc_quantity
            <<setw(15)<<totalCost (pc_quantity, PC_COST)<<endl<<endl;
        cout<<"Memory Cards      "<<setw(11)<<MEM_COST<<setw(15)<<mem_quantity
            <<setw(15)<< totalCost (mem_quantity, MEM_COST)<<endl<<endl;
        cout<<"Zip Drives        "<<setw(11)<<ZIP_COST<<setw(15)<<zip_quantity
            <<setw(15)<<totalCost (zip_quantity, ZIP_COST)<<endl<<endl;
        cout<<"Printers          "<<setw(11)<<PRINT_COST<<setw(15)<<print_quantity
            <<setw(15)<<totalCost (print_quantity, PRINT_COST)<<endl<<endl;
    
        totalOrder= totalCost (pc_quantity, PC_COST)+  totalCost (mem_quantity, MEM_COST)+ totalCost (zip_quantity, ZIP_COST) + totalCost (print_quantity, PRINT_COST);
    Last edited by M_Ghani; 02-28-2004 at 05:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Problems with str.replace function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 02:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21