C++ Help Please, deadline is getting closer

deetlej1

Active Member
Joined
Dec 7, 2008
Messages
38
Reaction score
0
I don't know what I missed but it seems like my loops just go on with out stopping.

This is the question:

Suppose we want to input and validate three characters in a do..while loop. The variable names are c1, c2 and
c3. The values of c1 and c2 may not be the same and c3 may not be equal to X. If this condition does not hold, the
loop has to be executed again. Write down a correct condition for the do..while loop.

My coding:

#include <iostream>
using namespace std;

int main()
{
char c1,c2,c3;

do
{
cout<<"Enter a character:";
cin>>c1;
cout<<"Enter a second character:";
cin>>c2;
cout<<"Enter a third character:";
cin>>c3;


} while ((c3 = 'X') or (c1 = c2));

return 0;

}

I can't see the problem here guys.
 
I know in c# you have to put == for comparison

Something like
Code:
} while ((c3 == 'X') or(c1 == c2));

Doing this
Code:
} while ((c3 = 'X') or(c1 = c2));
Assigns the value of X to c3 and the value of c2 to c1
 
Last edited:
Fairly simple, the question states that c3 may not be X and c1 may not equal c2, however your code states that
Code:
while ((c3 = 'X') or (c1 = c2))
To add to the post above, you need something like
Code:
while ((c3 != 'X') or (c1 != c2))
in order to satisfy the requirement.
 
Last edited:
A example of a while loop with out inputs

#include <iostream>
#include <time.h>
#include <windows.h>

using namespace std;

bool end=false;

void waitToQuit (int time)
{
clock_t endWait;
bool noInput=true;
endWait=clock()+time;
while (clock()<=endWait && noInput)
{
if (GetAsyncKeyState(VK_ESCAPE))
{
end=true;
noInput=false;
}
}
}

int main()
{
int count=1;
while (!end)
{
cout<<count<< " Created by Ismail press esc\n";
waitToQuit(1);
count++;
}
return 0;
}
 
Fairly simple, the question states that c3 may not be X and c1 may not equal c2, however your code states that
Code:
while ((c3 = 'X') or (c1 = c2))
To add to the post above, you need something like
Code:
while ((c3 != 'X') && (c1 != c2))
in order to satisfy the requirement.

You are completely wrong!
Assumme c1 = 'A', c2 = 'A', and c3 = 'X'.

Then you'll get
while (false and false) = while (false), the loop will break and a invalid input would have been accepted

Read the question:
. If this condition does not hold, the
loop has to be executed again

So what you actually want is:
Code:
while [b]![/b]((c3 != 'X') && (c1 != c2))
Which by de morgans law is equal to:
Code:
while ((c3 == 'X') or (c1 == c2))

The problem wasn't the logic, that was correct. It was the single '='
 
Last edited:
You are completely wrong!
Assumme c1 = 'A', c2 = 'A', and c3 = 'X'.

Then you'll get
while (false and false) = while (false), the loop will break and a invalid input would have been accepted

Read the question:


So what you actually want is:
Code:
while [b]![/b]((c3 != 'X') && (c1 != c2))
Which by de morgans law is equal to:
Code:
while ((c3 == 'X') or (c1 == c2))

The problem wasn't the logic, that was correct. It was the single '='
Foot in mouth for me, didn't read the rest of the question :o Figured that there wouldn't be a double negative requirement. Made a mistake with the && thing btw, but you quoted me before I edited it :D

Ismail TM: what are you on about? :confused:
 
Last edited:
Thank you very much it was only the == now on to the next question :-)
 
Top
Sign up to the MyBroadband newsletter
X