Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 1st, 2011, 9:24 PM   #1
libchk
Newbie
 
Join Date: Sep 2011
Posts: 1
Rep Power: 0 libchk is on a distinguished road
advise on how to properly use strcmp

Hi Noob C here,

Im trying to compare a string from a user input using fgets, there are two codes here the first one prints as false even if i input the correct string.
print output should be look like this, see below.

1. Yes, its al pacino but it prints as " No his not.


The other code below prints either true or false, could you advise what i missed here? Many Thanks.
#include <stdio.h>
int main(void)

{

 char actor[15];

  printf("Who is your favorite movie actor?\n");
  fgets(actor, 15, stdin);

if (strcmp(actor, "al pacino") == 0 ){   //  for this one it prints as false even if the string input is CORRECT
  printf("Yes its %s\n",actor);
}
else {
  printf("No, his not\n");
  return 0;
}

}


##################################################
For this one it prints either true or false
#################################################

#include <stdio.h>
int main(void)

{

 char actor[15];

  printf("Who is your favorite movie actor?\n");
  fgets(actor, 15, stdin);

if (strcmp(actor, "al pacino") != 0 ){   
  printf("Yes its %s\n",actor);
}
else {
  printf("No, his not\n", actor);
  return 0;
}

}
libchk is offline   Reply With Quote
Old Sep 1st, 2011, 10:03 PM   #2
Benoit
Security Specialist
 
Benoit's Avatar
 
Join Date: Sep 2004
Posts: 1,053
Rep Power: 14 Benoit will become famous soon enough
Re: advise on how to properly use strcmp

fgets will leave a newline character at the end of your input.

Try parsing your string using sscanf:

fgets( input, stdin );
sscanf( input, "%s", actor);

sscanf is also useful for validating input.
Benoit is offline   Reply With Quote
Old Sep 2nd, 2011, 3:49 AM   #3
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: advise on how to properly use strcmp

scanf() is not useful if you want to put spaces in the input string. For fgets() all you have to do is remove the newline character
if( line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline   Reply With Quote
Old Sep 2nd, 2011, 11:56 PM   #4
Benoit
Security Specialist
 
Benoit's Avatar
 
Join Date: Sep 2004
Posts: 1,053
Rep Power: 14 Benoit will become famous soon enough
Re: advise on how to properly use strcmp

Quote:
Originally Posted by Ancient Dragon View Post
scanf() is not useful if you want to put spaces in the input string. For fgets() all you have to do is remove the newline character
if( line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
You may also have to check for carriage return character, depending on the platform.
Benoit is offline   Reply With Quote
Old Sep 3rd, 2011, 7:06 AM   #5
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: advise on how to properly use strcmp

If the file is opened in text mode then the os will always convert "\r\n" into just a single character '\n', regardless of platform. In memory its just '\n', but on disk it could be any number of characters.
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline   Reply With Quote
Old Sep 4th, 2011, 12:23 AM   #6
Rashakil
Expert Programmer
 
Join Date: Dec 2006
Posts: 572
Rep Power: 11 Rashakil will become famous soon enough
Re: advise on how to properly use strcmp

Quote:
Originally Posted by Ancient Dragon View Post
If the file is opened in text mode then the os will always convert "\r\n" into just a single character '\n', regardless of platform. In memory its just '\n', but on disk it could be any number of characters.
For starters on any sane system it would be the C library in the process itself that converts the character, not the operating system... I mean kernel. Second, Linux or other Unixy operating systems' text mode is the same thing as binary mode, and fgets will write both '\r' and '\n' characters.
Rashakil is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
TicTacToe game not working properly xHero C++ 5 Jun 10th, 2010 12:12 AM
Properly caling JavaScript in ASP AFProgMan ASP 1 Jun 6th, 2009 6:25 AM
using "break" properly edik C++ 15 Mar 13th, 2009 9:15 AM
Python module written in C compiles properly but returns wrong value pythonscript Python 1 Jan 26th, 2009 8:24 PM
application failed to initialize properly 0xc0000005 ronaldr C++ 3 Dec 27th, 2007 8:24 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC