Find the answer to your Linux question:
Results 1 to 5 of 5
I'm trying to write a program that repeatedly asks for characters until "$" is entered. The program then outputs the number of characters entered before the $ was entered. So ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    38

    Why isn't this working (C++)?


    I'm trying to write a program that repeatedly asks for characters until "$" is entered. The program then outputs the number of characters entered before the $ was entered.

    So I did this:

    #include <iostream>
    using namespace std;

    int main() {


    int b;
    char a;

    b = 0;

    do{ cout << "Enter character: ";
    cin >> a;
    b++;
    } while ( a != "$" );

    cout << b << "\n";

    return 0;

    }

    I called this "dollar.cpp".

    Unfortunately, the program doesn't compile. I get this as an error:


    dollar.cpp:23:20: warning: comparison with string literal results in unspecified behaviour
    dollar.cpp:23:20: error: ISO C++ forbids comparison between pointer and integer

    What's wrong here?

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,558
    Try single quotes around the dollar sign in the while() condition. IE:
    Code:
    } while (a != '$');
    This is a pretty common sort of mistake for beginning programmers.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Aug 2011
    Posts
    38
    Thanks, it works!!

    Can I just ask, what is the difference between " and '?

    When i'm using cout, i'm using " so what's the general rule for using one or the other?

  4. #4
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,561
    Single quotes are for a character and double quotes are for a string, which more accurately would be a pointer to a string.
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,558
    Quote Originally Posted by Catch_22 View Post
    Thanks, it works!!

    Can I just ask, what is the difference between " and '?

    When i'm using cout, i'm using " so what's the general rule for using one or the other?
    What elija said. This may clarify the "rule":

    Code:
    char stuff[] = { 'a', 'b', 'c', 0 }; // an initialized string/char array variable.
    char a = stuff[0];
    char b = stuff[1];
    char c = stuff[2];
    char* astring = stuff; // Or, char* astring = &stuff[0]; - same thing.
    
    if (a == 'a') printf("ok, a == 'a'\n");
    if (b != 'b') printf("not ok - b != 'b' should not compute since b (stuff[1]) == 'b'\n");
    if (c == "c") printf("not ok - c is not a string, it's a char\n"); // This would cause your compiler error.
    if (astring == stuff) printf("ok - astring points to stuff\n");
    if (astring == "abc") printf("not ok - astring (alias stuff) has same data, but not same address as \"abc\" string literal\n");
    if (strcmp(astring, "abc") == 0) printf("ok - astring contains same data as \"abc\" string literal\n");
    Are we confused yet?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •