1.4 Ctrl+D and Ctrl+Z issues (1.4)

APalley

Well-Known Member
When I use switch-case, to output the amount of the characters I have to switch (c) for 26*2 times because of 26 upper and lower case letters. I wonder whether there is a better and more convenient way to solve this problem.
Following is my code(I didn't write down all cases for letters):
int sum1 = 0;
char c;
while((c = getchar()) != 4)
{
switch (c)
{
case'65':
case'66':
case'67':
sum1++;
}
printf("The amount of characters:%d\n",sum1);

@APalley
Think about the inverse logic. You should not be limiting yourself to the alphabet. Rather, count anything (besides ctrl) as characters and count words using whitespace.
 

shstanley

Member
C++ Student
I have many questions about Ex1.4.1
Q1.what is the difference between
c = getchar();while (int(c) != 4)
and
c = getchar();while (c != 4)
???which should be right???
Q2.why should we use code such as: while (c != 26) and while (c != EOF)...?
The question asks us to terminate when ^D(with ASCII 4) is press,so i think the code :while (c != 4) is enough,but it seems that my opinion is wrong.Many people write code like :while (c != 26) and while (c != EOF)...
thanks!
@APalley
 
Last edited:

Lois Hu

Member
C++ Student
Hi @APalley
I am working on Mac OS Xcode, and I found that ^Z does not work in the console. As mentioned above, ^D works perfectly and it is recorded as -1 here. However when I type in ^Z, nothing happens, even if I have tried the values discussed above. I am working on 1.4.6 and I am wondering whether I can still keep the ^D way to end the input.
 

APalley

Well-Known Member
Hi @APalley
I am working on Mac OS Xcode, and I found that ^Z does not work in the console. As mentioned above, ^D works perfectly and it is recorded as -1 here. However when I type in ^Z, nothing happens, even if I have tried the values discussed above. I am working on 1.4.6 and I am wondering whether I can still keep the ^D way to end the input.
If you're checking for both ctrlZ and EOF there's no reason it shouldn't work. Using ctrlZ is preferred for these two exercises.
 

Lois Hu

Member
C++ Student
Is there anyone using Xcode? I have problem with typing ctrlZ in the console. Does anyone also have this problem?
 

Michael Barnes

New Member
C++ Student
Hi @APalley

It took me some time but I have a program that works with the exception of accounting for multiple consecutive spaces. I'm not sure how to prevent my word counter from adding the unnecessary count. Also, I cannot figure out why but for some reason my character counter also receives an additional count when multiple spaces are entered. I apologize if you have already addressed this in the thread.
 

Pavlos Sakoglou

Well-Known Member
C++ Student
Hi @APalley

It took me some time but I have a program that works with the exception of accounting for multiple consecutive spaces. I'm not sure how to prevent my word counter from adding the unnecessary count. Also, I cannot figure out why but for some reason my character counter also receives an additional count when multiple spaces are entered. I apologize if you have already addressed this in the thread.
See this:
https://www.quantnet.com/threads/exercise-1.23604/#post-168706
 

Jinzhe Luan

New Member
C++ Student
Please read the prior posts...

You should not be calling getchar() twice. Also, think about your logic...Should this be an OR or an AND?
Hi,

I still don't quite understand why we should use && although, I check that && will work and || will not. I thought if we want the while loop to end either we enter EOF or has a ASCII value of 26 shouldn't we use or? I check the previous post and still confused.
 

APalley

Well-Known Member
Hi,

I still don't quite understand why we should use && although, I check that && will work and || will not. I thought if we want the while loop to end either we enter EOF or has a ASCII value of 26 shouldn't we use or? I check the previous post and still confused.
The condition in the while statement is not the termination condition, but the continuation condition. In plain English: Loop while condition A is true AND condition B is true
 

_uxi_

Member
C++ Student
In resolution to the ongoing issue about cntrl-Z and cntrl-D:

getchar() will return EOF depending on the OS if it's on a new line -- UNIX based OS EOF is defined as ^D. Dos based, ^Z. Otherwise, it will return the ASCII code for that combo. So 4 for ^D, and 26 for ^Z. In short, it really all depends on the driver/OS, and how/when it interprets a control as an EOF or ASCII value (which have some legacy use)...

So a useful thing to do would be to check for EOF in combination (OR) with the respective ASCII code.
@Pavlos Sakoglou
Hi, APalley.Hi, Pavlos
for" input=getchar()" in Sec4_Ex1,2,3,and 6,
In Xcode,I tried while(input!=4) and end with {"crtl+D","crtl+D+enter","crtl+Z"}, none of these works.
It only works for while(input!=EOF), and end with crtl+D in the console, which I code in my homework. Will it hurt scores of homework?
 

Pavlos Sakoglou

Well-Known Member
C++ Student
@Pavlos Sakoglou
Hi, APalley.Hi, Pavlos
for" input=getchar()" in Sec4_Ex1,2,3,and 6,
In Xcode,I tried while(input!=4) and end with {"crtl+D","crtl+D+enter","crtl+Z"}, none of these works.
It only works for while(input!=EOF), and end with crtl+D in the console, which I code in my homework. Will it hurt scores of homework?
That's ok. Some systems interpret EOF as 4 and vice versa.
 
On the Exercise 1.4.1, should getchar() be used to read the sentences that I am inputting?
Also getchar() is used to read only one character at a time. It cannot hold a word or a full sentence. My question is how am I supposed to count the number of words that I input?

Please help.:sick:
 

APalley

Well-Known Member
On the Exercise 1.4.1, should getchar() be used to read the sentences that I am inputting?
Also getchar() is used to read only one character at a time. It cannot hold a word or a full sentence. My question is how am I supposed to count the number of words that I input?

Please help.:sick:
getchar() input a single character. You will need to build a loop that reads in one character at a time, using if statements to determine the numbers of works, lines, and characters. For example, you can detect a new word when a space is inputted.
 

CJZ

Member
C++ Student
Hi, @APalley. I have a question with ^D.

In 1.4.2, do-while loop, a line with a single ^D will be counted as a new line, but in while loop, it works fine. In this situation, I cannot simply minus 1 to the word count because if ^D is in the same line with some other characters, the word count still correct... what should I do with it?
 

APalley

Well-Known Member
In 1.4.2, do-while loop, a line with a single ^D will be counted as a new line, but in while loop, it works fine. In this situation, I cannot simply minus 1 to the word count because if ^D is in the same line with some other characters, the word count still correct... what should I do with it?
Determining what to do here is the crux of the exercise :) think in terms of additional if statements
 

CJZ

Member
C++ Student
Determining what to do here is the crux of the exercise :) think in terms of additional if statements
I wonder why this would happen? In while loop, a crtl-D in a new line will not be counted as a word, but in do-while loop, it will be counted as a word.

I add a condition in do-while word-counting that requires the current character cannot be 4, and it is correct now.
 
Top