can't figure out what's with this code

This is a discussion on can't figure out what's with this code within the C++ Programming forums, part of the General Programming Boards category; hey guys, i'm new at coding, and this is a relatively simple thing, i just can't figure out whats wrong ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    can't figure out what's with this code

    hey guys, i'm new at coding, and this is a relatively simple thing, i just can't figure out whats wrong

    i think that it might be that i'm using char

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char music;
        
        cout<<"hey kat, do you like this new band i found, 'circle takes the square': ";
        cin>> music;
        cin.ignore();
        if (music = y) {
                  cout<<"good choice\n";
        }
        
        if (music = n) {
           cout<<" aw you're going to make me cry!\n";
           }
           cin.get();
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Code:
    music = y ;
    if y were defined, would be an assignment. You want to use the compare operator. Same with the other condition. And you want to compare to a character constant.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In other words:
    Code:
    if(music == 'y')
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    ok now...

    ok so i changed that, and added an 'else' to the no part, but when the user puts in a 'n' it says the same thing as 'y', any clue why?

    btw thanks you two guys

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char music;
        
        cout<<"hey kat, do you like this new band i found, 'circle takes the square': ";
        cin>> music;
        cin.ignore();
        if (music = 'y') {
                  cout<<"good choice\n";
        }
        
        else if (music = 'n') {
           cout<<" aw you're going to make me cry!\n";
           }
           cin.get();
    }

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are still using assignment. = and == are completely different operators.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    ok, thanks anon, todd, and dwks

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Btw, if the warning levels are high enough, some compilers will warn about assignment like that inside if statements. A typical example is Visual Studio.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Banned
    Join Date
    Nov 2007
    Posts
    678
    why not make such warnings etc. the default behave?
    and experienced users can turn them off, if they wish, but newbies will be saved from buggy situations

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    on gcc it will issue a warning with -Wall.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21