11 Replies - 2185 Views - Last Post: 11 November 2007 - 05:26 AM Rate Topic: -----

#1 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Enter-a-word program

Post icon  Posted 10 November 2007 - 06:19 AM

I'm trying to write a program to print out a word, when you enter it manually. For example, you enter "DiC", and the output is "DreamInCode".

I tried using the "if" structure. It's a very beginner, and probably wrong program, but that's what I'm doing - I'm learning. Hope you can guide me through writing this. :)

Here what I wrote:

#include <stdio.h>

int main()

{

char word;
printf("Enter the word\n");
scanf("c", &word);
if (word==dic)
	{ 
printf("DreamInCode\n");
	}
return 0;

}



The GCC compiler reports some error, and while I'm typing this, I'm trying to fix them. The code above is where I started writing the program.

Is This A Good Question/Topic? 0
  • +

Replies To: Enter-a-word program

#2 taure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-September 07

Re: Enter-a-word program

Posted 10 November 2007 - 07:56 AM

Hyia,

if you want to capture a string of characters, you should use:
char * word;

instead of
char word;


and then instead of
scanf("c", &word);

you should use
scanf("%s", &word);

This post has been edited by taure: 10 November 2007 - 07:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Enter-a-word program

Posted 10 November 2007 - 08:15 AM

Thanks, but it reports this and it doesn't work:

printest.c: In function ‘main’:
printest.c:11: error: ‘dic’ undeclared (first use in this function)
printest.c:11: error: (Each undeclared identifier is reported only once
printest.c:11: error: for each function it appears in.)
Was This Post Helpful? 0
  • +
  • -

#4 taure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-September 07

Re: Enter-a-word program

Posted 10 November 2007 - 12:57 PM

Ah yes, I missed that error ;-) if you write it like this, the compiler thinks the word dic is a variable name, and that's certainly not what you want. To suggest it that it is a string, try writing
if (word=="dic")

now that should work.

and btw I would suggest finishing the main() function with
system("PAUSE");
before running the program ;-) cheers!

This post has been edited by taure: 10 November 2007 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Enter-a-word program

Posted 10 November 2007 - 03:16 PM

Thanks again.

Now I have this.
The program does nothing after I input the word. It should print out "DreamInCode", but it does nothing, but "sh: PAUSE: command not found".
I tried with both
return 0;
and
system("PAUSE");
...and still doesn't print "DreamInCode".

:S

I compile this with the GCC compiler btw.
Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Enter-a-word program

Posted 10 November 2007 - 03:59 PM

system() passes the enclosed string to the command processor, so the commands that are acceptable are system-dependent. From the looks of it, you're either using a linux/unix environment, or you're using gcc under cygwin, so the "PAUSE" command is not available. If you're trying to hold the console open, take a look at the pinned post at the top of the C++ forum. And a system call is not the same as the return value for main; the return 0 statement still needs to be in there, but you're going to need something else to hold the window open before you return a value.

This post has been edited by jjhaag: 10 November 2007 - 03:59 PM

Was This Post Helpful? 0
  • +
  • -

#7 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Enter-a-word program

Posted 10 November 2007 - 04:58 PM

I'm using GCC on openSuSE 10.2
Was This Post Helpful? 0
  • +
  • -

#8 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Enter-a-word program

Posted 10 November 2007 - 05:04 PM

The "pause" command is for windows, so it's not portable. All it does on a console app is to hold the window open, waiting for the user to hit a key before returning to the calling program and (usually) just exiting.

If you've fixed up the original program with the suggestions in the previous posts by the other members, it should work. But take a look at that pinned thread I mentioned if you need to hold the console open for some reason. Otherwise, there's no reason to include the system() call.
Was This Post Helpful? 0
  • +
  • -

#9 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Enter-a-word program

Posted 11 November 2007 - 01:54 AM

Here's the whole code, changed by your suggestions:

#include <stdio.h>

int main()

{

char * word;
printf("Enter the word\n");
scanf("%s", &word);
if (word=="dic")
	{
printf("DreamInCode\n");
	}
return 0;

}


Was This Post Helpful? 0
  • +
  • -

#10 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Enter-a-word program

Posted 11 November 2007 - 02:21 AM

It looks like you've been given some bad advice here - You can't just create a pointer and expect to be able to fill some memory however you like (Your program actually crashed on my machine). You need your program to tell the computer how much space you want before you can use it. In this case, that means creating an array of characters.

Something like this -
char word[100];
printf("Enter the word\n");
scanf("%s", word);


Creating the array gives your program ownership of 100 char sized pieces of memory lined up in a row. You can safely access and modify anything in this chunk of memory, without the danger of interfering with memory that's likely to be owned by some other running process (Such as your O/S), and causing a segmentation fault.


The other problem you have, is that you can't compare strings in C using the == operator, you need the strcmp function. Have a look here for its usage -
http://www.cpprefere...ing/strcmp.html

* The reason for this is that the C language does not know how to compare arrays with each other, instead, it converts arrays into the memory address of their respective first elements, and compares those memory addresses.

This post has been edited by Bench: 11 November 2007 - 02:34 AM

Was This Post Helpful? 0
  • +
  • -

#11 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Enter-a-word program

Posted 11 November 2007 - 02:24 AM

Aww, hell...I guess I didn't really look at the previous posts closely enough. You're going to need to change a few things to get this working, I think.

Instead of just a char*, you're going to need to declare a char array to hold the input. The pointer will just hold an address, while you need to hold a series of characters - and unless you were dynamically allocate space and set the char* word to point to it, you're not going to be able to store a string in your variable word as it stands. So use a static array instead; you'll need to have enough elements to accept any reasonably sized input, maybe like 100 elements to start with:
char word[100];



You'll also need to compare the two strings either element by element, or by using the strcmp (string compare) function from string.h - I would recommend using the function. This function returns a zero if the two strings are identical, so your if statement would look something like this:
if ( strcmp( "dic", word ) == 0 ) {...


If you were to use the equality operator (==) to compare the character array to "dic", you'd actually be comparing the address of the first element of the character array to "dic", which would return false. And if you were still just using a pointer to store the inputted string (which wouldn't actually store it, since no space is actually alllocated for the string), you'd again be comparing to an address, so that wouldn't work either.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#12 Boyan  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Enter-a-word program

Posted 11 November 2007 - 05:26 AM

Thanks everyone! :genius:

It works great, and I learned something new. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1