Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,390 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,138 people online right now. Registration is fast and FREE... Join Now!




Strings

 
Reply to this topicStart new topic

Strings, Boolean Statement Problems

grinsalot
19 Sep, 2006 - 10:45 AM
Post #1

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 50


My Contributions
Hi,

I'm having some problems with my boolean statement. I need the bool to return false if the user does not enter only the letters A or C. However, this bool always returns false. I can't seem to figure out why.

Thank you in advance for any suggestions or help.


CODE
int main()
{
  cin >> string_1;
  for(int i=0;i<string_1.length();i++)
      if(string_1[i] != 'A' || 'C')
        {
          cout << "Try again." << endl;
          cin >> string_1;
        }

User is offlineProfile CardPM
+Quote Post

Nova Dragoon
RE: Strings
19 Sep, 2006 - 10:56 AM
Post #2

The Innocent Shall Suffer, Big Time
Group Icon

Joined: 16 Aug, 2001
Posts: 6,132



Thanked: 7 times
Dream Kudos: 515
Expert In: Python, Linux

My Contributions
part of the problem is this line:
if(string_1[i] != 'A' || 'C')

which needs to look like:
if(string_1[i] != 'A' || string_1[i] != 'C')
User is offlineProfile CardPM
+Quote Post

grinsalot
RE: Strings
19 Sep, 2006 - 11:11 AM
Post #3

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 50


My Contributions
Thank you for your help Nova Dragoon. I'm still not able to get the program to run properly though. My bool statement always evaluates to false on the first run-through.
User is offlineProfile CardPM
+Quote Post

Nova Dragoon
RE: Strings
19 Sep, 2006 - 11:17 AM
Post #4

The Innocent Shall Suffer, Big Time
Group Icon

Joined: 16 Aug, 2001
Posts: 6,132



Thanked: 7 times
Dream Kudos: 515
Expert In: Python, Linux

My Contributions
edit: I read the quesiton wrong
User is offlineProfile CardPM
+Quote Post

Xing
RE: Strings
19 Sep, 2006 - 11:32 AM
Post #5

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
User is entering a string or a single character?
User is offlineProfile CardPM
+Quote Post

grinsalot
RE: Strings
19 Sep, 2006 - 11:54 AM
Post #6

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 50


My Contributions
Hi Xing,

The user is entering a string of as many letters as they like. However, the string can only contain A and C. If the string contains any other letters, I have to identify the incorrect entries.

Thank you.

I have worked out some alternate code. It too is failing, however.

CODE
int main()

{
   int i = 0;
   cin >> string_1;
   int length = string_1.length();
     while(i<length){
                 while(string_1[i] !='A' || string[i] != 'C')
      {
           cout << "Try again." << endl;
           cin >> string_1;
            i = i + 1;
      }
  }


This post has been edited by grinsalot: 19 Sep, 2006 - 12:04 PM
User is offlineProfile CardPM
+Quote Post

Xing
RE: Strings
19 Sep, 2006 - 12:23 PM
Post #7

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Replace || with &&

This post has been edited by Xing: 19 Sep, 2006 - 12:23 PM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Strings
19 Sep, 2006 - 12:26 PM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You'll likely want to check for the AND condition...not the OR.

[edit]Xing beat me too it! smile.gif[/edit]
User is offlineProfile CardPM
+Quote Post

grinsalot
RE: Strings
19 Sep, 2006 - 12:30 PM
Post #9

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 50


My Contributions
QUOTE(Xing @ 19 Sep, 2006 - 01:23 PM) *

Replace || with &&

Thank you Xing and Amadeus! It's the details that I consistently seem to miss. Note to self: Pay attention!

Thank you, again.
User is offlineProfile CardPM
+Quote Post

violent_crimson
RE: Strings
19 Sep, 2006 - 03:41 PM
Post #10

New D.I.C Head
*

Joined: 31 Aug, 2006
Posts: 36


My Contributions
#include <iostream>
using namespace std;

char szInput [3];

int main () {
//get one character from the user into the array

if (szInput [1] != 'a' && szInput [1] != 'c') {
//you entered an invalid character..
}
return 0;
}
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Strings
19 Sep, 2006 - 05:27 PM
Post #11

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
His code was working with the small change that was suggested.
User is offlineProfile CardPM
+Quote Post

Xing
RE: Strings
19 Sep, 2006 - 05:36 PM
Post #12

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
@violent_crimson
Code Tags Please
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:28AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month