9 Replies - 485 Views - Last Post: 08 November 2019 - 05:01 PM Rate Topic: -----

#1 jae1738   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-November 19

Optimising or rewriting nested do-while loop

Posted 07 November 2019 - 12:38 PM

My assignment asks me to do the following:
-user inputs characters (letters) until the input is equal to . (a dot)
-after a dot, program checks if any letters other than approved ones have been input
-if any letter other than the approved ones has been input, then program asks the user to input everything all over again

The easiest solution I could think of was creating a nested do-while loop. It goes something like this:

Here is the part of the code I am having problems with:

do{
sum=0; /*this is for reseting the score in case of a bad input*/
printf("Input the characters: ");/*program needs to write this again in a case it requests input again*/
    do{
scanf("%c",&letter);

if(letter!='A' && letter!='B' && ... && letter!='.')
error=1;

if(letter=='A')
sum+=1;
else if(letter=='B')
sum+=2;
.
.
./*and so on, I list all of the actions for the approved letters*/

       }while(letter!='.');

}while(error==1);


I hope it is clear what I meant. However, the compiler I have to work in has a executing time limit and I get the message "Execution took too long, program terminated". I assume it is because I am using nested do-while. Note that the program works perfectly for the inner loop part.
I would appreciate any suggestions on this matter.

Is This A Good Question/Topic? 0
  • +

Replies To: Optimising or rewriting nested do-while loop

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5496
  • View blog
  • Posts: 14,470
  • Joined: 18-April 07

Re: Optimising or rewriting nested do-while loop

Posted 07 November 2019 - 01:21 PM

I assume you have learned about arrays? That a string is actually an array of characters? Which means you can create an array of "abcdefghijklmnopqrstuvwxyz1234567890." or whatever and loop through it, comparing it to "letter".

Have you learned about arrays and strings yet? Might help a lot here.
Was This Post Helpful? 0
  • +
  • -

#3 jae1738   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-November 19

Re: Optimising or rewriting nested do-while loop

Posted 07 November 2019 - 01:28 PM

No, we have not learned about arrays on this course yet. It is meant to be done with loops and conditions only.

This post has been edited by Skydiver: 07 November 2019 - 07:59 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5496
  • View blog
  • Posts: 14,470
  • Joined: 18-April 07

Re: Optimising or rewriting nested do-while loop

Posted 07 November 2019 - 04:16 PM

oh ok, well things to think about...

1) When you detect an error and set error = 1 and it goes back around again, make sure to reset error to zero again.
2) scanf reads a character at a time from the input stream. So when you detect an error and go back around, scanf is still going to read from the previous input you entered because those letters are left in the stream.
3) Keep in mind that hitting return puts a carriage return/linefeed character in the stream



Not perfect but some code to think about...

    do{
        sum=0; 
        error=0; // Don't forget to reset error state back to normal
        
        printf("Input the characters: ");
        
        do {
            scanf("%c",&letter);
            
            if (letter!='A' && letter!='B' && letter!='.') {
                error=1;
            }
            
            if (letter=='A')
                sum+=1;
            else if(letter=='B')
                sum+=2;
        } while (letter != '.');

        // If there was an error, clear out the remaining buffer to start again
        if (error == 1) {
            while ((getchar()) != '\n');
        }
    } while(error==1);



Of course this code could use some improvement, but reading through it may help you understand what I am talking about in my points.

This post has been edited by Martyr2: 07 November 2019 - 04:17 PM

Was This Post Helpful? 1
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Optimising or rewriting nested do-while loop

Posted 07 November 2019 - 07:35 PM

View Postjae1738, on 07 November 2019 - 02:38 PM, said:

However, the compiler I have to work in has a executing time limit and I get the message "Execution took too long, program terminated".

What? Compilers don't execute code. Compilers just convert source code into executable binary code. They can't possibly determine that a program is taking too long to execute. If they could, the compiler writer would be up for an award in Computer Science for finally solving The Halting Problem.

Can you tell us the name of this "compiler"?
Was This Post Helpful? 0
  • +
  • -

#6 jae1738   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-November 19

Re: Optimising or rewriting nested do-while loop

Posted 07 November 2019 - 07:48 PM

@skydiver:

Alright, I did not explain it well then. It is a platform used by my uni and with every assignment you get tests which output of my program has to pass in order to get full points. So it's not the compiler, it is the test that is set to be like "ok this is taking too long, probably something wrong, I'll stop it here."

@Martyr2:

Thank you very much for your help, that might actually solve it. It never crossed my mind that I forgot to reset the error as well.

This post has been edited by Skydiver: 07 November 2019 - 07:58 PM
Reason for edit:: Removed excessive quotes.

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Optimising or rewriting nested do-while loop

Posted 07 November 2019 - 08:09 PM

Perhaps I'm missing something but why are you checking for each character? Recall the ASCII table layout.

So this becomes a simple exercise of implementing the following pseudo-code:
do
{
    print "Please enter input"

    error = false
    ch = getchar()
    while ( ch != '.' )
    {
        if ( NOT ( ( 'A' <= ch AND ch <= 'Z' ) OR
                   ( 'a' <= ch AND ch <= 'z' ) ) )
        {
            error = true
        }
        ch = getchar()
    }
} while ( error )


Was This Post Helpful? 0
  • +
  • -

#8 jae1738   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-November 19

Re: Optimising or rewriting nested do-while loop

Posted 08 November 2019 - 03:08 PM

@Skydiver I do not need all letters of the alphabet. Only few are allowed to be input, other are treated as an error.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Optimising or rewriting nested do-while loop

Posted 08 November 2019 - 04:56 PM

That wasn't obvious in your first post, but okay, giant switch statement then since you are not allowed to lookup against an array.

On second thought, are you allowed to use strchr()?
Was This Post Helpful? 0
  • +
  • -

#10 jae1738   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-November 19

Re: Optimising or rewriting nested do-while loop

Posted 08 November 2019 - 05:01 PM

@Skydiver:
No, I don't think so.
I've solved the problem by reseting the error to 0 and clearing the buffer. Now it works as it should.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1