473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Whats wrong with my || operator

1
Why does the following code never exit the loop when the correct string is entered?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6.     std::string myString = "";
  7.     while( (myString!="one") || (myString!="two") )
  8.     {
  9.         cout << "enter the word one or two: ";
  10.         cin >> myString;
  11.     }
  12. return 0;
  13. }
  14.  
Mar 31 '08 #1
2 1039
ashitpro
542 Expert 512MB
Why does the following code never exit the loop when the correct string is entered?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6.     std::string myString = "";
  7.     while( (myString!="one") || (myString!="two") )
  8.     {
  9.         cout << "enter the word one or two: ";
  10.         cin >> myString;
  11.     }
  12. return 0;
  13. }
  14.  
Error is in your logic...
use && instead ||
Mar 31 '08 #2
Ganon11
3,652 Expert 2GB
Exactly. If your user enters "one", then myString!="one" is false, but myString!="two" is still true, because "one"!="two".
Mar 31 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

43
by: Mr.Tickle | last post by:
// Run this code and look for his obvious error by the compiler namespace FlagCheck { using System; enum SomeFlags {
4
by: Alfonzo Morra | last post by:
I've the ff code in cp assignmenent cstor: PB& PB::operator=( const PB& b) { if ( this != &b ) { PB *pb = new PB( b ) ; this = pb ; // <- Compiler barfs here } return *this ; }
1
by: Tony Johansson | last post by:
This class template and main works perfectly fine. But could be better. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter...
5
by: JJ | last post by:
Hi all, whats wrong with the following code: DateTime calcDate = new DateTime(); DateTime dDate = DateTime.Now; if (calcDate.DayOfWeek(dDate) != "Saturday") || (calcDate.DayOfWeek(dDate)...
4
by: lothar.behrens | last post by:
Hi, my own stream implementation writes correctly, but it does not read all back. Why ? Thanks, Lothar Here is the output: 'Testdata1: ', 0
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
10
by: andrew browning | last post by:
i have overlaoded all of my arithmetic operators but all are functioning as multiplication. below is a sample of the addition operator: Rational operator + (const Rational& r1, const Rational&...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
14
by: nobrow | last post by:
Yes I know what lvalue means, but what I want to ask you guys about is what are all valid lvalues ... a *a a *(a + 1) .... What else?
10
by: Jim Langston | last post by:
I use a game engine using MSVC++ .net 2003 and have no problems. Some users of DevC++ who use the same engine crash at times when a copy of this structure is the return variable. I don't have...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.