trying to change filter comment in c++ to c language under visual c++

This is a discussion on trying to change filter comment in c++ to c language under visual c++ within the C Programming forums, part of the General Programming Boards category; copy and paste the below source code for analysis i tried to change cin.get(c) and !cin.eof to c = getchar() ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    3

    trying to change filter comment in c++ to c language under visual c++

    copy and paste the below source code for analysis
    i tried to change cin.get(c) and !cin.eof to c = getchar() and ((c=getchar()) != EOF) but it didn't work, tell me why??? it can be compiled but the characters i typed in was skipped oddly
    if abcd is entered
    bd will be displayed

    i need to compile this source under the linux platform, so i need help to write a version of this program under c language instead of c++.........please help!!!

    #include <iostream.h> // include file c++
    #include <stdio.h> // c lang
    #include <stdlib.h> // c lang

    int main()
    {
    char c; // declaration of variable used to scan the stream of characters

    while (!(cin.eof())) // reads input until end of file is reached
    {
    cin.get(c);

    if (c != '/' && c != '"') // check for possible start of comment or quotation
    printf("%c", c);

    else if (c == '"') { // Finds start of quotation
    printf("%c", c);
    cin.get(c);

    while (c != '"') {// Ignores characters inside quotation
    printf("%c", c);
    cin.get(c);

    if (c == '\\') {// Check for '\"' characters inside string and ignores them
    printf("%c", c);
    cin.get(c);

    if (c == '"')
    printf("%c", c);

    else
    printf("%c", c);

    cin.get(c);
    }

    if (cin.eof()) {// Displays error if program gets to the end of file before finding the closing quote
    cerr <<"\n ------- ERROR!!! End of file reached before end of string!!! -------\n\n\n";
    return EXIT_FAILURE;
    }
    }

    if (c == '"') // Finds end of quotation
    printf("%c", c);
    }

    else if(c == '/') {// Checks for // kind of comment
    cin.get(c);

    if (c == '/') {
    while (c != '\n') {// Delete all characters until end of line is reached
    cin.get(c);

    if (c == '\n')
    printf("%c", c);

    else
    printf("");
    }
    }

    else if(c == '*') { // Checks for /* kind of comment

    cin.get(c);

    while (c != '*') { // Deletes all characters until '*' is found
    printf("");
    cin.get(c);

    if (cin.eof()) { // Displays error if program gets to the end of file before finding the end of the comment
    cerr <<"\n ------- ERROR!!! End of file reached before end of comment!!! ------- \n\n\n";
    return EXIT_FAILURE;
    }

    }

    if (c == '*') { // Finds possible end of comment

    cin.get(c);

    while (c != '/') {
    printf("");
    cin.get(c);
    }

    if (c == '/') // Confirms end of comment
    printf("");

    }

    }

    }

    else
    printf("%c", c);

    }
    return EXIT_SUCCESS;
    }

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    let's say you have two statements in order:
    Code:
    cin.get(c);   //get a character and puts it into c
    if (!cin.eof) ...  //if stream cin has reached eof...
    and you changed it to:
    Code:
    c = getchar();  //get a character and put it in c
    if ((c = getchar()) != EOF) ...//get another character and put it into c, then check if its EOF
    see the problem? you're getting two characters, but only counting the second one. i would remove the c = getchar(); statement altogether.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 09:45 AM
  4. DemoLinux, change language possible ?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-16-2002, 01:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21