I am having trouble with this simple program to display three numbers entered from highest, medium, lowest. Specifically, with the ouput_result void function. Stuck, and not sure what I am doing wrong. Fairly new to C++, this is my first course in C++. Any help would be appreciated.

#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;

void get_user_input(int & a, int & b, int & c);
void output_result (int a, int b, int c);
int larger(int a , int b);
int largestOfThree(int a, int b, int c);
int medium (int a, int b);
int middleOfThree (int a, int b, int c);
int smaller (int a , int b);
int smallestOfThree (int a, int b, int c);


int main(int argc, char *argv[])
{

    int a, b, c;

    get_user_input(a, b, c);

    output_result ('largst', a, b, c);
    output_result ('medium', a, b, c);
    output_result ('smallest', a, b, c);

    system("PAUSE");
    return EXIT_SUCCESS;
}


void get_user_input(int & a, int & b, int & c)
{
   cout << "Please enter three numbers:";
   cin >> a >> b >> c;

}

void output_result(string task, int a, int b, int c)
{
     if (task == "largest") cout << "The largest number is "" <<endl;
     if (task == "medium") cout << "The medium number is "" <<endl;
     if (task == "smallest") cout << "The smallest number is "" <<endl;
}     

int largestOfThree(int a, int b, int c)
{
  return larger(a, larger(b, c));   
}
int larger(int a , int b)
{
  if (a >= b)
    return a;
  else
    return b;

}

int smallestOfThree (int a, int b, int c)
{
  return smaller(a, smaller (b, c));
}
int smaller (int a, int b)
{
 if (a <= b)
   return a;
 else 
   return b;
}

int middleOfThree(int a, int b, int c) 
      {
           if ( a <= b   && a >= c) 
              return a;
           else
              if (a <= c && a >= b) 
                  return a;


           if ( b <=  a && b >= c )
               return b;
           else
                 if (b <= c && b >= a)
                       return b;


           if ( c <= a && c >= b)
               return c;
           else
               if ( c <= b && c >= a)
                     return c;
      }



}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

There are many problems with this code: But the bit that should be stopping you compile and debug it is in the call: output_result ('largst', a, b, c); You see two problems first that single quotes are used for a single character e.g. 'x' etc. You need a string. So change it to "largest". [Another minor issue: you have spelt largest incorrectly in your code].

Next you have not declared you function correctly. The prototype is void output_result(int,int,int); I think you need this void output_result(std::string,int,int,int); [Note: a reference e.g. const std::string& would be better which you obviously have covered since you are using them in get_input.]

Finally, you have a couple of "" in your output_result part. They are wrong obviously. [If you actually want a quote in the output, then you need to "escape" it e.g. "this is a \" quote in a string" .]

After that you will need to debug it, get the functions for smaller/greater called etc.


When you post again, can you please use code tags and give a little bit of the compiler error messages.

Edited 6 Years Ago by StuXYZ: n/a

Thanks for the feedback. I really appreciate it. After making the changes, I received the following compiler error messages:

project7-1.cpp: In function `int main(int, char**)':
project7-1.cpp:24: error: invalid conversion from `const char*' to `int'

project7-1.cpp:8: error: too many arguments to function `void output_result(int, int, int)'
project7-1.cpp:24: error: at this point in file
project7-1.cpp:25: error: invalid conversion from `const char*' to `int'
project7-1.cpp:8: error: too many arguments to function `void output_result(int, int, int)'
project7-1.cpp:25: error: at this point in file
project7-1.cpp:26: error: invalid conversion from `const char*' to `int'

project7-1.cpp:8: error: too many arguments to function `void output_result(int, int, int)'
project7-1.cpp:26: error: at this point in file
project7-1.cpp: At global scope:

project7-1.cpp:40: error: expected identifier before '&' token

project7-1.cpp:40: error: expected `,' or `...' before "int"
project7-1.cpp:41: error: ISO C++ forbids declaration of `parameter' with no type
project7-1.cpp: In function `void output_result(std::string, int&)':
project7-1.cpp:42: error: `task' undeclared (first use this function)
project7-1.cpp:42: error: (Each undeclared identifier is reported only once for each function it appears in.)

project7-1.cpp: At global scope:
project7-1.cpp:97: error: expected declaration before '}' token

make.exe: *** [project7-1.o] Error 1

Execution terminated

Sorry but can you post your current code [using code tags] please, I have no idea what line 24 is for example, and I don't exactly know what changes you made.

This article has been dead for over six months. Start a new discussion instead.