• 0

What's wrong with my C++ code?


Question

The entire program ends after int main(), but I want it to continue forward to the max/min functions...

#include <iostream>
using namespace std;


int a, b, c;
int main ()

{

	a = 0;
	b = 0;
	c = 0;

	cout << "Please enter three integers." << endl;
	cout << "Integer one: " << endl;
  cin >> a; 
	cout << "Integer two: " << endl;
  cin >> b;
	cout << "Integer three: " << endl;
  cin >> c;

	cout << "Your three input values were: " << a << ", " << b << ", and " << c << "." << endl;

	return (a, b, c);
}

	int maxofthree (int a, int b, int c)
{

	if (a > b)
  if (c > a)
 	 cout << "The largest value is " << c << "." << endl;
  else
 	 cout << "The largest value is " << a << "." << endl;
	if (b > a)
  if (c > b)
 	 cout << "The largest value is " << c << "." << endl;
  else
 	 cout << "The largest value is " << b << "." << endl;

  return (a, b, c);
}

  int minofthree (int a, int b, int c)
{

	if (a < b)
  if (c < a)
 	 cout << "The smallest value is " << c << "." << endl;
  else
 	 cout << "The smallest value is " << a << "." << endl;
	if (b < a)
  if (c < b)
 	 cout << "The smallest value is " << c << "." << endl;
  else
 	 cout << "The smallest value is " << b << "." << endl;

  return 0;
}

Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 0

In the main function you want to call the max and min functions.

So after you print out the three numbers in the main function, add:

maxofthree(a, b, c);
minofthree(a, b, c);

Also, in your main, just return 0 and I really don't think you need to return anything from the max and min functions either so make them void for the time being and take out the return statements. By the way, you can only return one 'value' in a return statement, whether its an integer, char etc...

And do what Zirus1701 suggested by declaring the variables a, b and c inside the main function.

Edited by D-FENS
Link to comment
Share on other sites

  • 0
1. You can't return 3 integers from a functions or can you? I'm not sure about this.

I dont think you can in the way the current coding is,

The only way I can think of passing back more than one value is to return an array of objects/values

Link to comment
Share on other sites

  • 0
maxofthree (a, b, c);
	minofthree (a, b, c);

	return 0;
}

  WHAT DO I PUT HERE?
{

	if (a > b)
  if (c > a)
 	 cout << "The largest value is " << c << "." << endl;
  else
 	 cout << "The largest value is " << a << "." << endl;
	if (b > a)
  if (c > b)
 	 cout << "The largest value is " << c << "." << endl;
  else
 	 cout << "The largest value is " << b << "." << endl;
}

  WHAT DO I PUT HERE?
{

	if (a < b)
  if (c < a)
 	 cout << "The smallest value is " << c << "." << endl;
  else
 	 cout << "The smallest value is " << a << "." << endl;
	if (b < a)
  if (c < b)
 	 cout << "The smallest value is " << c << "." << endl;
  else
 	 cout << "The smallest value is " << b << "." << endl;

}

Link to comment
Share on other sites

  • 0

You are getting a redefinition error because you are calling the functions maxofthree and minofthree before they have been definited. So you can either define some prototypes for these functions or move the main function below the 2 functions in the code.

I've pm'd you some small changes to your code...still a few things that could be made cleaner, but that'll all come together as you work on it.

Link to comment
Share on other sites

  • 0

#include <iostream>
using namespace std;


int a, b, c;
int main ()

{

a = 0;
b = 0;
c = 0;

cout << "Please enter three integers." << endl;
cout << "Integer one: " << endl;
 cin >> a; 
cout << "Integer two: " << endl;
 cin >> b;
cout << "Integer three: " << endl;
 cin >> c;

cout << "Your three input values were: " << a << ", " << b << ", and " << c << "." << endl;

maxofthree(a,b,c); // Here are the function calls.
minofthree(a,b,c);

return 0;
}

void maxofthree (int a, int b, int c) // Why you were returning a,b,c im not sure you output them in the functions
{

if (a > b)
 if (c > a)
 ?cout << "The largest value is " << c << "." << endl;
 else
 ?cout << "The largest value is " << a << "." << endl;
if (b > a)
 if (c > b)
 ?cout << "The largest value is " << c << "." << endl;
 else
 ?cout << "The largest value is " << b << "." << endl;

 return;
}

void minofthree (int a, int b, int c)
{

if (a < b)
 if (c < a)
 ?cout << "The smallest value is " << c << "." << endl;
 else
 ?cout << "The smallest value is " << a << "." << endl;
if (b < a)
 if (c < b)
 ?cout << "The smallest value is " << c << "." << endl;
 else
 ?cout << "The smallest value is " << b << "." << endl;

return;
}

I'm pretty sure that should be better, I don't know if it actually works the way you had everything going, but those are how you do the function calls.

Link to comment
Share on other sites

  • 0
you probably want to move the int a, b, c; defs into main(). i think c++ can handle the local vars with the same name, but it's kinda bad form.

I think he was making them global variables?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.