if..else problem

This is a discussion on if..else problem within the C++ Programming forums, part of the General Programming Boards category; Yea i know this is lame but i'm new to this particular attempt, the code is self explanatory and i ...

  1. #1
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    if..else problem

    Yea i know this is lame but i'm new to this particular attempt, the code is self explanatory and i commented as to where the problem lies, thnx for the help.

    Code:
    /*	Steven Billington	
    	Simple bot
    */
    
    #include <iostream.h> 
    
    int main ()
    {
    	const max = 50; // Set limit for input. Also tried with just
    					//input[5] with same problems
    	char input[max];
    
    
    	cout<<"Hello\n"; //Prompt user for input
    	cout<<"Response: ";
    
    	cin.getline(input,max); //Store input into input variable
    
    	cout<<"\n"; // Start a new line
    
    	// Check to see what to do, program is skipping right to
    	//else option
    	if (input == "a")
    	{
    		cout<<"Its a nice day out."<<"\n";
    	}
    
    	else
    		cout<<"What?\n";
    
    
    	return 0;
    
    }

  2. #2
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    (input[max]) is an array; therefore (input) is a pointer to the beggining of the array; i.e. in this case a pointer to a char.
    by writing
    Code:
    if(input=="a")
    you are asking if a char pointer (the pointer itself, not the char it points to) is equal to a string literal.

    A better way to do it would be
    Code:
    if(*input == 'a')
    or even to use strcmp
    Code:
    if(!strcmp(input, "a"))
    something like that..

    actually, if you only need to check one char for now, it would be easier to understand if
    Code:
    if(input[0] == 'a')
    since this way you are checking if a char equals another char(same as the first example but easier to notice).

    correct me if I'm wrong..


    (PS. there's no such thing as a dumb question )
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Ride-or-die, I've seen this question at least 20 times on this board. And you have three times my post-count...

    Woot, I'm in a sig
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    This should be in the FAQ.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    I just couldn't resist adding that one to my sig drax ^.^
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  6. #6
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    edit: spoke too soon, new code w/ errors. * didn't work.

    Code:
    /*	Steven Billington	
    	Simple bot
    */
    
    #include <iostream.h>
    #include <string.h> 
    
    int main ()
    {
    	const max = 50; // Set limit for input. Also tried with just
    					//input[5] with same problems
    	char input[max];
    
    
    	cout<<"Hello\n"; //Prompt user for input
    	cout<<"Response: ";
    
    	cin.getline(input,max); //Store input into input variable
    
    	cout<<"\n"; // Start a new line
    
    	// Check to see what to do, program is skipping right to
    	//else option
    	if (!strcmp (input == "Hello"))
    	{
    		cout<<"Its a nice day out."<<"\n";
    	}
    
    	else
    		cout<<"What?\n";
    
    
    	return 0;
    
    }
    PHP Code:
    --------------------Configurationsimplebot Win32 Debug--------------------
    Compiling...
    simplebot.cpp
    D
    :\MyProjects\Mine experimental or unfinished\Marvin version one\simplebot.cpp(24) : error C2660'strcmp' : function does not take 1 parameters
    Error executing cl
    .exe.

    simplebot.obj 1 error(s), 0 warning(s
    Last edited by RoD; 10-23-2002 at 01:27 PM.

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    look up strcmp somewhere, the msdn is always a good place to start.

    There are a few error messages that you'll see time and time again, try to understand what they mean and how to fix the problem. Dont just fix this one and go away not knowing why it never worked before...
    Couldn't think of anything interesting, cool or funny - sorry.

  8. #8
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I'll do that endo, thnx guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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