TheSilverFox 36 Newbie Poster

Oh my gosh, wow, lol. I see it now. Thank you so much! lol...I can't believe I missed that...

TheSilverFox 36 Newbie Poster

Hello all!

I am typing in some code from an example in my book, but it shows the absolute value symbol, I believe, looking similar to " l l ", but it can't be just to lower case Ls spaced apart like I just made that...or is it? If not, can you tell me how to create this symbol in the compiler? Thank you!

TheSilverFox 36 Newbie Poster

Alright, I have read most of that post...I believe that the values need to be passed by reference...which would change the values...but does that mean that that is the only thing that I can return? When the program runs, it just completely skips the whole...display the results thing. Does it need another pause maybe? Let me go mess with it a bit more and I'll report back. Thank you so much for your help! This is really helping me learn, not just get it done, and I'm grateful for that =)


EDIT:

Hmm...I really am not sure what I need to change in the program to make it transfer the results and display them properly within that one function. I can't use another function, because a) that would just add to the confusion, and b) I would get points off the assignment for not doing it precisely as directed. I'm going to go back an reread that link again and see if I can make anything click...

TheSilverFox 36 Newbie Poster

OK, hold on. I added a type to the function, which I didn't know it needed until I read Ancient Dragon's post, and now it doesn't give me any errors, but it doesn't display the result of Calculate. Is that because double pay isn't part of the parameters?

Here's my code:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

double Calculate( int worked, double salary );

int main()
{
       int worked;
       double salary;
       double pay;
       
       
       cout << "Enter the hours worked: (-1 to end)" << endl;
       cin >> worked;
       cout << "Enter the salary: " << endl;
       cin >> salary;
       
       while( worked > 0 ){
             double Calculate( int worked, double salary );
              
              cout << "Enter the hours worked: (-1 to end)" << endl;
              cin >> worked;
              cout << "Enter the salary: " << endl;
              cin >> salary;
              }
              
       system("pause");
       return(0);
}

 double Calculate( int worked, double salary )
{
     double over;
     double half;
     double extra;
     double pay;

     
     if(worked > 40){
               over = worked - 40;
               half = salary * 1.5;
               extra = over * half;
               pay = (worked * salary) + extra;
               
               cout << "The pay is: " << "$" << pay << endl;
               }
               
     else
               pay = worked * salary;
               
               cout << "The pay is: " << "$" << pay << endl;
               
               return( pay );
               
}
TheSilverFox 36 Newbie Poster

Sorry for the double post....

TheSilverFox 36 Newbie Poster

Yeah, that's the error I'm getting, I know...but I'm not sure how to fix that...I'm totally new to C++ and I really don't understand it... How do I pass something to the function or from it?

The reason I put "void Calculate( double )" was because I was trying to model it from my text book, but I obviously didn't do it right...

Does the parameter need to be the variables I'm working with inside of it? Because I tried doing that, but it didnt work either...

TheSilverFox 36 Newbie Poster
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void Calculate( double );

int main()
{
       int worked;
       double salary;
       double pay;
       
       
       cout << "Enter the hours worked: (-1 to end)" << endl;
       cin >> worked;
       cout << "Enter the salary: " << endl;
       cin >> salary;
       
       while( worked > 0 ){
              Calculate( );
              
              cout << "Enter the hours worked: (-1 to end)" << endl;
              cin >> worked;
              cout << "Enter the salary: " << endl;
              cin >> salary;
              }
              
       system("pause");
       return(0);
}

void Calculate( double )
{
     double over;
     double half;
     double extra;
     double pay;
     int worked;
     double salary;
     
     if(worked > 40){
               over = worked - 40;
               half = salary * 1.5;
               extra = over * half;
               pay = (worked * salary) + extra;
               
               cout << "The pay is: " << "$" << pay << endl;
               }
               
     else
               pay = worked * salary;
               
               cout << "The pay is: " << "$" << pay << endl;
               
}

Ok, finally fixed the code tags. =P

Ancient Dragon commented: Good job -- You finally got code tags right :) +36
TheSilverFox 36 Newbie Poster

Wow, ouch, I'm sorry...I'll make sure I do that from now on...

TheSilverFox 36 Newbie Poster

Woo! The program works as planned! This is the final structure:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

void displayResults(int * counter);

int main()
{
    
    int sales;
    double salary;
    const int SIZE = 9;
    int counter[SIZE] = {0};
    
    
    
    
    cout << "Enter a sales amount: (negative to end)";
    cin >> sales;
    
    
    while (sales > 0){
         salary = (sales * .09) + 200;
    
         if ( (salary >= 200) && (salary < 300) )
              ++counter[ 0 ];
         else if ( (salary >= 300) && (salary < 400) )
              ++counter[ 1 ];
         else if ( (salary >= 400) && (salary < 500) )
              ++counter[ 2 ];
         else if ( (salary >= 500) && (salary < 600) )
              ++counter[ 3 ];
         else if ( (salary >= 600) && (salary < 700) )
              ++counter[ 4 ];
         else if ( (salary >= 700) && (salary < 800) )
              ++counter[ 5 ];
         else if ( (salary >= 800) && (salary < 900) )
              ++counter[ 6 ];
         else if ( (salary >= 900) && (salary < 1000) )
              ++counter[ 7 ];
         else
              ++counter[ 8 ];
    cout << "Enter a sales amount: (negative to end)";
    cin >> sales;
    }        
    
    displayResults(counter);

 
 system("pause");
 return 0;
}

void displayResults(int * counter)
{
   
     cout << " Range        Number" << endl;
     cout << "$200 - $299\t  " << counter[0] << endl;
     cout << "$300 - $399\t  " << counter[1] << endl;
     cout << "$400 - $499\t  " << counter[2] << endl;
     cout << "$500 - $599\t  " << counter[3] << endl; …
TheSilverFox 36 Newbie Poster

Ok, I did that, wildgoose. The compiler is now giving me an error that the line

int *pCnt = &counter[ n ];

shadows a parameter. I can't delete the declaration, because that's what links it to the original array... Also, I assumed that when you typed


void displayResults(int*pCnt, )

it should be


void displayResults(int*pCnt, int )

so that's what I made it. Was that wrong?

TheSilverFox 36 Newbie Poster

Ok. This is what the function reads now:

void displayResults(int*, int)
{
     int n;
     const int SIZE = 9;
     int counter[SIZE];
     int *pCnt = &counter[ n ];
     for (n=0; n < 9; n++)
         {
            counter[n] = n;
         }

     
     cout << " Range        Number" << endl;
     cout << "$200 - $299\t  " << counter[ 0 ] << endl;
     cout << "$300 - $399\t  " << counter[ 1 ] << endl;
     cout << "$400 - $499\t  " << counter[ 2 ] << endl;
     cout << "$500 - $599\t  " << counter[ 3 ] << endl;
     cout << "$600 - $699\t  " << counter[ 4 ] << endl;
     cout << "$700 - $799\t  " << counter[ 5 ] << endl;
     cout << "$800 - $899\t  " << counter[ 6 ] << endl;
     cout << "$900 - $999\t  " << counter[ 7 ] << endl;
     cout << "Over $1000 \t  " << counter[ 8 ] << endl;
}

The program works with no compiler errors, but the results show just 0-8 in sequential order for the Number corresponding to each range. I believe that is because somewhere, a pointer isn't pointing to the original int counter[9]; I created...I'm not sure which parts (if any) should be omitted or edited...

Thank you very much for your quick responses and help! I greatly appreciate it!!

TheSilverFox 36 Newbie Poster

I tried adding that into my code:

void displayResults( )
{

int counter[ 9 ];
int n;
int i;

i = counter[ n ] ;
int *pCnt = &counter[ n ];

cout << " Range Number" << endl;
cout << "$200 - $299\t " << &counter[ 0 ] << endl;
cout << "$300 - $399\t " << &counter[ 1 ] << endl;
cout << "$400 - $499\t " << &counter[ 2 ] << endl;
cout << "$500 - $599\t " << &counter[ 3 ] << endl;
cout << "$600 - $699\t " << &counter[ 4 ] << endl;
cout << "$700 - $799\t " << &counter[ 5 ] << endl;
cout << "$800 - $899\t " << &counter[ 6 ] << endl;
cout << "$900 - $999\t " << &counter[ 7 ] << endl;
cout << "Over $1000 \t " << &counter[ 8 ] << endl;
}

and it just caused my .exe to crash. I wasn't 100% sure how to add it in properly though. Do I need to declare n better? Where do I use i? Thank you in advance...

TheSilverFox 36 Newbie Poster

This is what I need to do:

Use a single-subscripted array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000 or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):
a) $200-$299
b) $300-$399
c) $400-$499
d) $500-$599
e) $600-$699
f) $700-$799
g) $800-$899
h) $900-$999
i) $1000 and over
Summarize the results in tabular format

This is what I've got:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

void displayResults( );

int main()
{
    int sales;
    double salary;
    int counter[ 9 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    
    
    
    
    cout << "Enter a sales amount: (negative to end)";
    cin >> sales;
    
    
    while (sales > 0){
         salary = (sales * .09) + 200;
    
         if ( (salary >= 200) && (salary < 300) )
              ++counter[ 0 ];
         else if ( (salary >= 300) && (salary < 400) )
              ++counter[ 1 ];
         else if ( (salary >= 400) && (salary < 500) )
              ++counter[ 2 ];
         else if ( (salary >= 500) && (salary < …