User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,074 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,124 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 4413 | Replies: 3
Reply
Join Date: Mar 2004
Posts: 6
Reputation: ElectroBoy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ElectroBoy ElectroBoy is offline Offline
Newbie Poster

Function that returns void

  #1  
May 5th, 2004
I am struggling with creating a function that is void. I can write the code inline without a function as follows :

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
string answer;
answer = "Yes";
while ( answer[0] == 'Y' || answer[0] == 'y' ) { // continue processing
// Announce purpose of program:
cout << "This program finds roots of a quadratic equation." << endl;
// Input the coefficients:
cout << "Enter the coefficients a, b, and c, separated by spaces:>";
double a;
double b;
double c;
cin >> a >> b >> c;
// Compute the discriminant:
double discriminant = b*b - 4*a*c;
// Compute and output the roots:
if ( discriminant > 0 ) {
cout << "Two real roots: "
<< setprecision (4) << (0 - b + sqrt(discriminant)) / (2 * a)
<< " and "
<< setprecision (4) << (0 - b - sqrt(discriminant)) / (2 * a)
<< "." << endl;
} else if ( discriminant < 0 ) {
cout << "Two complex roots, where the real part is "
<< setprecision (4) << (0 - b) / (2 * a) << endl
<< "and the imaginary part is plus or minus "
<< setprecision (3) << abs(sqrt(0 - discriminant) / (2 * a)) << "." << endl;
} else { // if ( discriminant == 0 || discriminant == -0 )
cout << "Two identical real roots, both "
<< setprecision (4) << (0 - b) / (2 * a)
<< "." << endl;
} // else
// function main
cout << "How about that!" << endl;
cout << "Do you want to continue (Y/N)? ";
cin >> answer;
}
return(0);
}


I can write the function as below and it works, however it runs the functions output and then gives me a 0. :

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;
// Function
int Calc(double a, double b, double c)
{
// Compute the discriminant:
double discriminant = b*b - 4*a*c;
// Compute and output the roots:
if ( discriminant > 0 || discriminant == 0 ) {
cout << "Two real roots: "
<< setprecision (4) << (0 - b + sqrt(discriminant)) / (2 * a)
<< " and "
<< setprecision (4) << (0 - b - sqrt(discriminant)) / (2 * a)
<< "." << endl;
} else {
cout << "In this case, the roots of the equation are imaginary." << endl
<< "We will not perform any calculations" << endl;
}
return (0);
}

int main()
{
// Declare local variables
string answer;
double a;
double b;
double c;
// Assign value to answer string
answer = "Yes";

while ( answer[0] == 'Y' || answer[0] == 'y' ) { // continue processing


// Statement and input the coefficients:
cout << "Determine the roots (x1 and x2) of the equation using the quadratic formulas: " << endl;
cout << "Enter the coefficients for a, b, and c, separated by spaces:";
cin >> a >> b >> c;
cout << Calc(a,b,c);
cout << "How about that!" << endl;
cout << "Do you want to continue (Y/N)? ";
cin >> answer;
}
return(0);
}


WHICH IS JUST A SET UP SO YOU UNDERSTAND I HAVE BEEN WORKING ON MY HOMEWORK. My question is about using void functions and the error I get when I try to write one. Here is the problem code :

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;
// Function
void Calc(double a, double b, double c)
{
// Compute the discriminant:
double discriminant = b*b - 4*a*c;
// Compute and output the roots:
if ( discriminant > 0 || discriminant == 0 ) {
cout << "Two real roots: "
<< setprecision (4) << (0 - b + sqrt(discriminant)) / (2 * a)
<< " and "
<< setprecision (4) << (0 - b - sqrt(discriminant)) / (2 * a)
<< "." << endl;
} else {
cout << "In this case, the roots of the equation are imaginary." << endl
<< "We will not perform any calculations" << endl;
}
}

int main()
{
// Declare local variables
string answer;
double a;
double b;
double c;
// Assign value to answer string
answer = "Yes";

while ( answer[0] == 'Y' || answer[0] == 'y' ) { // continue processing


// Statement and input the coefficients:
cout << "Determine the roots (x1 and x2) of the equation using the quadratic formulas: " << endl;
cout << "Enter the coefficients for a, b, and c, separated by spaces:";
cin >> a >> b >> c;
cout << Calc(a,b,c);
cout << "How about that!" << endl;
cout << "Do you want to continue (Y/N)? ";
cin >> answer;
}
return(0);
}


Here is the error on compile :

C:\Program Files\Utilities\Microsoft Visual Studio\MyProjects\Project3\proj3.cpp(45) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
Error executing cl.exe.


---------------

Any insight into using a function that returns void would be appreciated. Otherwise I go back to writing the code without a function.

AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2004
Posts: 76
Reputation: infamous is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: SUPER simple question

  #2  
May 6th, 2004
if it returns void, then you can't use the return value b/c there isn't one! you are trying to "cout << Calc()" , that means print out the return value of function Calc(), but it has no return value since the return type is void.
Reply With Quote  
Join Date: Mar 2004
Posts: 1,514
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Rep Power: 10
Solved Threads: 48
Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: Function that returns void

  #3  
May 6th, 2004
Hi,

I think what infamous is saying is that you need to just call Calc(a,b,c); and be done with it... do not try to cout << Calc(a,b,c).

[code]

//cout << Calc(a,b,c);
Calc(a,b,c);
cout << "How about that!" << endl;
cout << "Do you want to continue (Y/N)? ";

[\CODE]

The 0 that you are seeing is because if your function returns cleanly, it will send a 0 back to the calling function (main called Calc.... Calc ran fine, and it sends you a 0 to let you know it ran fine).

Good Luck.

Christian
Last edited by kc0arf : May 6th, 2004 at 8:17 pm. Reason: code did not format properly.
Reply With Quote  
Join Date: Mar 2004
Posts: 6
Reputation: ElectroBoy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ElectroBoy ElectroBoy is offline Offline
Newbie Poster

Re: Function that returns void

  #4  
May 6th, 2004
I understand the error I am making and it makes sense. Its funny how once something clicks in your head it seems so simple. My understanding was incomplete. Thank you for helping me get this point Infamous and kc0arf.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC