Continuing the function

Hello I have a problem of where it won't output the other results. It stops after the first function. My main goal of this code is to output the middle character but the float or char is not showing.

Below is my code for the header and the cpp.

CPP
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

// TestRatio.cpp
// Demonstrate use of function template.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


#include "Middle.h"

int main()
{
	 int  integer3(123456789);
	 int integer1(123456789);
	int integer2(123456789);	
	

  const  int NUM(3);

  cout << "Enter " << NUM << " integers:  ";
  for (int i=1; i<=NUM; i++)
  
  {
    while (!(cin >> integer1))   // While we have an error reading...
    {
      cin.clear();  // Remove the error state in stream
      cin.ignore(); // Read but ignore the next character
    }
	 while (!(cin >> integer2))   // While we have an error reading...
    {
      cin.clear();  // Remove the error state in stream
      cin.ignore(); // Read but ignore the next character
    }
	  while (!(cin >> integer3))   // While we have an error reading...
    {
      cin.clear();  // Remove the error state in stream
      cin.ignore(); // Read but ignore the next character
    }
	  
	

  cout << "The middle of "  << integer1 << ", " << integer2 << ", " << integer3  
     << " is: " << doMid(integer1, integer2 , integer3 ) << endl;
 
 }







  float float1=0.0;
  float float2=0.0;
  float float3=0.0;
    cout << "Enter three (3) floating point numbers: " ; 
		cin  >> float1 ;
		cin >> float2 ;
	    cin >> float3 ;
	cout << "The middle of "  << float1 << ", " << float2 << ", " << float3  
     << " is: " << doMid (floa1, float2 , float3 ) << endl;





  char letter1;
  char letter2;
  char letter3;
   cout << "Enter three (3) characters: " ; 
		cin >> letter1 ;
	    cin >> letter2 ;
	    cin >> letter3 ;
  cout << "The middle of "  << letter1 << ", " << letter2 << ", " << letter3  
     << " is: " << doMid (letter1, letter2 , letter3 ) << endl;


  return 0;
}



HEader file
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
template < class T >
T doMid( T n1, T n2, T n3)
{
	T temp;

	if (n1 > n2)
	{
		temp = n1;
		n1 = n2;
		num2 = temp;
		
	}

	if (n1 > n3 )
	{
		temp = n1;
		n1 = n3;
		n3 = temp;
	}

	if (n2 > n3)
	{
		temp = n2;
		n2= n3;
		n3 = temp;
	}
	return n2;
	
}

Last edited on
line 63 << " is: " << doMid (floa1, float2 , float3 ) << endl; spelling error floa1 should be float1

line 10 in your header file num2 = temp; num2 doesn't exist I assume its supposed to be n2
Last edited on
yes those should be changed.. but still how do i get it to continue on to the other two questions of the float and the char
it does that now but the integer one is wrapped in a for loop so you have to do that one three times after that the other two will execute
Last edited on
so in order for me to be able to use the character ignore how do i make it loop once only so that it can go on to the next ones?

what the output i am trying to get
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
 Enter three (3) integers: 9 -4 14
The middle of 9, -4, 14 is: 9
Enter three (3) floating point numbers: -3.9 -5 -0.12
The middle of -3.900000, -5.000000, -0.120000 is: -3.900000
Enter three (3) characters: r g j
The middle of r, g, j is: j
// This works as advertised 

Enter three (3) integers: 4 4 4
The middle of 4, 4, 4 is: 4
Enter three (3) floating point numbers: 9.0 9 9.0
The middle of 9.000000, 9.000000, 9.000000 is: 9.000000
Enter three (3) characters: r r r
The middle of r, r, r is: r
//Checking the handling of equal values

Enter three (3) integers: 92 askldjf9 1230
The middle of 92, 9, 1230 is: 92
Enter three (3) floating point numbers: 5.9 9hrli 0.dui9
The middle of 5.900000, 9.000000, 0.000000 is: 5.900000
Enter three (3) characters: The middle of d, u, i is: i
//Garbage in the input. Note that 5.9 9. and 0.0 were read as the
//floats, which left "dui9" in the input stream. This was processed
//by the next cin statements.
just move the brace on line 47 to line 79

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// TestRatio.cpp
// Demonstrate use of function template.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


#include "Middle.h"

int main()
{
	 int  integer3(123456789);
	 int integer1(123456789);
	int integer2(123456789);	
	

  const  int NUM(3);


  for (int i=1; i<=NUM; i++)
  {
		cout << "Enter " << NUM << " integers:  ";

		while (!(cin >> integer1))   // While we have an error reading...
		{
		  cin.clear();  // Remove the error state in stream
		  cin.ignore(); // Read but ignore the next character
		}
		 while (!(cin >> integer2))   // While we have an error reading...
		{
		  cin.clear();  // Remove the error state in stream
		  cin.ignore(); // Read but ignore the next character
		}
		  while (!(cin >> integer3))   // While we have an error reading...
		{
		  cin.clear();  // Remove the error state in stream
		  cin.ignore(); // Read but ignore the next character
		}
	  
	

	  cout << "The middle of "  << integer1 << ", " << integer2 << ", " << integer3  
		 << " is: " << doMid(integer1, integer2 , integer3 ) << endl;
 
 







	  float float1=0.0;
	  float float2=0.0;
	  float float3=0.0;
		cout << "Enter three (3) floating point numbers: " ; 
			cin  >> float1 ;
			cin >> float2 ;
			cin >> float3 ;
		cout << "The middle of "  << float1 << ", " << float2 << ", " << float3  
		 << " is: " << doMid (float1, float2 , float3 ) << endl;





	  char letter1;
	  char letter2;
	  char letter3;
	   cout << "Enter three (3) characters: " ; 
			cin >> letter1 ;
			cin >> letter2 ;
			cin >> letter3 ;
	  cout << "The middle of "  << letter1 << ", " << letter2 << ", " << letter3  
		 << " is: " << doMid (letter1, letter2 , letter3 ) << endl;

  }
  return 0;
}
Last edited on
now this is the output .. so am i having too many cin while statements?
1
2
3
4
5
6
7
8
9
10


Enter 3 integers:
9 4 8
The middle of 9, 123456789, 123456789 is: 123456789
Enter three (3) floating point numbers:
The middle of 4, 0, 0 is: 0
The middle of 8, 0, 0 is: 0
^CPress any key to continue . . .


and this
1
2
3
4
5
6
7
8
9
10

Enter 3 integers:
9 asd45 41
The middle of 9, 123456789, 123456789 is: 123456789
Enter three (3) floating point numbers:
3.99 45.05s 54
The middle of 5, 0, 0 is: 0
^CPress any key to continue . . .

Last edited on
Topic archived. No new replies allowed.