0

I'm a bit new to coding and taking a beginner course in C++ coding. My instructor has given me a an assignment asking for me to write a while loop checking for upper and lower case variables. I feel like it is a simple thing to write and I've researched some help on the topic, but I can't seem to make it work yet. I've emailed my instructor, but he hasn't replied, so I am posting here to hopefully get some new perspective.

Here is the code he provided:

// PLACE YOUR NAME HERE

#include <iostream>
using namespace std;

int main()
{
    char doneYet = 'N'; // on the 2nd run comment this and uncomment the next line
//char doneYet = 'Y'; 

// Set this while loop to run as long as doneYet is ‘N’ or ‘n’
while() // Make sure to check for BOTH upper and lowercase!
    {
        cout << "Am I done yet? (Y/N)\n";
        cin >> doneYet;
    }

    system("pause");
    return 0;
}

His request is "Type in the program above and run it. Answer ‘y’ after the first run. Now modify the program comments so that doneYet is ‘y’ and run it. What happens? Based on this experiment, what is the MINIMUM number of times a while loop can run?"

So far I have this as my code:

// PLACE YOUR NAME HERE

#include <iostream>
using namespace std;

int main()
{
    char doneYet = 'N'; // on the 2nd run comment this and uncomment the next line
//char doneYet = 'Y'; 

// Set this while loop to run as long as doneYet is ‘N’ or ‘n’
while( doneYet = 'N') // Make sure to check for BOTH upper and lowercase!
    {
        cout << "Am I done yet? (Y/N)\n";
        cin >> doneYet;
    }

    system("pause");
    return 0;
}

It will now prompt for input from the user. But whatever I enter, whether it is a n, N, y, Y or any other number or letter, it just repeats the loop and asks again. I believe my instructor is asking for the program to end if y or Y is not entered. I'm sure it's just a simple task, but a little more input would be great. Thank you for your time.

  • 1
    This `doneYet = 'N'` inside `while` loop makes loops always true. Either you want `doneYet == 'N'` or `doneYet != 'N'`. – Achal Oct 25 '18 at 02:51
  • Also read [Why is using namespace std considered bad practice ?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Achal Oct 25 '18 at 02:55
  • You need `while( doneYet == 'N' || doneYet=='n')` – awiebe Oct 25 '18 at 02:57
  • If you turn on compiler warnings, the compiler will tell you what the problem is. Always turn on warnings, and do treat warnings as errors. – drRobertz Oct 25 '18 at 07:43

3 Answers3

0

Hint: research the difference between the operators for 'assignment' and 'compare for equality'.

dave
  • 103
  • 2
0

Here is a sample using a boolean, it's a tad bit cleaner. On a more important note, the reason why your code wasn't working was simply because you have no way to get out of the loop! When using a while loop, you always need to set a condition that when met, lets you exit the loop. Otherwise you'll be stuck!

#include <iostream>
using namespace std;

int main()
{
    char doneYet = 'N';
    bool not_done = true;

while(not_done) // While doneYet is 'N' or 'n'
    {
        cout << "Am I done yet? (Y/N)\n";
        cin >> doneYet;

        if(doneYet == 'y' || doneYet == 'Y')
            not_done = false;
    }
    return 0;
}

As for your second part of the question(if it needs answering):

Type in the program above and run it. Answer ‘y’ after the first run. Now modify the program comments so that doneYet is ‘y’ and run it. What happens? Based on this experiment, what is the MINIMUM number of times a while loop can run?"

If doneYet was 'N' to start with and the while loop looks like while(doneYet == 'N')then your while loop will run at least once. If doneYet was 'Y' to start with and the loop looks like while(doneYet == 'N'), then the loop will not run at all! It will test whether the condition is met, but the block (what's inside the while loop) will not execute.

GKE
  • 960
  • 8
  • 21
0

Don't worry when starting a new language everything is hard!

So the issue is you're using an assignment operator and not a comparison operator.

Here = is assigning doneYet to 'N' meaning whatever doneYet was before change its value to 'N'. What you want to do is check whether doneYet is equal to 'N', you would use == for that. As you want it to check for either 'N' OR 'n' you need to include another check in the same place using the OR operator ||. It should look like,

while(doneYet == 'N' || doneYet == 'n')

This says if doneYet is equal to N or if doneYet is equal to n execute the code below, otherwise skip it.

If you're interested why your loop is endlessly looping its because you're setting doneYet to N at the start of every loop. A character like N to a computer is just a number (78 I think) and according to the computer, when it's checking whether to run a loop 0 is false and everything else is true, so it will be true forever.

CODE BELOW

#include <iostream>
using namespace std;

int main()
{
    char doneYet = 'N';

    while(doneYet == 'N' || doneYet == 'n') // While doneYet is 'N' or 'n'
    {
        cout << "Am I done yet? (Y/N)\n";
        cin >> doneYet;
    }

    return 0;
}
Mr. Pyramid
  • 3,855
  • 5
  • 32
  • 56
Saman
  • 42
  • 5