Program 16

Pages: 123
It does quit successfully whenever I enter '|' , but it stops asking the user to enter another pair of values after

entering the first one ¿¿

No however I went forward " Change the program to write out the smaller value is:

followed by the smaller of the numbers and the larger value is: followed by the larger value. Augment the

program so that it writes the line the numbers are equal (only) if they are equal. This should be easy but it

isn`t because it does not work like I expected:

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
 

#include<iostream>

using namespace std;

int main()
{

  cout<< "Enter two integer values (| to quit): <<\n";
  
  int a=0;
  int b=0;

  while  ( cin>>a>>b ) 
    {
      
      cout << a << '\t' << b << '\n';
      
      if (a<b) cout<< "The smaller value is: a and the larger value is: b \n";
      if (a>b) cout<< "The smaller value is: b and the larger value is: a \n";

      else cout<< "The numbers are equal\n";
      
    }


  char c;
  while ( cin >> c )
    {
      if ( c ==  '|')
        {
          return 0;
        }
      else
        {
        }
    }
}

Last edited on
If you want it such that the programme ends when | is entered, you will need to redesign the programme.
Because, if the condition of the first loop is not met, the programme will enter another, which ì guess is not what you want.

Aceix.
Sorry, I need a little more help than that. My current code looks like this:

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

#include<iostream>

using namespace std;

int main()
{

 
  
  int a=0;
  int b=0;

  while  ( cin>>a>>b ) 
    {
      cout<< "Enter two integer values (| to quit): <<\n";
      
     if (a<b)

    { cout<< "The smaller value is: "<<a<<" and the larger value is: "<<b<<" \n"; }
     
     else if (a>b) 

    { cout<< "The smaller value is: "<<b<<" and the larger value is: "<<a<<" \n"; }

     if (a==b) 

      {  cout<< "The numbers are equal\n"; }

      
    }


  char c;
  while ( cin >> c )
    {
      if ( c ==  '|')
        {
          return 0;
        }
      else
        {
        }
    }
}




But is not working for some reason
Last edited on
not working

That is not a good description of the situation.

The lines 32--44 are currently unnecessary. The first character in the input stream that does not convert to integer (it does not have to be |) will set the stream into failed state and thus quit the while loop. Furthermore, a stream in failed state will fail all further input operations.

If you have to quit with '|' and nothing else, then you have three cases:

1. Input is two integers. Handle and ask again. Your while-loop does that.
2. The input failed because next character in the stream is |. Clear stream error, read the character, exit.
3. The input failed because next character in the stream is EOF. No further input is possible. Exit.
4. The input failed because next character in the stream is something else. Clear stream error, remove offending characters, and resume reading integers.
Ok,

I moved the part where I prompt the user to enter the integer values before the while loop so that I can see it.

Then I removed the lines 32-44. The program if working except that if I enter another character than '|' the

program quits even though it shouldn`t. I don`t know how to implement part 4 should it be something like

1
2
3
4
5

char c;

while (cin>>c) { if c!= '|' } { }      // if char is not equal to '|' resume

Whatever it is you're doing that terminates the program - only do it if c == '|'.

Nested loops.
1
2
3
4
while (true) {
  // Step 1: The line 14--31 loop
  // Step 2: Check why Step 1 did stop
}
Ok, I`m not 100% sure what to do here ( I have to admit that time trouble also for one of the reasons). If I`m wrong, just correct me and I will try again.

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

#include<iostream>

using namespace std;

int main()
{

 
  
  int a=0;
  int b=0;
  char c;

  cout<< "Enter two integer values (| to quit): <<\n";

  while (true) {

  while  ( cin>>a>>b ) 
    {
     
       if (a<b)

    { cout<< "The smaller value is: "<<a<<" and the larger value is: "<<b<<" \n"; }
     
     else if (a>b) 

    { cout<< "The smaller value is: "<<b<<" and the larger value is: "<<a<<" \n"; }

     if (a==b) 

      {  cout<< "The numbers are equal\n"; }

      }

    for (char c; cin >> c && c != '|'; )  // checking that it's not a '|' character

   { if (c == '.') continue; }

   else { break; }

 }

 
Last edited on
Does that even compile?

(Rhetorical question - it's obvious what the answer is.)

EDIT: And if you adopted a sensible indentation and bracing style, you'd see in an instant some of the things you're doing wrong.
Last edited on
closed account (48T7M4Gy)
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
#include<iostream>

using namespace std;

int main()
{
    int a = 0;
    int b = 0;
    char c = '0';

    while  ( c != '|' )
    {
        cout<< "Enter two integer values: \n";
        cin >> a;
        cin >> b;

        if (a<b)
            cout << "The smaller value is: " << a << " and the larger value is: " << b << "\n";

        if (a>b)
            cout << "The smaller value is: " <<b << " and the larger value is: " <<a <<"\n";

        if (a==b)
            cout << "The numbers are equal\n";

        cout << "enter | to quit or any other character to continue." << endl;
        cin >> c;
    }

    cout << "See ya later pacman";
}

Last edited on
@kemort:
You do use 'c' uninitialized on line 16. Besides, your program asks for two numbers and a character, but task assignment wants two numbers or a character.

@pacman169:
1
2
3
4
5
6
while  ( cin>>a>>b ) 
 {
   // ...
 }
// At this line the loop above has stopped because
// the std::cin is not good 
closed account (48T7M4Gy)
@keskivorto
I'm just tryin' to put pacman out of his misery. He might end up the best C++ programmer ever but who knows. Small beginnings.

As for the two numbers and a character, well ther ya go :-)

'c' uninitialized is unnecessary. Maybe good software engineering, maybe not. ;-) Either way that's the least of pacmans problems.

I didn't indent either or even write a goodbye message. My extra bad. ;-[
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

#include<iostream>

using namespace std;

int main()
{

 
  
  int a=0;
  int b=0;
  char c='0';

  cout<< "Enter two integer values (| to quit): <<\n";

while (true) {

  while  ( cin>>a>>b ) 
    {
      
      if (a<b)

          { cout<< "The smaller value is: "<<a<<" and the larger value is: "<<b<<" \n"; }
     
      else if (a>b) 

          { cout<< "The smaller value is: "<<b<<" and the larger value is: "<<a<<" \n"; }

      if (a==b) 

                  {  cout<< "The numbers are equal\n"; }

         

         while  ( cin>>c ) {  

              if( c != '|')  { continue; }
         
           {    

       }

  }
What's your question?
Wow, I invented a new way and now it works perfectly:

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
  

#include<iostream>

using namespace std;

int main()
{

 
  
  int a=0;
  int b=0;
  char c='0';

  

while ( c != '|') {

      cout<< "Enter two integer values: \n"; 
      cin>>a>>b; 
    
      
    if (a<b)

          { cout<< "The smaller value is: "<<a<<" and the larger value is: "<<b<<" \n"; }
     
    else if (a>b) 

          { cout<< "The smaller value is: "<<b<<" and the larger value is: "<<a<<" \n"; }

    if (a==b) 

             {  cout<< "The numbers are equal\n"; }

    cout << "enter | to quit or any other character to continue." << endl;
    cin >> c;

         
while (c == '|')  { return 0;}

  }

 }
Why do you need a while loop on 40? The contents of that loop can never repeat - they will execute either never, or once. Isn't it pointless to have a loop?

If you didn't have a loop there, what would happen if the user enters '|' at line 37? How would that behaviour differ from what you have now?
why are you making it too long ?
Topic archived. No new replies allowed.
Pages: 123