Thread: Can't figure out why it won't work

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    12

    Can't figure out why it won't work

    Hi all. I'm new to C++ and I wrote a program that compiled without errors or warnings but doesn't do what its suppose to do. The program asks for the last name of 1 of 2 authors and then is suppose to display the books written by that author. No matter which name is typed in the program always displays the books written by the second author. I use strcmp and an if/else statement.

    Heres my code:

    #include <iostream>
    #include <cstring>
    using namespace std;

    int main()
    {

    char Name[30];
    char Binchy;
    char Pilcher;
    cout << "Enter Authors Last Name: \n";
    cin >> Name;

    if(strcmp(Name,"Binchy")){

    cout << " \n";
    cout << "The Copper Beach\n";
    cout << " \n";
    cout << "Light a Penny Candle\n";
    cout << " \n";
    cout << "The Lilac Bus\n";
    cout << " \n";
    cout << "Firefly Summer\n";
    cout << " \n";
    cout << "Scarlet Feather\n";
    cout << " \n";
    cout << "Sleeping Tiger\n";
    cout << " \n";
    cout << "The Grass Lake\n";
    cout << " \n";
    cout << "Silver Wedding\n";
    cout << " \n";
    cout << "Silver Lake\n";
    cout << " \n";
    cout << " \n";
    }

    else
    cout << "Snow in April\n";
    cout << " \n";
    cout << "Voice in Summer\n";
    cout << " \n";
    cout << "Flowers in the Rain\n";
    cout << " \n";
    cout << "Coming Home\n";
    cout << " \n";
    cout << "Shell Seekers\n";
    cout << " \n";
    cout << "The Empty House\n";
    cout << " \n";
    cout << "Wild Mountain Thyme\n";
    cout << " \n";
    cout << "Under Gemini\n";
    cout << " \n";
    cout << "September\n";
    cout << " \n";
    cout << " \n";

    system("PAUSE");
    return 0;
    }


    Eventually I would like to add an option to add a book to either list but I'll just take one step at a time.

    Any help would be appreciated.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    First of all, you forgot your braces for the else statement :
    Code:
    else
    {
    cout << "Snow in April\n";
    //...
    }
    Also, I'm not sure whether you used strcmp correctly. I would suggest to use strcmpi (case insensitive); it will return 0 if the two strings match, so if you want the if statement to run only if the name entered is "binchy" then you need to use:
    Code:
    if (!strcmpi(Name,"Binchy"))
    {
    //...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    12

    The Program works fine now

    The Program works fine now. I had a feeling about the braces but I sure didn't know that I needed a different function. Thanks very much for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  2. The simpler version of two programs doesn't work......
    By w00tw00tkab00t in forum C++ Programming
    Replies: 1
    Last Post: 02-06-2006, 07:07 PM
  3. Writing BMP file... doesn't work correctly
    By tin in forum Game Programming
    Replies: 3
    Last Post: 12-28-2005, 04:40 AM
  4. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 11:53 AM
  5. Developers Wanted
    By Quasicom in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-24-2005, 12:46 AM