CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2013
    Posts
    11

    problem with if and else if

    hi.i have several if and else if,but the last if only check one if what's the problem.
    plz help
    Code:
    if(strcmp(func,"add")==0 || strcmp(func,"ADD")==0)
    {
    //some orders.
    }
    else
    {
    if(strcmp(func,"multiply")==0 || strcmp(func,"MULTIPLY")==0)
    {
    //some orders
    }
    else
    {
    if(strcmp(func,"transpose")==0  || strcmp(func,"TRANSPOSE")==0)
    {
    //some orders
    }
    else
    {
    if(strcmp(func,"sort")==0  || strcmp(func,"SORT")==0)
    {
    //some orders
    }
    else
    {
    if(strcmp(func,"compare")==0  || strcmp(func,"COMPARE")==0)
    {
    //some orders
    }
    else
    {
    cout<<"you have entered wrong answer\n"
    }}}}}
    but when you entered "sort" ,program sort but print you have entered wrong answer too.
    what should i do??

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: problem with if and else if

    Please, format your code with proper indentations. Example:
    Code:
    if(strcmp(func,"add") == 0 || strcmp(func,"ADD") == 0)
    {
    	//some orders.
    }
    else
    {
    	if(strcmp(func,"multiply") == 0 || strcmp(func,"MULTIPLY") == 0)
    	{
    		//some orders
    	}
    	...
    }
    Otherwise it is impossible to understand your logic!
    Victor Nijegorodov

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: problem with if and else if

    BTW, what did you mean by "when you entered "sort" ,program sort"?
    I don't see any action in the code you have posted!
    Victor Nijegorodov

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: problem with if and else if

    Your question isn't clear, but I would suggest you learn about stricmp. Note the i in the middle.

  5. #5
    Join Date
    Nov 2013
    Posts
    11

    Re: problem with if and else if

    i mean program run well when i entered sort but it also print you have entered wrong answer.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: problem with if and else if

    Quote Originally Posted by Faraz95 View Post
    i mean program run well when i entered sort but it also print you have entered wrong answer.
    The code you posted doesn't do that.

  7. #7
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: problem with if and else if

    Did you run the program in the debugger? That should be pretty straight forward and show you what's going on.

  8. #8
    Join Date
    Nov 2013
    Posts
    11

    Re: problem with if and else if

    Quote Originally Posted by Richard.J View Post
    Did you run the program in the debugger? That should be pretty straight forward and show you what's going on.
    i don't know what debugger is??i use dev compiler

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: problem with if and else if

    Is that your real code? As I said it doesn't do that for me, but it doesn't compile either.

  10. #10
    Join Date
    Nov 2013
    Posts
    11

    Re: problem with if and else if

    it's solved thanks for your help

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured