Jump to content

breaking a compond...error

- - - - -

  • Please log in to reply
1 reply to this topic

#1
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
I am doing some practice to do some breaking of compounds - like this

Quote

Enter a compound: H2SO4
H2
S
O4
Enter a compound: Bye
GoodBye!


I have done the code, but it gives me errors and am unsure about it, would you be able to assist?


#include <stdlib.h>

#include <string.h>

#include <ctype.h>



int main(void)

{

	

	char s[81] = *s[i];

	int len = strlen(s), i;


	for (i = 0; i < len; i++)

	s[i] = toupper(s[i]);

	puts(s);


	while (s[i] != 0)

	{

		printf("\nEnter a compound:");

		scanf("%c", &s[i]);


		if(s[i] == "Bye")

		{

		printf("Goodbye!\n");

		break;

		}	

		

		for( i = 0; i < len; i++)

		if(isdigit(s[i]))

			printf("%c", s[i]);

		printf("\n");


		system("pause");

		return (0);

}


Error from the terminal

Quote

*****@ubuntu:~/week10$ gcc -o compound compound.c
compound.c: In function ‘main’:
compound.c:9: error: ‘i’ undeclared (first use in this function)
compound.c:9: error: (Each undeclared identifier is reported only once
compound.c:9: error: for each function it appears in.)
compound.c:18: warning: incompatible implicit declaration of built-in function ‘printf’
compound.c:19: warning: incompatible implicit declaration of built-in function ‘scanf’
compound.c:21: warning: comparison between pointer and integer
compound.c:34: error: expected declaration or statement at end of input
*****@ubuntu:~/week10$


*** please note, name has been removed for privacy reasons.

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

char s[81] = *s[i];

int len = strlen(s), i;

You're using i in first line, before you've even declared it. Also, this doesn't work:

if(s[i] == "Bye")

s is a single dimension array, so it can only contain a single string. When you index your string, you get 1 character and that can never equal to anything more than 1 character. Also, you appear to be missing a closing curly bracket for while loop.

If you want to compare char* (strings) then you'll have to use strcmp which is located in string.h
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users