Thread: Program to convert spaces to newlines doesn't compile

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    5

    Angry Program to convert spaces to newlines doesn't compile

    This is my whole file that I'm trying to compile:


    Code:
    #include <stdio.h>
    
    
    static int MAXIM = 100;
    
    
    void main() {
      char sentence[MAXIM];
      printf("Your sentence?: ");
      scanf("%s", sentence);
    
    
      printf("\n");
      
      for( int i = 0  ; i == MAXIM ; i++ ) {
        char ch = sentence[i];
        if (ch = " ")
          printf("\n");
        else
          printf("%c", ch);
      }
    }
    Running that through GCC will

    Code:
    stuff.c In function `main':
    space.c:14:12: warning: assignment to `char' from `char *' makes integer from pointer without a cast [-Wint-conversion]
       14 |     if (ch = " ")
          |            ^
    What'd I do wrong?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,673
    Two things in this line:

    Code:
    if (ch = " ")
    First, a space within quotes is treated as a C-string (char *). That's the meaning behind the error. You need to use single quotes for a character.
    Second, you are not comparing in that statement, you are assigning (as the error says). = for assignment, == for comparison.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,063
    Don't forget the rest of the errors.
    Code:
    $ gcc -Wall main.c
    main.c:7:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
     void main() {
          ^
    main.c: In function ‘main’:
    main.c:17:12: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
         if (ch = " ")
                ^
    main.c:17:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         if (ch = " ")
         ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2020
    Posts
    5
    Quote Originally Posted by rags_to_riches View Post
    Two things in this line:

    Code:
    if (ch = " ")
    First, a space within quotes is treated as a C-string (char *). That's the meaning behind the error. You need to use single quotes for a character.
    Second, you are not comparing in that statement, you are assigning (as the error says). = for assignment, == for comparison.
    Turns out it wasn't just any old error; it was a stupid error.

    The program now compiles, but doesn't print the sentence. It doesn't even print the appropriate newlines.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,778
    That's because now that you've fixed the syntax errors, you still have a logic error to fix. Carefully step through your loop with some test input to see what happens (or does not happen).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    92
    Your: for( int i = 0 ; i == MAXIM ; i++ )
    Better could be:
    for( int i = 0 ; i < MAXIM ; i++ )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-14-2017, 10:18 AM
  2. not sure why this doesn't compile
    By Dave Couture in forum C Programming
    Replies: 5
    Last Post: 08-18-2012, 05:08 AM
  3. Doesn't Compile
    By Justin C in forum C++ Programming
    Replies: 12
    Last Post: 04-29-2005, 06:40 PM
  4. Replies: 3
    Last Post: 03-07-2003, 09:06 PM

Tags for this Thread