-4

I've look everywhere online, I'm completely lost and starting to really get pissed off. Nothing I see anywhere has anything to do with the error I'm seeing, it's as if the whole world has this problem, but a completely different solution. It's frustrating to hell and back because none of the fixes I've seen anywhere seem to even be related to my problem. Here is my code, I pray someone may help me...

#include<stdio.h>
#include<stdlib.h>

int count = 0;
int state = 1;
int copy = getchar();

int main() {
     if (state != 0){
    
        while (copy != EOF)
           count++;
    
        state = 0;
    } else(){
         printf("The function has finished, your name has %d characters", count);
         count = 0;
         state = 1;
     }
 }

Ignore the placement of brackets in terms of depth, I must be a damn fool because no matter what I did (```, or ''', or ctrl k, etc) I couldn't get the freaking code to indent more than one line, so I literally went through and added 5 spaces to each. I hope I did it decently, I hope someone can explain to me what it is I'm not seeing...

  • 5
    Hint: `else` does not have `()`. If your C reference books don't cover this, time to get a [better one](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – tadman Mar 21 '21 at 16:43
  • If you're having trouble getting the code to indent that's peculiar, but could be the result of using tabs and having them collapse when pasting. – tadman Mar 21 '21 at 16:44
  • Side note: nothing changes the value of `copy` in your `while()` loop. – Weather Vane Mar 21 '21 at 16:44
  • `int copy = getchar()` runs that function once and once only, assigning the value to `copy`. It doesn't set it up as an alias to the call. Testing `copy != EOF` will never result in anything but a fail until `copy` is altered. – tadman Mar 21 '21 at 16:45
  • 1
    `int copy = getchar();` is illegal outside of a function. – Weather Vane Mar 21 '21 at 16:46
  • Yeah...your syntax is incorrect and your code structure makes no sense:(( – Martin James Mar 21 '21 at 16:56

1 Answers1

3

There's a lot that's gone sideways in this code, but it's easily fixed:

#include <stdio.h>
#include <stdlib.h>

// Avoid using global variables

int main() {
  int count = 0;

  // Always use { ... } around your blocks to avoid ambiguity, even for
  // simple one-liners like this.
  while (getchar() != EOF) {
    count++;
  }

  // Don't forget \n to add a newline
  printf("The function has finished, your name has %d characters\n", count);

  // Return 0 from main() to be polite
  return 0;
}

That if (state != 0) will only ever fire the first branch, there's never a chance to visit that line again, so your program just finishes without printing. I've removed that here. Remember if is a test that happens in an instant, as the program works through that section, not something that's a persistent test that will trigger if/when the variable changes.

One thing to keep in mind is that in C when you assign to a variable that's an operation that copies a value from the right-hand-side (RHS) to the left-hand-side (LHS). In other words:

int x = 1;
int y = x;

x = x + 1;
printf("x=%d, y=%d\n", x, y);

This prints x=2, y=1. Saying y = x is not like it is in math where you're stating some kind of equivalence. This is purely an assignment. It's an instruction (machine instruction, quite literally) to copy x to y, where x and y are separate "objects"1.


1 C term roughly describing values which can be, but are not always stored in memory. Sometimes they're kept in registers instead. Technical version: "An object is a region of data storage in the execution environment, the contents of which can represent values".

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Amazing! Thank you for the rapid and detailed reply! – Drew Mortenson Mar 21 '21 at 16:57
  • It's worth stepping through your code in a debugger to see how the variables change and where the execution flows. Once you get a sense of how C works internally it'll be a lot easier to be effective with it. – tadman Mar 21 '21 at 16:59