LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2002, 02:06 AM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
Help with the following simple C program .....


Hi. what did I do wrong in the following? I am simply trying to have this program respong to a typed word command such as 'newyork' or 'chicago'. I am confused as I am new and don't have my C book handy right now.

#include <stdio.h>

#define NEWYORKTIME 555
#define CHICAGOTIME 655

int main(void)
{


char choice[10];

puts("Enter time: \'newyork\' or \'chicago\'");
scanf("%s", &choice);

if(choice == newyork)
printf("\nTime in New York is %d\n", NEWYORKTIME);

else if(choice == chicago)
printf("\nTime in Chicago is &d\n", CHICAGOTIME);

return 0;
}

I get the following gcc errors > but don't understand why I would have to 'declare' 'newyork' and 'chicago'
as they are not variables

time.c: In function `main':
time.c:15: `newyork' undeclared (first use in this function)
time.c:15: (Each undeclared identifier is reported only once
time.c:15: for each function it appears in.)
time.c:18: `chicago' undeclared (first use in this function)

Last edited by purpleburple; 09-13-2002 at 02:10 AM.
 
Old 09-13-2002, 04:04 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
you ARE using them as variables, not text strings. enclose them in quotes. i'd recommend using a seitch block here instead of if's too

Last edited by acid_kewpie; 09-13-2002 at 04:08 AM.
 
Old 09-13-2002, 04:12 AM   #3
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
You can't compare character arrays with just the == operator. You should do something like:

if (stricmp(choice, "newyork") == 0)
printf("\nTime in New York is %d\n", NEWYORKTIME);
 
Old 09-13-2002, 04:40 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
heh, i already said that, but then wasn't totally sure if it was true.. so i changed it. it's strcmp though not stricmp?
 
Old 09-13-2002, 07:07 AM   #5
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
stricmp is the windows version of a case insensitive string compare. The linux equivalent of that would be strcasecmp, which in my opinion looks kinda confusing because the name indicates it IS checking for the upper or lower case of the string.
 
Old 09-13-2002, 10:52 PM   #6
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thanks guys!!!! I was enclosing it in single quotes and that wasn't working ... like this > if( variable == 'newyork') . So I am supposed to use double quotes "newyork" ...... ahh ok! Also since I am new to C I didn't know of a case-insensitive function like the above. I was just going to give an error if user entered in a case-insensitive way and then ask them to type in ALL lower case. But for practice I am going to design my own function that lets user enter in either lower or upper or combination of the two. Basically I am writing a small program that will except input such as .... 'onepm newyork' and then gives the user the GMT time.

thanks guys!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple program help! rohitkara Linux - General 1 05-24-2005 08:57 PM
simple c program liguorir Linux - Software 4 05-29-2004 06:22 AM
simple C program problem mined Programming 2 05-08-2004 05:42 AM
Compiling a simple C++ program kbeaver Programming 27 02-06-2004 08:02 PM
simple program bprasanth_20 Programming 5 10-29-2003 12:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration