program help



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

/* copy input to output, replace each string of one or more blanks by a single blank */

int main()

 {

   int c, n1 = 0;
   
   while ((c = getchar()) ! = EOF)  {
   while (c == ' ')
   ++n1;
   n1 = ' ';
   putchar(c);
   
   printf( "%d %n1", c, n1);
   
  } 
   
 }



I need help with the above code because it won`t compile, and don`t know why. Thanks
while ((c = getchar()) ! = EOF) // != not ! =

while ((c == getchar()) != EOF)
Last edited on
Thats weird... I thought c++ was white-space friendly... Why such discrimination against operators?
Last edited on
Sure, There is a lot of white-friendly things as you say, but it has never been the case with operators.

You can do cin >> x; // white space is not showing up, but there is 15 spaces between >> and x. And that works fine.

but you cant do cin > > x; with a space between the operator. Would be very illogical in my opinion if that was the case.
Last edited on
Yeah... cin > > x; looks funny :D
closed account (D80DSL3A)
The (c = getchar() part is probably correct (so = not ==).
He's using getchar() to obtain a value and store it in c which is then compared to EOF, a value supplied via macro in stdio.h
See http://www.cplusplus.com/reference/cstdio/
Oh I didint know that, thank you @fun2code :)
closed account (D80DSL3A)
You're welcome. We're all learning here.
My current program is actually as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

include <stdio.h>

/* copy input to output, replace each string of one or more blanks by a single blank */


int main()
{
	
int c, x; 


while ((c = getchar() != EOF))
{ 
	
	if (c == ' ') {  
	while ( scanf ( "%c", &x ) == 1 )  // Read white space
	;                                  // Do nothing
	putchar(' ');                      // Replace with single blank
	
   }
	
   putchar (c);
	
}

return 0;


}




It won`t work properly though, anyone else can write it?
Please, this exercise seems tough…

Now I get this output:

this is a string^D


When pressing ctrl+D
Here is my current solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <stdio.h>



 int main(void)
{
	int c, inspace;
	inspace = false;
	
	while ((c = getchar()) != EOF)
		
	{
		if ( c == ' ') 
		{  
			if (inspace == false)  
			{
			inspace = true;	
			putchar(c);
		    }
	    }
		
     else     { 
		        inspace = false;
		        putchar (c); 
			  } 
		
	 }
	
	return 0;
	
	
}
	
	
	





Personally I didn`t find it very attractive after all, but comments or suggestions are welcome.
Last edited on
Would it be 'OKAY' if we use char instead of int? That would make code more convenient.
Why do you use EOF here?
EOF is for End of File, right?
So it doesn't make any sense.

Would it be 'OKAY' if we use char instead of int? That would make code more convenient.

I just wondered why that was an int at all
Topic archived. No new replies allowed.