LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 03-16-2003, 11:02 AM   #1
OrganicX
Member
 
Registered: Aug 2002
Posts: 105

Rep: Reputation: 15
Problem in C: arrays, functions and strings


Im trying to write a program that allows the user to input

y, Y, YES, yes, or any combination of capitalization of yes such as YeS, yES etc. in order to go through and continue the program.




My problem is sending the array to a function, Im not sure how to do it. Do I have to send a pointer i.e array[0] or something else? I tried a few different things but cant get it to work. This is my code so far.



------------
#include<stdio.h>
#include<string.h>

int ckans(char [])

int main()
{
char ans[3];
int x;

printf("\nGo through program (y/n):\t");
scanf("%s", ans);

if((ans[1] == '\0')&&(ans[2] == '\0'))
{
if((*ans == 'y')||(*ans == 'Y'))
{
printf("\nWelcome");
printf("\n\n");
}
}
else
{
x = ckans(ans);
}



if(x == 1)
{
printf("\nWelcome to the program");
printf("\n\n");
}
else
{
printf("\nBYE");
printf("\n\n");
}


return 0;
}


int ckans(char ans[])
{
if((*ans == 'y')||(*ans == 'Y'))
{
if((ans[1] == 'e')||(ans[1] == 'E'))
{
if((ans[2] == 's')||(ans[2] == 'S'))
{
if(ans[3] == '\0')
{
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else
{
return 0;

}

}


--------

Thanks for any advice (I may use toupper and anything else to clean the code up a little later but for now I just want to be able to pass a character array to a function).
 
Old 03-16-2003, 11:15 AM   #2
Palin
Member
 
Registered: Feb 2003
Location: A Meatlocker, well feels like one
Distribution: Gentoo
Posts: 292

Rep: Reputation: 30
int ckans( char *)

you treat it like a pointer just pass in ans to the function like this

ckans( ans );

you might also want to look at using the string functions like strcmp contained in cstring.h might make the checking easier.
 
Old 03-16-2003, 02:51 PM   #3
OrganicX
Member
 
Registered: Aug 2002
Posts: 105

Original Poster
Rep: Reputation: 15
Im sorry, that still doesnt work, thanks for the advice anyway. I dont know what else to do because what you told me to do should work, maybe it's somthing else about my code.

The error I receive is

undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Last edited by OrganicX; 03-16-2003 at 03:02 PM.
 
Old 03-16-2003, 03:54 PM   #4
Palin
Member
 
Registered: Feb 2003
Location: A Meatlocker, well feels like one
Distribution: Gentoo
Posts: 292

Rep: Reputation: 30
works either way for me the only thing I found was you are missing
a semicolon after your function declaration.
 
Old 03-16-2003, 08:16 PM   #5
OrganicX
Member
 
Registered: Aug 2002
Posts: 105

Original Poster
Rep: Reputation: 15
I have the semicolon in my original code, I just forgot it when I wrote it on this thread.

It must be something else besides my code I guess.

Thanks for your help though
 
Old 03-16-2003, 08:18 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
First things first... In every case you mentioned above, you continue the program if the first character is a 'y'. If this prompt is a simple yes/no prompt, you may well be able to simply look at that first character. Granted, that means you would accept "yelp" as a yes response. It would make your life simpler though.

Yes, use toupper() or the string based equivalent. If you don't want to modify the user's input, make a copy of it with strcpy(), the make it all one case, and use that for comparison.

As for pointers to strings and arrays... The name of an array (with no subscript) is functionally equivalent to a pointer to the first element. Take this code:
Code:
int main()
{
  char char_array[5] = "Test"
  char *char_pointer;

  char_pointer = &(char_array[0]);

  if (char_pointer == char_array)
    printf("Success! The two are equal!\n");
  else
    printf("Hmmm... not equal... Very strange\n");

  return 0;
}
I have no idea what the error you get means. Is the code you posted EXACTLY what you're trying to compile?
 
Old 03-16-2003, 10:51 PM   #7
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Mmm, if you are programming in Dos then try to use strupr for makeng upcase and then compare your string with, for example, "YES" or "NO". I'm not sure that there would be such a function available in Linux libraries.
 
Old 03-16-2003, 11:37 PM   #8
OrganicX
Member
 
Registered: Aug 2002
Posts: 105

Original Poster
Rep: Reputation: 15
Quote:
First things first... In every case you mentioned above, you continue the program if the first character is a 'y'. If this prompt is a simple yes/no prompt, you may well be able to simply look at that first character. Granted, that means you would accept "yelp" as a yes response. It would make your life simpler though.

I wrote some similar code yesterday, like you said, if the user typed yoyo or anything else that started with a y, it still worked. I decided to make my code not work if a user typed anything besides y or yes, just to see if I could.

the code worked until I tried to pass the string to a function.
 
Old 03-17-2003, 01:18 AM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I am an absolute idiot! It was staring me right in the face.

You declare ans as:
char ans[3];

Why is that a problem? When you store the string "yes" in it, you don't have room for the null terminator. When you declare strings like that, you MUST give it one character more than the longest string you anticipate getting. So change it to (at least):
char ans[4];
 
Old 03-17-2003, 02:53 PM   #10
OrganicX
Member
 
Registered: Aug 2002
Posts: 105

Original Poster
Rep: Reputation: 15
Im sorry Dark_helmet, I took care of that too (the null character \0 at the end of the string) I made it char ans[4] and it still doesnt work, I still get the same error message.
 
Old 03-17-2003, 03:04 PM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Ok, that's odd, because I took your code as you posted it with two exceptions:
1) I added the semi-colon after the prototype
2) Changed the ans declaration to be 4 characters

Then I used "gcc -o test test.c". I got no warnings or error on compilation. Then I tried giving it "yEs", and it said "Welcome to the program".

Post the exact code you're using, otherwise I'd be grasping at straws.

For the compiler message, that suggests a problem with the system code. I don't know how familiar you are with the background stuff, but functions beginning with two underscores are typically stuff inlcuded with libraries, system-level code, etc. Since I don't get the error, I have to ask if you tried installing/upgrading/modifying anything (like glibc, gcc, or anything that has to do with compilation)??
 
Old 03-17-2003, 03:30 PM   #12
OrganicX
Member
 
Registered: Aug 2002
Posts: 105

Original Poster
Rep: Reputation: 15
no I just installed the os the other day (redhat 8) I decided to install everything and didnt touch anything since. I didnt have any problems compiling any of my other code. Im going to try some different code and see if my compiler has a problem passing strings to functions for some reason, if it does im at a loss because i dont know much if anything about the 'background stuff'.

thanks for your help and advice though.
 
Old 03-17-2003, 04:04 PM   #13
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Well, that makes it even more odd, because I compiled it under a stock Red Hat 8.0 install myself. Maybe not technically stock, because I've been playing with it some, but no huge modifications that would affect the compiler. Well, I wish you luck, because I'm out of suggestions.
 
Old 03-17-2003, 04:50 PM   #14
OrganicX
Member
 
Registered: Aug 2002
Posts: 105

Original Poster
Rep: Reputation: 15
Well the good news is my code works, the bad news is something is wrong with my gcc compiler in redhat 8.

I took the code I wrote on this thread and also added the semicolon and increased the array subscript, I compiled it on my gcc compiler in redhat 7.3 on a different machoine and it works fine.

I also wrote a different program where I tried to pass an integer array to a function on my gcc compiler in redhat 8 and received the same error. That same code on my gcc compiler in redhat 7.3 worked fine.

I dont even know where to begin to fix this problem with the compiler

this may be nothing but it also may be something significant,

I am not too familiar with vi or in this case vim, I touched something (shift # i think) and all the words 'char' are highlighted now. I poked around but dont know how to unhighlight them. If I open a new vi session and write the word char it get highlighted automatically now. I dont know how to stop this feature and is the only thing I can think of thats out of the ordinary.
 
Old 03-17-2003, 05:10 PM   #15
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
You probably activated some sort of syntax highlighting scheme... I don't use vi, so I don't know for sure. It probably won't hurt anything. I use emacs, and I know both are capable of highlighting reserved words, literal strings, comments, and all that kind of thing. They do it with regular expressions; not by modifying the original file.

As for the compiler, open up your package manager and remove all the development tools listed. Then I'd suggest restarting, and reinstalling them. Hopefully, that will fix the problem.
 
  


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
bash script help (arrays and strings from files) nkoplm Programming 14 12-02-2005 10:50 AM
using arrays and functions and trying to initialize a point in the array mshinska Programming 1 11-11-2005 03:21 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 05:04 AM
accessing functions using strings in runtime shyam_d_sundar Programming 3 05-18-2005 09:02 AM
Arrays of Functions and Classes problem. (C++) -=zAe=- Programming 2 02-24-2005 11:21 AM


All times are GMT -5. The time now is 01:15 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration