Solved

Help in excuting loops.

Posted on 1998-04-10
6
179 Views
Last Modified: 2010-04-15
I am presently learning the (for, while and do while) loops. My problem is my program stays in a continuous
loop and I have to shut down the computer to restart. I am new at this, so please be patient. I have this program
to write, representing the letters (A thru Z) and (a thru z) to coresspond to the digits (2 thru 9) on the telephone. Yes, I was here earlier with the same question, only using the ( if and if else) and I did get it to work.
 Now my code starts:
#include <stdio.h>
main()
{
 char ch='  ' ;
 printf("Enter a single letter, and I will tell you what the corresponding digit is on the telephone \
 (Period to stop).   ");
                                      flushall();
                                      ch=getchar();
while(ch>='A' && ch<='C' || ch>='a' && ch<='c')
         printf("\nThe digit 2 correspondes to the letter %c on the telephone.", ch);

while(ch>='D' && ch<='F' || ch>='d' && ch<='f')
         printf("\nThe digit 3 correspondes to the letter %c on the telephone.", ch);

ETC. ETC. ETC.
Then a statement to show any invalid characters that are entered, such as,

while(ch<'A' && ch>' / ' || ch<' ' ' && ch>'z')
        printf(\nThe entry %c is invalid.",ch);

while(ch !=' . ')
        printf("\nStopped.",ch);
}
Can you please help? I can't get the program to print the users input and loop back to ask for the users input
again. All I get is a continous loop of the first input. Also, will any digits entered ( 0 thru 9) show up in the printf as invalid characters since I am using ch? Thanks for any help you can provide. Dan (Beginner)      
 
0
Comment
Question by:cstudent
6 Comments
 

Author Comment

by:cstudent
ID: 1258073
Edited text of question
0
 
LVL 10

Accepted Solution

by:
rbr earned 50 total points
ID: 1258074
Try

   #include <stdio.h>
   main()
   {
    char ch='  ' ;
    do {
    printf("Enter a single letter, and I will tell you what the corresponding digit is on the
   telephone \
    (Period to stop).   ");
                                         flushall();
                                         ch=getchar();
   if (ch = '.')
        continue;

   if(ch>='A' && ch<='C' || ch>='a' && ch<='c')
            printf("\nThe digit 2 correspondes to the letter %c on the telephone.", ch);

   if(ch>='D' && ch<='F' || ch>='d' && ch<='f')
            printf("\nThe digit 3 correspondes to the letter %c on the telephone.", ch);

   ETC. ETC. ETC.
   Then a statement to show any invalid characters that are entered, such as,

   if(ch<'A' && ch>' / ' || ch<' ' ' && ch>'z')
           printf(\nThe entry %c is invalid.",ch);

    } while(ch !='.');
     printf("\nStopped.",ch);
 
0
 
LVL 10

Expert Comment

by:rbr
ID: 1258075
Sorry
use
if (ch == '.')
instead of
if (ch = '.')

I forgot the ==
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 2

Expert Comment

by:kellyjj
ID: 1258076
rbr,  good thing you remember the '=='.  That little thing has caused me more heartache that you can imagine.!!  hehe,  good thing I KNOW WHAT I AM DOING!!!  (damn, what won't this loop work??!!??)   heheheheh
0
 
LVL 5

Expert Comment

by:nebeker
ID: 1258077
The reason your code wasn't working is because the line:

    while(ch>='A' && ch<='C' || ch>='a' && ch<='c')
        printf("....");

will just keep executing (in this case, executing the printf statement) until your "while" clause is met;  i.e. until "ch" is in the range of 'A' to 'C', you'll stay in that loop forever.

You only use a while loop when you want to repeat a certain action over and over until a specific outcome is achived.  If you just want to look for certain values, use an "if" statement.


0
 

Author Comment

by:cstudent
ID: 1258078
And a big Thank You for 'Nebeker' for the explanation. I was still wondering why the program was stuck in a continous loop before, now I understand a bit better. Also thanks to 'rbr' for the info.,  but could you explain why to do certain things in the program. I took a while trying to figure out why you did things a certain way and what the reasoning was behind it. I think I figured most of it, but still being a beginner, I guessing. Thanks again the program is working fine. Dan
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Test against App 49 137
Problem in finding output of a program 11 104
Passing command line arguments in C 10 89
What is atomic operation? 6 58
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question