Thread: Newbie Question

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    4

    Newbie Question

    Hello all. I am using VS 2017 Community and trying to learn a little C. I am reading a book, and upon trying some of the samples, I am getting errors. Can anyone point me in the right direction to get past this?

    Here is the code:

    Code:
    #include <iostream>
    #include <stdio.h>
    
    
    #define IN 1 /* inside a word */
    #define OUT 0 /* outside a word */
    /* count lines, words, and characters in input */
    int main()
    {
        int c, nl, nw, nc, state;
        state = OUT;
        nl = nw = nc = 0;
        while ((c = getchar()) != EOF) {
            ++nc;
            if (c = ’\n’)
                ++nl;
            if (c = ’ ’ || c = ’\n’ || c = ’\t’)
                state = OUT;
            else if (state == OUT) {
                state = IN;
                ++nw;
            }
        }
        printf("%d %d %d\n", nl, nw, nc);
    Here are the errors...
    Code:
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(20): error C3873: '0x2019': this character is not allowed as a first character of an identifier
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(20): error C2017: illegal escape sequence
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(20): error C3872: '0x2019': this character is not allowed in an identifier
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(20): error C2065: '’': undeclared identifier
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(20): error C2146: syntax error: missing ')' before identifier 'n’'
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(20): error C2059: syntax error: ')'
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(20): error C2065: 'n’': undeclared identifier
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(21): error C2146: syntax error: missing ';' before identifier 'nl'
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(22): error C3873: '0x2019': this character is not allowed as a first character of an identifier
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(22): error C2017: illegal escape sequence
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(22): error C3872: '0x2019': this character is not allowed in an identifier
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(22): error C2065: '’': undeclared identifier
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(22): error C2146: syntax error: missing ')' before identifier '’'
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(22): error C2146: syntax error: missing ';' before identifier 'n’'
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(22): error C2065: 'n’': undeclared identifier
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(22): error C2146: syntax error: missing ';' before identifier 't’'
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(22): error C2059: syntax error: ')'
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(22): error C2065: 't’': undeclared identifier
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(23): error C2146: syntax error: missing ';' before identifier 'state'
    1>c:\users\mjdaley\source\repos\project9\project9\source.cpp(24): error C2181: illegal else without matching if
    1>Done building project "Project9.vcxproj" -- FAILED.
    Thank you in advance,

    Mike

  2. #2
    Guest
    Guest
    You're probably mixing up the characters and '. The latter is what you want to use in your code.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by parallon View Post
    Code:
    #include <iostream>
    #include <stdio.h>
    
    
    #define IN 1 /* inside a word */
    #define OUT 0 /* outside a word */
    /* count lines, words, and characters in input */
    int main()
    {
        int c, nl, nw, nc, state;
        state = OUT;
        nl = nw = nc = 0;
        while ((c = getchar()) != EOF) {
            ++nc;
            if (c = ’\n’)
                ++nl;
            if (c = ’ ’ || c = ’\n’ || c = ’\t’)
                state = OUT;
            else if (state == OUT) {
                state = IN;
                ++nw;
            }
        }
        printf("%d %d %d\n", nl, nw, nc);
    And your comparisons are in real assignments.
    '=' != '=='
    Other have classes, we are class

  4. #4
    Registered User
    Join Date
    Nov 2018
    Posts
    4
    Quote Originally Posted by Guest View Post
    You're probably mixing up the characters and '. The latter is what you want to use in your code.
    Well, I guess that's what Copy/Paste get you... lol


    Thank you both so much. So it finally compiled, but the program really doesn't do anything. I can type a sentence and hit Enter, but the cursor just jumps to the next line. I was expecting some info...

    Thanks again,

    Mike

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    You are also placing the C code in a .cpp file which triggers g++ NOT a .c file to invoke gcc. Also, remove the iostream include line!

  6. #6
    Registered User
    Join Date
    Nov 2018
    Posts
    4
    Thank you for that bit of info. I was wondering how to create C from VS 2017. Found a YouTube video that pointed me in the right direction. Although, still no results running the executable. I enter info and hit enter, and all that happens is the cursor jumps to the next line.

    Thanks again,

    Mike

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    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

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by parallon View Post
    Thank you for that bit of info. I was wondering how to create C from VS 2017. Found a YouTube video that pointed me in the right direction. Although, still no results running the executable. I enter info and hit enter, and all that happens is the cursor jumps to the next line.

    Thanks again,

    Mike
    Can't help you with "VS 2017", as I only use Linux and gcc.

    Your code contains 'multi byte characters" that will not compile under gcc.

    If you enter a line and press enter it will continue to wait for more data to be entered as you are testing the getchar() against "EOF" NOT '\n, a newline!

    Line 17
    Code:
     if (c = ’ ’ || c = ’\n’ || c = ’\t’)
    Should read:
    Code:
     if (c == ’ ’ || c == ’\n’ || c == ’\t’)
    And on other lines where you are testing c to be equal to a character constant!

    Code:
    c = '\n';
    is an assignment!
    Code:
    c == '\n';
    is a test for equality!

    Common beginner mistake, but is caught with a good compiler such as a current version of gcc.

    Please turn on and turn up warning levels to the highest setting!

  9. #9
    Registered User
    Join Date
    Nov 2018
    Posts
    4
    @laserlight: Here is the current code...

    Code:
    #include <stdio.h>
    
    
    
    
    #define IN    1
    #define OUT    0
    int main()
    {
        int c, nl, nw, nc, state;
    
    
        state = OUT;
        nl = nw = nc = 0;
        while ((c = getchar()) != EOF) {
            ++nc;
            if (c == '\n')
                ++nl;
            if (c == ' ' || c == '\n' || c == '\t')
                state = OUT;
            else if (state == OUT) {
                state = IN;
                ++nw;
            }
        }
        printf("%d %d %d\n", nl, nw, nc);
    }
    @rstanley: Yeah, I caught that earlier too, thanks to WoodSTokk.

    Here is the output when running the code...

    Newbie Question-c_error-png

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You won't see the output of printf("%d %d %d\n", nl, nw, nc); until you make while ((c = getchar()) != EOF) evaluate to false.

    The way you do this is by pressing ctrl-z after you've pressed enter at the end of a line of input.

    Beware that if you're running the exe file from within an IDE, that your console window may disappear almost immediately. Some IDEs can be made to hold the console window open after the program has exited.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello, I am a newbie and so I have a newbie question;)
    By piratemonkey247 in forum C Programming
    Replies: 4
    Last Post: 12-20-2012, 10:59 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. C newbie question
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-06-2008, 03:40 AM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. newbie question about i/o!!!!!!!!help!!!!11
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2002, 09:48 PM

Tags for this Thread