code output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amanda22
    New Member
    • Nov 2007
    • 8

    code output

    I know this is probably something silly, but I am missing something. I put the following into my compiler and I am not getting it. I know I need something to make this work, because everyone else is getting it. My project is to enter the following into the compiler and tell what the output would be (like 'abc').

    char Ch='/';
    while (Ch !='d')
    {
    putchar(Ch);
    Ch = getchar();
    }

    Please help!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What I see is that Ch is a /

    Then you drop to the loop where a / is not a d so you drop into the loop an display: /

    Then you get a char, say a
    and display: a

    Then you get a char, say b
    and display b.

    Then you get a char, say c
    and display c.

    Then you get a char, say d
    and exit the loop.

    So you output is /aabbccd

    Your letters are echoed when you enter them and then they are displayed. Except for the d which is newver displayed.

    Now, then what is your question?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by amanda22
      I know this is probably something silly, but I am missing something. I put the following into my compiler and I am not getting it. I know I need something to make this work, because everyone else is getting it. My project is to enter the following into the compiler and tell what the output would be (like 'abc').

      char Ch='/';
      while (Ch !='d')
      {
      putchar(Ch);
      Ch = getchar();
      }

      Please help!
      If you fed that to your compiler verbatim it is never going to work. You have to put
      those statements in a function, most likely int main() { /* your code */ return 0; }

      kind regards,

      Jos

      Comment

      • amanda22
        New Member
        • Nov 2007
        • 8

        #4
        Originally posted by JosAH
        If you fed that to your compiler verbatim it is never going to work. You have to put
        those statements in a function, most likely int main() { /* your code */ return 0; }

        kind regards,

        Jos
        So, I know this still might be silly, but I am still confusing how this is put into the compiler.

        I would have to enter it as:

        int main()
        {
        char Ch='/';
        while (Ch !='d')
        {
        putchar(Ch);
        Ch=getchar();
        }

        As you can see, I am missing something, and that is where I am stuck. My textbook does not really address this.

        Thanks for any guidance.

        Comment

        • joelf223
          New Member
          • Nov 2007
          • 6

          #5
          I believe you'll need

          return 0;

          on the end

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            And you need to #include <stdio.h> . Remember to include the header for whatever functions you want to use. putchar and getchar are not intrinsic to C.

            Comment

            • amanda22
              New Member
              • Nov 2007
              • 8

              #7
              Guys,

              I am desperate, I have tried to input this and the other programs I am working on that are similar and I don't know why it isn't working. On this problem, I am supposed to input abcdefghi and my only possible solutions are a. abc b. d c. /abc or d. /efghi

              2) Another one I can't get to work is what is supposed to happen when the following code is entered:

              char Ch;
              Ch='7';
              printf("%d\n", Ch);

              3) And still another I can't get to work, what would be the effect on entering this code:

              char Ch;
              while ((Ch=getchar()) !=';')
              putchar (Ch);

              4) What would be the effect of the following:

              char Ch;
              while ((Ch=getchar()! ='\n')
              putchar(' ');

              As I said, I am desperate to getting this done. If any of you are from Portland, I'll even take you out to eat.

              Comment

              • oler1s
                Recognized Expert Contributor
                • Aug 2007
                • 671

                #8
                You're looking for an answer, not to learn. Unfortunately, that's not how programmer and technical forums work. We're interested in helping people learn, and when someone comes by focused on just getting an answer to walk away with, we don't bother.

                Comment

                Working...