954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

User-Defined Function - part 2

Hey everyone, this is a big one.

The question has asked that I define functions and then write the function main to test the functions I wrote. I keep getting the following error at my first cout in main:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

I'm not sure what this is saying.
In addition, I need help with defining two of my functions and writing code to test them. They are as follows:

1. Write the definition of the function nextChar that sets the value of z to the next character stored in z.

2. Write the definition of the function funcOne that prompts the user to input a number. The function then changes the value of x to 2 times the old value of x plus the value of y minus the value entered by the user.

Now, with #2 I'm pretty sure I have the function defined right, though I don't know how to test it. #1 though, I have no clue how to define that function, though I did attempt it =/.

Could someone look through my code, and let me know if they notice any huge mistakes? This is our second chapter on user-defined functions, so I'm still learning about call by reference and whatnot. I have commented my code minimaly to assist you in finding the parts I'm having a tough time with.

Thanks guys! :mrgreen:

code:

<pre><code>#include &lt;iostream&gt;
#include &lt;iomanip&gt;
using namespace std ;

void initialize ( int&amp; , int&amp; , char&amp; ) ;
void getHoursRate ( double&amp; , double&amp; ) ;
double payCheck ( double&amp; , double&amp; ) ;
void printCheck ( double , double, double ) ;
int main()
{
    int x ;
    int y ;
    int w ;
    
    char z ;

    double rate ;
    double hours ;
    double amount ;

    cout &lt;&lt; initialize( x , y , z ) &lt;&lt; endl ;            //error is here
    cout &lt;&lt; getHoursRate() &lt;&lt; endl ;
    cout &lt;&lt; payCheck( hours , rate ) &lt;&lt; endl ;
    printCheck( hours, rate, amount ) &lt;&lt; endl ;
    cout &lt;&lt; funcOne( w, x, y ) ;    //confusion starts here
    cout &lt;&lt;    
}

void initialize ( int&amp; x, int&amp; y, char&amp; z )
{
    x = y = 0 ;
    z = ' ' ;
}

void getHoursRate ( double&amp; hours , double&amp; rate )
{
    cout &lt;&lt; &quot;Hours worked:  &quot; &lt;&lt; endl ;
    cin &gt;&gt; hours ;
    cout &lt;&lt; &quot;Rate:  &quot; &lt;&lt; endl ;
    cin &gt;&gt; rate ;
}

double payCheck ( double&amp; hours, double&amp; rate )
{
    double pay ;
    
    int ot ;

    if ( (hours - 40 &lt;= 0 ) )
        pay = rate * hours ;
    else
    {
        ot = hours - 40 ;
        pay = (40 * rate) + (ot * rate * 1.5) ;
    }
    return pay ;
}

void printCheck ( double hours , double rate , double pay )
{
    cout &lt;&lt; setprecision(2) &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setfill('.') &lt;&lt; left ;
    cout &lt;&lt; &quot;Hours&quot; &lt;&lt; setw(15) &lt;&lt; &quot;Rate&quot; &lt;&lt; setw(15) &lt;&lt; &quot;Amount Due&quot; &lt;&lt; endl &lt;&lt; endl ;
    cout &lt;&lt; hours &lt;&lt; setw(15) &lt;&lt; &quot;$&quot; &lt;&lt; rate &lt;&lt; setw(15) &lt;&lt; &quot;$&quot; &lt;&lt; pay &lt;&lt; endl ;
}

void funcOne ( double in, int x, int y )        //number 2 in my post
{
    cout &lt;&lt; &quot;Input a value:  &quot; &lt;&lt; endl ;
    cin &gt;&gt; in ;

    x = x * x + ( y - in ) ;
}

char nextChar ( char ch )                    //number 1 in my post
{
    ch = ch++ ;
}</code></pre>
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
 

Don't have time to check over the rest of the code, but I can tell you what is wrong here:

cout << initialize( x , y , z ) << endl ;            //error is here


initialize returns nothing, so cout has nothing to print. If you want to initalize the variables, try calling the function in a separate line instead of mixing it in with the cout call.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

2. Write the definition of the function funcOne that prompts the user to input a number. The function then changes the value of x to 2 times the old value of x plus the value of y minus the value entered by the user. Now, with #2 I'm pretty sure I have the function defined right, though I don't know how to test it. code:

<pre><code>void funcOne ( double in, int x, int y )        //number 2 in my post
{
    cout &lt;&lt; &quot;Input a value:  &quot; &lt;&lt; endl ;
    cin &gt;&gt; in ;
 
    x = x * x + ( y - in ) ;                //  &lt;----Wrong
}</code></pre>


With this function you are squaring x instead of doubling it (ie use 2*x instead of x*x). To test it, simply do some simple math in your head, using different values of x and y....then implement that in your code...if your code gives you different answers, then, unless you did the math wrong yourself, you know there is a problem with your code...

ft3ssgeek
Junior Poster
126 posts since Mar 2007
Reputation Points: 9
Solved Threads: 7
 

As for part 1, I believe the instructions are referring to c-strings...how much do you know about c-strings?

ft3ssgeek
Junior Poster
126 posts since Mar 2007
Reputation Points: 9
Solved Threads: 7
 

Ok, I've modified my code, and am getting an error that says:

error C2563: mismatch in formal parameter list

This is at the same line with the comment //error is here

I've called the void functions outside of a cout, and have put cout statements inside of the function to test them. What am I doing wrong? =/

<pre><code>#include &lt;iostream&gt;
#include &lt;iomanip&gt;
using namespace std ;
void initialize ( int&amp; , int&amp; , char&amp; ) ;
void getHoursRate ( double&amp; , double&amp; ) ;
double payCheck ( double&amp; , double&amp; ) ;
void printCheck ( double , double, double ) ;
void funcOne ( doulbe, int, int ) ;
void nextChar ( char ) ;
int main()
{
    int x ;
    int y ;
    int w ;
    
    char z ;
    double rate ;
    double hours ;
    double amount ;
    initialize( x , y , z ) &lt;&lt; endl ;            //error is here
    getHoursRate() &lt;&lt; endl ;
    cout &lt;&lt; payCheck( hours , rate ) &lt;&lt; endl ;
    printCheck( hours, rate, amount ) &lt;&lt; endl ;
    funcOne( w, x, y ) ;    //confusion starts here
 nextChar ( z ) ;
}
void initialize ( int&amp; x, int&amp; y, char&amp; z )
{
    x = y = 0 ;
    z = ' ' ;
    cout &lt;&lt; &quot;Inside initialize-function:  &quot; &lt;&lt; endl ;
    cout &lt;&lt; &quot;The values of 'x', 'y' and 'z', respectively are:  &quot; x &lt;&lt; &quot;\t&quot; &lt;&lt; y &lt;&lt; &quot;\t&quot; &lt;&lt; z &lt;&lt; endl &lt;&lt; endl ;
}
void getHoursRate ( double&amp; hours , double&amp; rate )
{
    cout &lt;&lt; &quot;Hours worked:  &quot; &lt;&lt; endl ;
    cin &gt;&gt; hours ;
    cout &lt;&lt; &quot;Rate:  &quot; &lt;&lt; endl ;
    cin &gt;&gt; rate ;
    cout &lt;&lt; &quot;Inside getHoursRate-function:  &quot; &lt;&lt; endl ;
    cout &lt;&lt; &quot;The values of 'hours' and 'rate', respectively are:  &quot; &lt;&lt; hours &lt;&lt; &quot;\t&quot; &lt;&lt; rate &lt;&lt; endl &lt;&lt; endl ;
}
double payCheck ( double&amp; hours, double&amp; rate )
{
    double pay ;
    
    int ot ;
    if ( (hours - 40 &lt;= 0 ) )
        pay = rate * hours ;
    else
    {
        ot = hours - 40 ;
        pay = (40 * rate) + (ot * rate * 1.5) ;
    }
    return pay ;
}
void printCheck ( double hours , double rate , double pay )
{
    cout &lt;&lt; setprecision(2) &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setfill('.') &lt;&lt; left ;
    cout &lt;&lt; &quot;Hours&quot; &lt;&lt; setw(15) &lt;&lt; &quot;Rate&quot; &lt;&lt; setw(15) &lt;&lt; &quot;Amount Due&quot; &lt;&lt; endl &lt;&lt; endl ;
    cout &lt;&lt; hours &lt;&lt; setw(15) &lt;&lt; &quot;$&quot; &lt;&lt; rate &lt;&lt; setw(15) &lt;&lt; &quot;$&quot; &lt;&lt; pay &lt;&lt; endl ;
}
void funcOne ( double in, int x, int y )        //number 2 in my post
{
    cout &lt;&lt; &quot;Input a value:  &quot; &lt;&lt; endl ;
    cin &gt;&gt; in ;
    x = x * 2 + ( y - in ) ;
    cout &lt;&lt; &quot;Inside funcOne-function:  &quot; &lt;&lt; endl ;
    cout &lt;&lt; &quot;The value of 'x' is:  &quot; &lt;&lt; x &lt;&lt; endl &lt;&lt; endl ;
}
char nextChar ( char ch )                    //number 1 in my post
{
    cout &lt;&lt; &quot;Inside nextChar-function:  &quot; &lt;&lt; endl ;
    cout &lt;&lt; &quot;Initial character stored in 'ch' is:  &quot; &lt;&lt; ch &lt;&lt; endl ;
    ch = ch++ ;
    cout &lt;&lt; &quot;The character stored in 'ch' after incriment is:  &quot; &lt;&lt; ch &lt;&lt; endl &lt;&lt; endl ;
}</code></pre>
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
 

initialize( x , y , z ) << endl ;
You can't do this in C++ esp when initialize doesn't return anything and even if it did, you aren't specifying the stream to which the output should be redirected.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
As for part 1, I believe the instructions are referring to c-strings...how much do you know about c-strings?



i don't =/

Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
 
As for part 1, I believe the instructions are referring to c-strings...how much do you know about c-strings?


I don't think so. Although, I don't know what the instructions mean, either.

Can you give us an example of the operation?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Just an update, thanks to everyone who helped. Here's the finalized code. This one is solved :D

<pre><code>/*****************************************************
* COSC 230 - Structured Programming
* Chapter 7:  Programming Exercise 1
* Apr. 4, 2007
*
* Discription:
*    blah
******************************************************/

//header files
#include &lt;iostream&gt;
#include &lt;iomanip&gt;
using namespace std ;

//function prototypes
void initialize ( int&amp; , int&amp; , char&amp; ) ;
void getHoursRate ( double&amp; , double&amp; ) ;
double payCheck ( double&amp; , double&amp; ) ;
void printCheck ( double , double, double ) ;
void funcOne ( double, int, int ) ;
void nextChar ( char ) ;

int main()
{
    int x ;
    int y ;
    int w ;
    
    char z ;

    double rate ;
    double hours ;
    double amount ;

    initialize( x , y , z ) ;    //test initialize-func.
    getHoursRate(hours, rate) ;    //test initialize-func.
    amount = payCheck( hours, rate ) ;        //initialize amount to paycheck-func.
    cout &lt;&lt; &quot;*** Inside payCheck-function ***&quot; &lt;&lt; endl &lt;&lt; payCheck( hours , rate ) &lt;&lt; endl ;    //test payCheck-func.
    printCheck( hours, rate, amount ) ;    //test printCheck-func.
    funcOne( w, x, y ) ;    //test funcOne-func.
    nextChar ( z ) ;    //test nextChar-func.
}

void initialize ( int&amp; x, int&amp; y, char&amp; z )
{
    x = y = 0 ;
    z = ' ' ;

    cout &lt;&lt; &quot;*** Inside initialize-function ***&quot; &lt;&lt; endl ;
    cout &lt;&lt; &quot;The values of 'x' and 'y', respectively are:  &quot; &lt;&lt; x &lt;&lt; &quot; and &quot; &lt;&lt; y &lt;&lt; endl ;
    cout &lt;&lt; &quot;The character stored in z is:  &quot; &lt;&lt; z &lt;&lt; &quot;(space character)&quot; &lt;&lt; endl &lt;&lt; endl ;
}//end void initialize

void getHoursRate ( double&amp; hours , double&amp; rate )
{
    cout &lt;&lt; &quot;Hours worked:  &quot; &lt;&lt; flush ;
    cin &gt;&gt; hours ;
    cout &lt;&lt; &quot;Rate:  &quot; &lt;&lt; flush ;
    cin &gt;&gt; rate ;

    cout &lt;&lt; endl &lt;&lt; &quot;*** Inside getHoursRate-function ***&quot; &lt;&lt; endl &lt;&lt; endl ;
    cout &lt;&lt; &quot;The values of 'hours' and 'rate', respectively are:  &quot; &lt;&lt; hours &lt;&lt; &quot;\t&quot; &lt;&lt; rate &lt;&lt; endl &lt;&lt; endl ;
}//end getHoursRate

double payCheck ( double&amp; hours, double&amp; rate )
{
    double pay ;
    
    int ot ;

    if ( (hours - 40 &lt;= 0 ) )
        pay = rate * hours ;
    else
    {
        ot = hours - 40 ;
        pay = (40 * rate) + (ot * rate * 1.5) ;
    }
    return pay ;
}//end double payCheck

void printCheck ( double hours , double rate , double amount )
{
    cout &lt;&lt; &quot;*** Inside printCheck-function ***&quot; &lt;&lt; endl ;
    cout &lt;&lt; setprecision(2) &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setfill('.') ;
    cout &lt;&lt; &quot;Hours&quot; &lt;&lt; setw(20) &lt;&lt; right &lt;&lt; &quot;Rate&quot; &lt;&lt; setw(25) &lt;&lt; right &lt;&lt; &quot;Amount Due&quot; &lt;&lt; endl ;
    cout &lt;&lt; hours &lt;&lt; setw(15) &lt;&lt; &quot;$&quot; &lt;&lt; rate &lt;&lt; setw(15) &lt;&lt; &quot;$&quot; &lt;&lt; amount &lt;&lt; endl &lt;&lt; endl ;
}//end void printCheck

void funcOne ( double in, int x, int y )
{
    cout &lt;&lt; &quot;Input a value:  &quot; &lt;&lt; flush ;
    cin &gt;&gt; in ;
    cout &lt;&lt; endl ;

    x = x * 2 + ( y - in ) ;

    cout &lt;&lt; &quot;*** Inside funcOne-function ***&quot; &lt;&lt; endl ;
    cout &lt;&lt; &quot;The value of 'x' is:  &quot; &lt;&lt; x &lt;&lt; endl &lt;&lt; endl ;
}//end void funcOne

void nextChar ( char ch )
{
    cout &lt;&lt; &quot;*** Inside nextChar-function ***&quot; &lt;&lt; endl ;
    cout &lt;&lt; &quot;Initial character stored in 'ch' is:  &quot; &lt;&lt; ch &lt;&lt; endl ;

    ch = ch++ ;

    cout &lt;&lt; &quot;The character stored in 'ch' after increment is:  &quot; &lt;&lt; ch &lt;&lt; endl &lt;&lt; endl ;
}//end nextChar</code></pre>
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You