Trying to understand the continue

I have it working like it is supposed to except if it comes across the numeric char then it halts. Is this what is supposed to happen or is there something I can change to get it to work?

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
  //Program exercise 6_1
#include <iostream>
#include <cctype>

int main()
{
    using namespace std;
    cout << "Enter text to be analyzed \n"
         << "Enter a @ to terminate. \n";
    char ch;
    cin.get(ch);
    while (ch != '@')
    {
     if (isdigit(ch))
            continue;
    if (ch >= 'A' && ch <= 'Z')
    {
        ch = tolower(ch);
    }
    else
    {
        ch = toupper(ch);
    }
      cout << ch;
      cin.get(ch);
    }

    return 0;
}
you have an infinite loop when a number is entered you skip the rest of the loop and you never get another character so you just keep skipping the loop.
Last edited on

When your code sees a continue statement inside the loop it will skip any instructions below it and begin at the top of the loop again, hence why you freeze in a indefinite loop.

What exactly are you trying to achieve? - accept only A-Z and not allow 0-9?
The way this is written if I enter text it will change lower case to upper and vise verse. If it detects a number it skips past that to the next loop. Everything works fine if I do characters and then the escape @, but if I have type numbers the program removes them like it is suppose to but them it halts.
Like the others have said, you have an infinite loop when you enter a number. You aren't reading in a new character to process because you are skipping that step with the "continue" statement. Therefore you are processing the number over and over and skipping the cin.get step each time.

Let's say I enter the number 3 into your program. Your code will do this:
3 is not the @ symbol, so enter the while loop.
Is 3 a digit? Yes, so continue to the top of the while loop.
3 is not the @ symbol so enter the while loop
Is 3 a digit? Yes, so continue to the top of the while loop.
...

That will continue indefinitely since a new character is never read in for processing.

To minimally impact what you already have written, you could do something like this:

1
2
3
4
if(isdigit(ch)) {
  cin.get(ch);
  continue;
}
Last edited on
works like a charm... I see what you meant now. Was having a brain overload

Problem here is you will end up with 3 unnecessary cin statements when adding discofire's suggestion - you only need one, and you also dont need to use continue.

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

#include <iostream>
#include <cctype>
using namespace std;

int main()
{
	char ch;

	do {
		cout << "Enter character to be analyzed: ";
		cin >> ch;
		if (isalpha(ch)) {
			// we are a character
			if (ch >= 'A' && ch <= 'Z')
				ch = tolower(ch);
			else
				ch = toupper(ch);
			cout << ch << endl;
		}
	} while (ch != '@');
	
	return 0;
}
Topic archived. No new replies allowed.