keep getting errors dont know how to fix them

palindrome

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2669 Views - Last Post: 06 April 2008 - 12:40 AM Rate Topic: -----

#1 ladiesman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-February 08

keep getting errors dont know how to fix them

Posted 05 April 2008 - 10:23 PM

can anyone help me please need some assistant. can you fix it for me. thanks

 
#include <stdio.h>
 #include<string.h>
 main()
{

		FILE * words; // a pointer declared to the file stream.
		char sc; // a character which will be input from the file.
// main code...

	myfileptr=fopen("words","r"); //open the file hopefully

	while((sc=gets(myfileptr))!=EOF) //get a char
	{

	if(sc)printf("%c",sc); //dump it to screen

	}
			printf("EOF\n");

	gets("%c",sc);
			if(strlen(sc)%2==0) printf("Word not palindrom");
			else
		{
				int i;
		for(i=0;i<strlen(sc);i+=1)
		if(sc[i]!=sc[strlen(sc)-1-i])
		{
				printf("Word not palindrom");
			break;
		}
		if(i==strlen(sc)) printf("The word is palindrom");
	
  }





my errors are:
g.c:10: error: 'myfileptr' undeclared (first use in this function)
g.c:10: error: (Each undeclared identifier is reported only once
g.c:10: error: for each function it appears in.)
g.c:12: warning: assignment makes integer from pointer without a cast
g.c:20: error: too many arguments to function 'gets'
g.c:21: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
g.c:25: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
g.c:26: error: subscripted value is neither array nor pointer
g.c:26: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
g.c:26: error: subscripted value is neither array nor pointer
g.c:31: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
g.c:33: error: parse error at end of input

Is This A Good Question/Topic? 0
  • +

Replies To: keep getting errors dont know how to fix them

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 10:33 PM

You set your file pointer to words, yet you try to reference it with myfileptr. So therefor, myfileptr does not exist when you try to use it.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 10:38 PM

Quote

g.c:20: error: too many arguments to function 'gets'
gets("%c",sc);



char * gets ( char * str );
int main(void) {
  char string [256];
  printf ("Insert your full address: ");
  gets (string);
  printf ("Your address is: %s\n",string);
  return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 10:51 PM

The errors are listed in the following format
<file>: <line number>: <Additional Info>

For example
g.c:10: error: 'myfileptr' undeclared (first use in this function)

This means that in file g.c, on line 10, you used a undeclared variable.


First Error
g.c:10: error: 'myfileptr' undeclared (first use in this function)
g.c:10: error: (Each undeclared identifier is reported only once
g.c:10: error: for each function it appears in.)

On line 10 (myfileptr=fopen("words","r");), you used the variable "myfileptr"; however, you haven't declared the variable.

Basically, the compiler doesn't know what "myfileptr" means. To correct this, you need to declare it as
FILE *myfileptr;



Second Error
g.c:12: warning: assignment makes integer from pointer without a cast

This one relates to
sc=gets(myfileptr)

I think you meant to use fgets() here

To fix this, you need to declare sc as
char sc[<INSERT MAX STRING LENGTH HERE>];



and then update the parameters. to

fgets ( sc, <MAX STRING LENGTH>, myfileptr);



Once this is done, you have to change the printf in line 15 to take a string instead of a character. IE, "%c" should become "%s"


Third Error
g.c:20: error: too many arguments to function 'gets'

I'm not quite sure what you meant to do here... Are you just ignoring the file, or did you mean to read in a new word from the console?


Fourth Error
g.c:25: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
g.c:26: error: subscripted value is neither array nor pointer
g.c:26: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
g.c:26: error: subscripted value is neither array nor pointer
g.c:31: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast

All of these will be corrected by changing sc to a char *.



Fifth Error
g.c:33: error: parse error at end of input

You are missing your closing bracket. IE, just add a "}"

Edit: Grr... I read gets() as fgets(). Updated response accordingly.

This post has been edited by Cerolobo: 05 April 2008 - 11:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 ladiesman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-February 08

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 11:12 PM

hey thanks so much now i have a 4 errors now. 3 of them are on one line now. can you take a look at it.
i wanted my program to do this
ada palindrome
back not palindrome
abba palindrome

#include <stdio.h>
 #include<string.h>
 main()
{

		FILE *myfileptr; // a pointer declared to the file stream.
	char *sc; // a character which will be input from the file.
// main code...

	myfileptr=fopen("words","r"); //open the file hopefully

	while((myfileptr=gets(myfileptr))!=EOF) //get a char
	{

	if(sc)printf("%s",sc); //dump it to screen

	}
			printf("EOF\n");

	gets("%s",sc);
			if(strlen(sc)%2==0) printf("Word not palindrom");
			else
		{
				int i;
		for(i=0;i<strlen(sc);i+=1)
		if(sc[i]!=sc[strlen(sc)-1-i])
		{
				printf("Word not palindrom");
			break;
		}
		if(i==strlen(sc)) printf("The word is palindrom");
	
  }
  }



g.c: In function 'main':
g.c:12: warning: passing argument 1 of 'gets' from incompatible pointer type
g.c:12: warning: assignment from incompatible pointer type
g.c:12: warning: comparison between pointer and integer
g.c:20: error: too many arguments to function 'gets'
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 11:15 PM

eh? :crazy:

I already pointed out what your error was with gets on the previous thread.
Was This Post Helpful? 0
  • +
  • -

#7 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 11:28 PM

Please reread my post on the original thread. I did do a edit a few minutes after the original post (sorry, I'm tired and I misread the code >.>)

Still, I'm not sure what you wanted to do with

gets("%s",sc);

on line 20
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 11:29 PM

The problem is, that gets can not take two arguments. Remove your %s & it should be fine.
Was This Post Helpful? 0
  • +
  • -

#9 ladiesman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-February 08

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 11:29 PM

View Postno2pencil, on 5 Apr, 2008 - 11:15 PM, said:

eh? :crazy:

I already pointed out what your error was with gets on the previous thread.



no2pencil
the thing is i dont understand because the word "words" is a file i'm putting into the program which is a long list of words i need to know if its palindrome or not.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 11:32 PM

View Postladiesman, on 6 Apr, 2008 - 02:29 AM, said:

no2pencil
the thing is i dont understand because the word "words" is a file i'm putting into the program which is a long list of words i need to know if its palindrome or not.


Again, gets does not take two arguments. That is why you are getting the error *too many arguments*.

char * gets ( char * str );

int main(void) {
  char string [256];
  printf ("Insert your full address: ");
  gets (string);
  printf ("Your address is: %s\n",string);
  return 0;
}

Was This Post Helpful? 0
  • +
  • -

#11 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 11:34 PM

To clarify,

gets()

waits for the user to input a string at the console.

I think you meant to use

fgets()

there, but I'm not sure.

Either way, it looks like you are attempting to print the entire file, and then check to see if the words are palindroms. Once you get to line 20, the file should be empty, so assumeing you wanted to check each word, you would need to either use

fseek()

or close and then open the file again.
Was This Post Helpful? 0
  • +
  • -

#12 ladiesman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-February 08

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 11:44 PM

View PostCerolobo, on 5 Apr, 2008 - 11:34 PM, said:

To clarify,

gets()

waits for the user to input a string at the console.

I think you meant to use

fgets()

there, but I'm not sure.

Either way, it looks like you are attempting to print the entire file, and then check to see if the words are palindroms. Once you get to line 20, the file should be empty, so assumeing you wanted to check each word, you would need to either use

fseek()

or close and then open the file again.



yes thats exactly wha i want to do cerolobo, i want to open the entire file and check if its palindrome.
Was This Post Helpful? 0
  • +
  • -

#13 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: keep getting errors dont know how to fix them

Posted 05 April 2008 - 11:53 PM

I cleaned up the syntax a bit, and added comments describing what I'm seeing.

Look at my last post for links to the functions on cplusplus.com

#include <stdio.h>
#include<string.h>
main()
{
  FILE *myfileptr;
  char *sc; // You need to either allocate memory for the string
			//   or make a array - char sc[MAX STRING LEN];

  myfileptr=fopen("words","r");

	// gets() should be fgets. Please read the link posted for the function
  while((myfileptr=gets(myfileptr)) != EOF)
  {
	if(sc)
	  printf("%s",sc);
	  
	// Here, you have printed the string to the screen. 
	// You will continue in this loop, until you get to the end of the
	// the file; however, you never check if the word is a palindrom
  }

  printf("EOF\n");

	// you can either fseek() to the start of the file, close the file and
	//   reopen it, or you can move the palindrom check into your while loop
	//   I would recommend moveing it into the while loop, since you are already
	//   have the string.
  gets("%s",sc);

  if(strlen(sc)%2==0) printf("Word not palindrom");
  else
  {
	int i;
	for(i = 0; i < strlen(sc); i += 1)
	  if(sc[i] != sc[strlen(sc) - 1 - i])
	  {
		printf("Word not palindrom");
		break;
	  }

	if(i==strlen(sc)) printf("The word is palindrom");
  }
}


Was This Post Helpful? 0
  • +
  • -

#14 ladiesman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-February 08

Re: keep getting errors dont know how to fix them

Posted 06 April 2008 - 12:02 AM

 
#include <stdio.h>
 #include<string.h>
 main()
{

		FILE *myfileptr; // a pointer declared to the file stream.
	char sc[5000]; // a character which will be input from the file.
// main code...

	myfileptr=fopen("words","r"); //open the file hopefully

	while((sc=fseek(myfileptr))!=EOF) //get a char
	{

	if(sc)printf("%s",sc); //dump it to screen

	}
			printf("EOF\n");

fseek( sc, 5000, myfileptr);
			if(strlen(sc)%2==0) printf("Word not palindrom");
			else
		{
				int i;
		for(i=0;i<strlen(sc);i+=1)
		if(sc[i]!=sc[strlen(sc)-1-i])
		{
				printf("Word not palindrom");
			break;
		}
		if(i==strlen(sc)) printf("The word is palindrom");
	
  }
  }


this progream does this:
smallish
smallness
smallpox
smalltime
Smallwood
smart
smarted
smarter
smartest
smartly
smartness
smash
smashed
smasher
smashers
smashes
smashing
smashingly
smattering
smear
smeared
smearing
smears
the next program finds if a word is palindrome or not, but i want it to directly get the list using the first program, and saynig is it is or not palindrome.

fseek( sc, 5000, myfileptr);
			if(strlen(sc)%2==0) printf("Word not palindrom");
			else
		{
				int i;
		for(i=0;i<strlen(sc);i+=1)
		if(sc[i]!=sc[strlen(sc)-1-i])
		{
				printf("Word not palindrom");
			break;
		}
		if(i==strlen(sc)) printf("The word is palindrom");
	
  }
  }


Was This Post Helpful? 0
  • +
  • -

#15 ladiesman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-February 08

Re: keep getting errors dont know how to fix them

Posted 06 April 2008 - 12:17 AM

does anyone know how to put what cerolobo code he fixed for me into a while loop. he mentiioned putting the part of the code that checks for palindrome into my while loop.

#include <stdio.h>
#include <string.h>
main()
{
  FILE *myfileptr;
  char *sc[359782]; // You need to either allocate memory for the string
			//   or make a array - char sc[MAX STRING LEN];

  myfileptr=fopen("words","r");

	// gets() should be fgets. Please read the link posted for the function
  while((myfileptr=fgets(myfileptr))!=EOF)
  {
	if(sc)
	  printf("%s",sc);
	  
	// Here, you have printed the string to the screen. 
	// You will continue in this loop, until you get to the end of the
	// the file; however, you never check if the word is a palindrom
 


  printf("EOF\n");

	// you can either fseek() to the start of the file, close the file and
	//   reopen it, or you can move the palindrom check into your while loop
	//   I would recommend moveing it into the while loop, since you are already
	//   have the string.
  fgets("%s");

  if(strlen(sc)%2==0) printf("Word not palindrom");
  else
  {
	int i;
	for(i = 0; i < strlen(sc); i += 1)
	  if(sc[i] != sc[strlen(sc) - 1 - i])
	  {
		printf("Word not palindrom");
		break;
	  }

	if(i==strlen(sc)) printf("The word is palindrom");
  }
}
}


i think i did but my terminal for my mac says:
g.c: In function 'main':
g.c:12: warning: passing argument 1 of 'fgets' from incompatible pointer type
g.c:12: error: too few arguments to function 'fgets'
g.c:29: error: too few arguments to function 'fgets'
g.c:31: warning: passing argument 1 of 'strlen' from incompatible pointer type
g.c:35: warning: passing argument 1 of 'strlen' from incompatible pointer type
g.c:36: warning: passing argument 1 of 'strlen' from incompatible pointer type
g.c:42: warning: passing argument 1 of 'strlen' from incompatible pointer type
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2