program help

Hello,

Any ideas how I would test this program are welcomed:

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
40
41

#include <iostream>

/*
      for ( i = 0; i< lim-1 && (c = getchar()) != '\n' && c!= EOF; ++i)
      s[i] = c;
*/


int main(void)

{

   char s[100];
   int i;
   int lim = 100;


   while ( i< lim-1 )
  {
     c = getchar();
     
     if ( c = '\n' )
     
      break;
      
      else if ( c = EOF )
      {
         return 0;
      }
      
      else
      
        s[i++] = c;
        
    }
    
  return 0;
  
  }



// Write a loop equivalent to the for loop without using && or ||
Last edited on
Put both bits of code into different functions. Have main() call one function. Compile the program and feed an input file to it. Save the output. Now change main() to call the other function. Recompile and rerun, saving the output to a different file. Now compare the files.

You have a common bug on lines 23 and 27: use == instead of =.
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
40
41
42
43
44
45
46
47
48
49
50

#include <iostream>

void f()
{
      for ( i = 0; i< lim-1 && (c = getchar()) != '\n' && c!= EOF; ++i)
      s[i] = c;
      
}    


 int ff()
{
      
   char s[100];
   int i;
   int lim = 100;


   while ( i< lim-1 )
  {
     c = getchar();
     
     if ( c == '\n' )
     
      break;
      
      else if ( c == EOF )
      {
         return 0;
      }
      
      else
      
        s[i++] = c;
        
    }
    
  
}


int main(void)

{

    f();
  
}



It`s too hard to test it right now, maybe some day I will ( maybe I`m just lazy)
Last edited on
Topic archived. No new replies allowed.