Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

control-d

Status
Not open for further replies.

thelearner

Programmer
Jan 10, 2004
153
0
0
US
Hi,

I have a program from the tutorial that is suppose to count
the lower case character. The tutorial said that I have to
enter control-d or control-z after the last line of my text
to indicate end of file. When I excecute my program, I got
a blank screen. Then I typed a line as below and hit enter.

This is my string. control-d

I did not get a message 'Press any key to continue ...' like in my other programs. Instead the cursor moved to the
next line. Look like it did not hit the end of file. How is
it suppose to work?
Thanks in advance

Here is my program
#include <stdio.h>

int main()
{
int i, j;
char input[120];
char *pt;
char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
int alphaCount[26];

for (i = 0; i < 26; i++)
{
alphaCount = 0;
}

while ((pt = gets(input)) != NULL)
{
for (i = 0; i < 120 && input != '\0'; i++)
{
for (j = 0; j < 26; j++)
{
if (input == alphabet[j])
{
alphaCount[j]++;
}
}
}
}
for (i = 0; i < 26; i++)
{
printf("found %d occurrences of %c\n", alphaCount, alphabet);
}

system("PAUSE");
return 0;
}







 
> Look like it did not hit the end of file.

That's because Control-D is the end of file.

Control-D does not mean the string, "Control-D."

It means to press and hold the key on your keyboard labeled "Ctrl"; then, still pressing the Ctrl key, press the key on your keyboard labeled "D". That generates the character corresponding to the end of file.
 
Though if you're expecting the string, "Press any key to continue ...", that would suggest that you're using Windows, which uses a Ctrl-Z to indicate End Of File (EOF).

Again, that would be:
1) press the key labeled "Ctrl"
2) press the key labeled "Z"
3) release the two keys in any order
 
> char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
If you write
Code:
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
The compiler will work out the size of array from the size of the string.

> for (i = 0; i < 26; i++)
Way too many uses of 26 in the code.
Use a symbolic constant to add meaning to the code, and perhaps make future change easier.
Code:
#define ALPHABET_SIZE 26
//..
  int alphaCount[ALPHABET_SIZE];
//..
  for (i = 0; i < ALPHABET_SIZE; i++)

> while ((pt = gets(input)) != NULL)
Never use gets()

--
 
Thank for the tip, Salem.
ChipperMDW, I tried pressing "ctrl" + "Z", it added ^Z
after my text. Still did not show "Press any key to
continue ...".

Thanks
 
> ChipperMDW, I tried pressing "ctrl" + "Z", it added ^Z
So which operating system and compiler do you have?


--
 
Hi,

I'm using DEV-c++ v.9.9.0 on window2000.

Thanks
 
> I'm using DEV-c++ v.9.9.0 on window2000.
I just tried it and it seems that you need to press the enter key AFTER pressing ctrl-z.

--
 
I hit 'enter key' after 'ctrl' + 'z'. The cursor move to
the next line and notthing else happen (beside the ^z that
I got from pressing ctrl + z).
 
Did you try it with Ctrl-D, Enter?


Note that at this point, your fingers should be doing this:

1) press Ctrl
2) press Z (or D)
3) release Ctrl and Z (or D; whichever you used in the last step)

(At this point, you're not pressing any keys)

4) press Enter
5) release Enter


It probably wouldn't make much difference, but you shouldn't be pressing the "Shift" key at any point; the D and Z are shown as capital letters, but you don't enter them that way.
 

Yes, I tried both and did exactly as you mentioned.
(the other replied, CLICK9, was from me too. I use someone else computer and did not relieze about the userid until it's too late.)
 
I don't know what to tell you, then.

Try various other Ctrl-letter sequences and see if one of those works as end-of-file.

Read the documentation for the shell or IDE you're using.

Check on the DEV-c++ website and see if anyone had similar problems.

Maybe ask on one of the C++ forums at Tek-Tips.


Now, you are putting Ctrl-Z and Ctrl-D on a line of their own, aren't you?
Code:
This is some input^Z
wouldn't work. You'd have to use
Code:
This is some input
^Z
 
Thank, it works now. Earlier I did not hit ctrl + 'Z' on its own line. Trickly stuff!

Thanks all for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top