program help

Apr 4, 2015 at 3:42pm


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
Apr 4, 2015 at 3:44pm
while ((c = getchar()) ! = EOF) // != not ! =

while ((c == getchar()) != EOF)
Last edited on Apr 4, 2015 at 4:01pm
Apr 4, 2015 at 3:48pm
Thats weird... I thought c++ was white-space friendly... Why such discrimination against operators?
Last edited on Apr 4, 2015 at 3:49pm
Apr 4, 2015 at 3:52pm
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 Apr 4, 2015 at 3:53pm
Apr 4, 2015 at 3:55pm
Yeah... cin > > x; looks funny :D
Apr 4, 2015 at 4:00pm
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/
Apr 4, 2015 at 4:01pm
Oh I didint know that, thank you @fun2code :)
Apr 4, 2015 at 4:04pm
closed account (D80DSL3A)
You're welcome. We're all learning here.
Apr 4, 2015 at 9:19pm
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?
Apr 5, 2015 at 11:18am
Please, this exercise seems tough…

Now I get this output:

this is a string^D


When pressing ctrl+D
Apr 5, 2015 at 2:38pm
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 Apr 5, 2015 at 2:41pm
Apr 5, 2015 at 2:57pm
Would it be 'OKAY' if we use char instead of int? That would make code more convenient.
Apr 5, 2015 at 3:39pm
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.