Transfer cout statements to main

Hello!
I'm trying to write a program that uses selection sort, linear search, and binary search. I am not allowed to use cout statements in functions other than main however.

I've been trying to find a workaround, but I cannot think of a way to incorporate cout statements in main as they rely on conditional statements on those specific functions (selection sort, linear search, and binary search are separate functions).

Below is my code. I've bolded the parts I'm having trouble with. The cout statements that I bold are only supposed to execute when an empID match is found, so I'm not sure how to write it separately from the search function into the main function.

Guidance would be much appreciated. Thank you!!

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <sstream>

void foo()
{
    std::cout << "from foo: this is line one of the output\n" ;
    // ...
    std::cout << "from foo: this is line two of the output\n" ;
    //..
    std::cout << "from foo: this is line three of the output\n" ;
}

int main()
{
    // create a string buffer which can be used for output
    std::stringbuf sbuf( std::ios::out ) ;

    // ask cout to send the output to the string buffer
    const auto old_buf = std::cout.rdbuf( std::addressof(sbuf) ) ;

    foo() ; // call the function

    std::cout.rdbuf(old_buf) ; // restore the original stdout buffer

    std::cout << "*** we are back in main now ****\n\n" ;
    
    // retrieve the output from the string buffer
    const std::string foo_output = sbuf.str() ;

    // and print it out in main
    std::cout << "output from foo:\n------------\n" << foo_output ;
}

http://coliru.stacked-crooked.com/a/4979b8c07d35e824
Thank you for the reply, JLBorges! Though I'm not sure I understand the string buffer stuff. If the foo function has cout lines, isn't it making output statements through that function and not main when called?
Don't pass all that pointless garbage into your search function.
All you need is empID and ID.
The "found" variable is worse than useless. Ditch it and simply return if you find the ID.
Use a for loop; that's what they are for.
Have the search pass back the index that it found, or -1 if it didn't find it.
Print the crap out in main.
1
2
3
4
5
6
int linearSearch(long empID[], long ID) {
    for (int i = 0; i < SIZE; i++)
        if (empID[i] == ID)
            return i;
    return -1;
}

Last edited on
Thanks tpb! But I think I'm supposed to keep the bool for function type. How do you suggest I should go about this?
Last edited on
But I think I'm supposed to keep the bool for function type

Why do you think that?

I suppose you also think you need to pass every variable you can think of into the function, instead of just passing the variables you actually need into the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool linearSearch(long empID[], long ID, double hours[], double payRate[], double wages[], const int SIZE)
{
	int index = 0;
	int position = -1;
	bool found = false;
	while (index < SIZE && !found)
	{
		if (empID[index] == ID)
		{
			found = true;
			position = index;
... 
		}
		index++;
	}
	return position;
}

After removing the cout statements, which you say can't be in the function.

Why are you passing all of those variables into the function, you're only using empId[], ID and SIZE (which is a global variable and doesn't need to be passed into the function)? Why are you passing all of those other variables?

Why did you define your function to return a bool value and then try to return an int value?



Your search functions are taking waaay too many arguments.

Your linear search function should probably look like this:
    bool linearSearch(long empId[], long idToFind, long& indexOfIdFound);

This returns bool for success or failure. On success, the indexOfIdFound will have a useful value.

1
2
3
4
5
6
7
8
9
10
11
  long ID, indexOfEmployee;
  cout << "Enter an employee ID for your search: ";
  cin >> ID;
  if (linearSearch(empID, ID, indexOfEmployee)
  {
    cout << "Employee found at index " << indexOfEmployee << "\n";
  }
  else
  {
    cout << "Employee ID " << empID << " not found!\n";
  }

Hope this helps.
@jlb Sorry I didn't elaborate, it's just the assignment requirement. I was passing those variables when I still included the cout statements in those functions, but I will remove them after transferring the cout statements. I passed size because in the assignment requirement, the instructor usually does not allow modification of a function signature which is specified and included int size.

@Duthomhas Thank you!! I fiddled around with your suggestion for a bit and finally got what I wanted :)!
Topic archived. No new replies allowed.