Thread: Help with this small snippet of code?

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    3

    Help with this small snippet of code?

    Hello,

    I am experimenting with structure and trying to assign
    address of a struct type and print a members value,
    but I get many errors. Please see code below;

    Code:
    #include <iostream>
    using namespace std;
    
    struct Num {
     unsigned int total;
    };
    
    
    
    
    void Set(struct Num *nn) {
     nn->total = 99;
    }
    
    
    int main () {
     struct Num spike;
     struct Num *NewPtr;
    
    
     NewPtr=&spike;
     NewPtr->total = 5;
     Set(NewPtr);
     
     printf("\ns.total =    
     %d\n", NewPtr->total);
    }
    here are errors:

    compiler.cpp:60:9: warning: missing terminating " character
    60 | printf("\ns.total =
    | ^
    compiler.cpp:60:9: error: missing terminating " character
    60 | printf("\ns.total =
    | ^~~~~~~~~~~~
    compiler.cpp:61:4: error: stray '\' in program
    61 | %d\n", NewPtr->total);
    | ^
    compiler.cpp:61:6: warning: missing terminating " character
    61 | %d\n", NewPtr->total);
    | ^
    compiler.cpp:61:6: error: missing terminating " character
    61 | %d\n", NewPtr->total);
    | ^~~~~~~~~~~~~~~~~~
    compiler.cpp: In function 'int main()':
    compiler.cpp:61:2: error: expected primary-expression before '%' token
    61 | %d\n", NewPtr->total);
    | ^
    compiler.cpp:61:3: error: 'd' was not declared in this scope
    61 | %d\n", NewPtr->total);
    | ^

    thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,618
    You need to take the newline out of the middle of your string.
    Code:
     printf("\ns.total = %d\n", NewPtr->total);
    Other things to note
    1. You're writing C++ code for a C++ compiler.
    2. printf is the C way to print things, you should be using cout instead.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2021
    Posts
    3
    Hello Salem,

    impossible !

    I am using a C/C++ compiler app which supposedly
    does both!


    and considered the new line in the
    Printf statement consider this
    code which works fine:

    Code:
    struct Num {
     unsigned int total;
    }s;
    
    
    int test(struct Num nn) {
     nn.total = 9;
     return 0;
    }
    
    
    int main () {
     s.total = 5;
     test(s);
     printf("\ns.total = %d\n", s.total);
    }
    the question is why would the latter work
    and the original example fail in the
    printf statement?

    Thanks for replying !

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,309
    Quote Originally Posted by Trying-C
    impossible !

    I am using a C/C++ compiler app which supposedly
    does both!
    ... but not at the same time. You have to pick a language: do you want to write this program in C or in C++?

    Also, read Stroustrup's answer to the FAQ: What do you think of C/C++?

    As for the newline: Salem was talking about a physical newline in your string literal. Your latter code doesn't have that problem.
    Last edited by laserlight; 03-30-2021 at 12:28 PM.
    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

  5. #5
    Registered User
    Join Date
    Mar 2021
    Posts
    3
    Hi, I want to use C.

    but there is no option to choose C or C++???

    and I dont understand what you are talking
    about when you say the string in the
    printf statement .... I took both \n out of the
    printf statement and still doesnt work?!!!

    oooofffff ! 😟
    Last edited by Trying-C; 03-30-2021 at 04:19 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,309
    Quote Originally Posted by Trying-C
    Hi, I want to use C.

    but there is no option to choose C or C++???
    You just haven't found it yet. What IDE and/or compiler are you using?

    Quote Originally Posted by Trying-C
    and I don’t understand what you are talking
    about when you say the string in the
    printf statement .... I took both “\n” out of the
    printf statement and still doesn’t work?!!!
    Try to compile this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("hello
    world\n");
        return 0;
    }
    You should get a compile error. Now try to compile this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("hello\nworld\n");
        return 0;
    }
    The program should compile successfully. Then try to compile this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("hello\
    world\n");
        return 0;
    }
    The program should also compile successfully, but you will find that the output is all on one line.

    The idea is that a string literal, i.e., something like "hello\nworld\n" cannot just contain a physical line break (unless you escape that line break so that the two physical lines form a single logical line, which is what the last program demonstrated). This is the issue with your original code: you typed enter when you shouldn't have. It is as simple as that.

    In practice, if you do want to break a long string literal across multiple physical lines, you would take advantage of the fact that adjacent string literals ("adjacent" excluding whitespace) are concatenated during compilation, like this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("hello\n"
               "world\n");
        return 0;
    }
    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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,618
    > Hi, I want to use C.
    > but there is no option to choose C or C++???

    > compiler.cpp:
    Rename this to compiler.c

    Most compiler drivers recognise automatically that .c files are for the C compiler, and .cpp/.cxx are for the C++ compiler.
    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.

  8. #8
    Registered User
    Join Date
    Sep 2020
    Posts
    2
    Quote Originally Posted by Trying-C View Post
    and I don’t understand what you are talking
    about when you say the string in the
    printf statement .... I took both “\n” out of the
    printf statement and still doesn’t work?!!!

    What they are talking about is that I think you accidentally hit enter in your printf() line... they aren't meaning the new line character '\n' but rather how you probably pressed enter in the middle of your "hello world" string. Your string should be on a single line with control characters to format what you want within that string. The computer won't see your entered new line in the code the same as a '\n' character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this code snippet wrong
    By jeff loomis in forum C Programming
    Replies: 4
    Last Post: 01-15-2014, 09:06 PM
  2. what will be the output of this code snippet
    By devilofwar in forum C Programming
    Replies: 12
    Last Post: 12-13-2010, 05:41 PM
  3. Explanation of code snippet
    By Jelte in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2010, 06:59 AM
  4. C code snippet which I need help understanding
    By c_me in forum C Programming
    Replies: 3
    Last Post: 07-28-2006, 06:06 AM
  5. a great little code snippet...
    By skeptik in forum Windows Programming
    Replies: 1
    Last Post: 09-24-2002, 11:45 PM

Tags for this Thread