Big Confusion on an easy Program

This is a discussion on Big Confusion on an easy Program within the C Programming forums, part of the General Programming Boards category; I am trying with all my power to make this little program work! I have to make a pogram that ...

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Big Confusion on an easy Program

    I am trying with all my power to make this little program work! I have to make a pogram that copies input to output and replaces each string of a blank or more by a single blank!

    It is exercise 1-9 if you have The C Programming Language book, second edition!

    This is how far I got of it, but I know it is wrong since the compilers prints 102 infinitely! I will post my wrong noob code hoping that someone capable will give me a hint and tell me what am I doing wrong!Here is the code:
    Code:
    #include <stdio.h>
    
    main()
    {
    int blank;
    int x;
    blank = 0;
    x = getchar();
    
    while (x != EOF)
    {
    if (x == '  ');
        printf("%d\n", x);
    }
    
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    x == ' '
    2 spaces instead of 1, you also don't change X on every loop (I think your supposed to...)

    Code:
    while ((x = getchar()) != EOF)
    {
        if (x == ' ')
            printf("&#37;d\n", x);
    }
    Also try and work on your indenting
    Last edited by zacs7; 06-15-2007 at 08:29 AM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by cookie View Post
    I have to make a pogram
    http://en.wikipedia.org/wiki/Pogram

    Yikes!

    Quote Originally Posted by cookie View Post
    This is how far I got of it, but I know it is wrong since the compilers prints 102 infinitely!
    Look at what is inside your loop. Of course it's just going to kee printing the same thing.

    Quote Originally Posted by cookie View Post
    I will post my wrong noob code hoping that someone capable will give me a hint and tell me what am I doing wrong!
    What you need to do is have a loop where the condition is to keep reading until EOF. Inside the loop, you need to a check if the character read is a blank char... Then if so, replace it and enter another loop that just eats the whitespace chars (by just reading and doing nothing until the next char is not a blank one).

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    If you are using C rather than C++, main() should be int main(). Also, a character uses the "char" variable type, not an "int". I don't have the book. EOF means "end of file" so unless reading or writing files, there's no need for this. Also, for single quotes, you should only have one character inside, not two. For multiple, you'd use double quotes and have one extra that's big enough to store the entire string plus the null character. Also, there shouldn't be a semicolon at the end of an if statement. I'm not sure if you've learned any of this or not. Also, better indentation could make the code easier to read, especially if it gets into the hundreds or thousands of lines with many nested things (like loops and if statements).

    I see quite a few syntax errors and things that would show warnings in your compiler. What compiler do you use? You may want to turn up the warning level.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by ulillillia View Post
    Also, a character uses the "char" variable type, not an "int".
    getchar() returns an int. This is necessary due to EOF. Your data will be messed up in certain circumstances if you don't use an int.

  6. #6
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I'm not familiar with getchar since I don't use it. I just saw the content in the quotes so I thought it should be a string, thus the "char" is used. Oh well.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > if (x == ' ');
    What does that ; do ?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    A semicolon ( ; ) is a punctuation mark used to separate words opposed in meaning and to mark off interdependent statements. It binds two sentences more closely than they would be if separated by a full stop/period. It often replaces a conjunction such as and or but. Writers might consider this appropriate where they are trying to indicate a close relationship between two sentences, or a 'run-on' in meaning from one to the next; they do not want the connection to be broken by the abrupt use of a full stop. It is used as a stronger division than a comma, or a "super comma", to make meaning clear in a sentence where commas are being used for other purposes. A common example of this use is to separate the items of a list when some of the items themselves contain commas.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by ulillillia
    If you are using C rather than C++, main() should be int main(). Also, a character uses the "char" variable type, not an "int". I don't have the book. EOF means "end of file" so unless reading or writing files, there's no need for this.
    You can write EOF in the terminal (for example if you wanted to end the program...) And you should know that EOF doesn't fit into a char , getchar() is standard so you should also know that

  10. #10
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Thanks Everybody

    The code, my code, neither the one posted by zacs7 worked! All it would do, it would print 32 for each space I had in my sentence! But what I did, is made it print a blank space instead of x using this

    printf(" \n");

    instead of:

    printf("%d\n", x); that woukd print 32 instead of a blank!


    Thanks alot everybody! here is the final code


    Code:
    #include <stdio.h>
    
    int main()
    {
    	int x;
    	while ((x = getchar()) != EOF)
    	{
        	if (x == ' ')
                printf(" \n");
    	}
    }
    P.S. I learned to indent properly!


    Thanks Everybody!

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    P.S. I learned to indent properly!
    Nearly :P you if statement still is not indented inside the loop, but then I'm just nit picking

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    :\ you never explained your problem... I assumed you wanted to print the ascii position of a space.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  3. Help me ! easy c program
    By DINORS in forum C Programming
    Replies: 21
    Last Post: 06-27-2008, 11:59 PM
  4. Replies: 2
    Last Post: 05-10-2002, 05:16 PM
  5. big trouble in little program
    By jonesy in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 03:04 PM

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