I am reteaching myself C from the ground up. I found a programming project in the book that stated to modify an old piece of code that printed rows of squares(to be shown below) so that after every 23rd square the program prompts the user to press the "Enter" key to continue. Now when I originally tried to write the program, upon adding in lines of code to try and test what was going on, I found that the newline character was being read by getchar (as well as scanf in slightly different versions that I tried). That is when I posted. Upon reading your reply post, I got the idea to use an extra call of getchar to assign the newline character to a char variable then assign an arbitrary character to that variable thus clearing the old newline character.
Also, I'm not quite sure what you're referring to concerning the newline character originating from the input line. Wouldn't the if statement on line 12 of the code posted previously limit any "seen" newline charachters to those that come from the call of printf on line 19 of the same code above?
original code that just prints squares:
[code]
#include <stdio.h>
int main(void){
int i,n;
printf("\nPlease enter the number of squares you wish to display:");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%10d\t%10d\n",i,i*i);
return 0;
}
[\code]
New functional code that I have written
Code:
#include <stdio.h>
int main(void){
int i,n,j=0;
char ch='A';
printf("****Square2 v2.01****\n");
printf("\nPlease enter the number of squares you wish to display:\n");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++){
if(i%24!=0)
printf("%10d %10d\n",i,i*i);
else{
if(j==0){
ch=getchar();
j++;
}
printf("Please press enter:");
ch='A';
while(ch!='\n')
ch=getchar();
printf("%10d %10d\n",i,i*i);
}
}
printf("\n****Program Terminated****\n");
return 0;
}
What I would expect from the program I posted before was that after 23 entries, the program would prompt the user to press enter to continue. What I was seeing before I made the modification that fixed it was that the program was reading a new-line character from somewhere before (even when I had taken out all occurences of it written in the code) after the prompt was displayed, the proceeded to display the rest of the entries starting with entry 24 on the same line as the prompt was displayed. This is the expected output which I get with the corrected program with an entry of 28:
maxwellboltzmannsky@ubuntu:~$ ./square2_v2.01
****Square2 v2.01****
Please enter the number of squares you wish to display:
52
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
11 121
12 144
13 169
14 196
15 225
16 256
17 289
18 324
19 361
20 400
21 441
22 484
23 529
Please press enter:
24 576
25 625
26 676
27 729
28 784
And this is what I got with the code similar to what I posted before (the same error occured):
maxwellboltzmannsky@ubuntu:~$ ./square2_v2.01
****Square2 v2.01****
Please enter the number of squares you wish to display:
52
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
11 121
12 144
13 169
14 196
15 225
16 256
17 289
18 324
19 361
20 400
21 441
22 484
23 529
Please press enter: 24 576
25 625
26 676
27 729
28 784
In the first the program prompted for the user to press enter. In the second, it just printed what is shown without prompting for enter.
Also, I hope my indentation is a little better.