Help needed Please!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • silverchrono@hotmail.com

    #1

    Help needed Please!

    this is my first semester in C
    and im trying to figure out how to reset a counter.

    heres why im trying to do.

    void text()

    59 printf("You can end entering the text by using '#'\n");
    60 int i=0;
    61 int j=0;
    62 int k=0;
    63 while ((tx[i]=getchar())!='# ')
    64 {
    65 i++;
    66 }
    67
    68 while(tx[j]!='\0')
    69 {
    70 putchar(tx[j]+cc[k]);
    74 k++; j++;
    //basically I want [j] to keep on counting until EOF a// no problem
    there
    //but i want k to count only from 0 to 2 then resets back to 0 again
    then 1 then 2, then resets to 0 again Until EOF...now i have a good
    reason why i want k to count from 0 to 2 only and resets to 0

    anyone have a clue on how to do this thx.

  • Jack Klein

    #2
    Re: Help needed Please!

    On 1 Mar 2006 21:32:54 -0800, silverchrono@ho tmail.com wrote in
    comp.lang.c:
    [color=blue]
    > this is my first semester in C
    > and im trying to figure out how to reset a counter.
    >
    > heres why im trying to do.
    >
    > void text()
    >
    > 59 printf("You can end entering the text by using '#'\n");
    > 60 int i=0;
    > 61 int j=0;
    > 62 int k=0;
    > 63 while ((tx[i]=getchar())!='# ')
    > 64 {
    > 65 i++;
    > 66 }
    > 67
    > 68 while(tx[j]!='\0')
    > 69 {
    > 70 putchar(tx[j]+cc[k]);
    > 74 k++; j++;
    > //basically I want [j] to keep on counting until EOF a// no problem
    > there
    > //but i want k to count only from 0 to 2 then resets back to 0 again
    > then 1 then 2, then resets to 0 again Until EOF...now i have a good
    > reason why i want k to count from 0 to 2 only and resets to 0
    >
    > anyone have a clue on how to do this thx.[/color]

    How about this, after you increment 'k':

    if (k > 2)
    {
    k = 0;
    }

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++

    Comment

    • silverchrono@hotmail.com

      #3
      Re: Help needed Please!

      this works it resets k to 0..however the loop ends there and
      permanently sets k to 0..

      it doesn't go from 0 to 2 again..

      Comment

      • Jaspreet

        #4
        Re: Help needed Please!


        silverchrono@ho tmail.com wrote:[color=blue]
        > this is my first semester in C
        > and im trying to figure out how to reset a counter.
        >
        > heres why im trying to do.
        >
        > void text()
        >
        > 59 printf("You can end entering the text by using '#'\n");
        > 60 int i=0;
        > 61 int j=0;
        > 62 int k=0;
        > 63 while ((tx[i]=getchar())!='# ')
        > 64 {
        > 65 i++;
        > 66 }
        > 67
        > 68 while(tx[j]!='\0')
        > 69 {
        > 70 putchar(tx[j]+cc[k]);[/color]

        What doess cc mean ? You probably meant tx.
        [color=blue]
        > 74 k++; j++;
        > //basically I want [j] to keep on counting until EOF a// no problem
        > there
        > //but i want k to count only from 0 to 2 then resets back to 0 again
        > then 1 then 2, then resets to 0 again Until EOF...now i have a good
        > reason why i want k to count from 0 to 2 only and resets to 0
        >
        > anyone have a clue on how to do this thx.[/color]

        Jack's solution seems to be correct.
        After line number 74:

        if ( k > 2 )
        {
        k = 0;
        }

        Comment

        • silverchrono@hotmail.com

          #5
          Re: Help needed Please!

          OH thx that actually work..

          all i did was

          void text()
          56 {
          57
          58 printf("Please Enter a Text for coding\n");
          59 printf("You can end entering the text by using '#'\n");
          60 int i=0;
          61 int j=0;
          62 int k=0;
          63 while ((tx[i]=getchar())!='# ')
          64 {
          65 i++;
          66 }
          67
          68 while(tx[j]!='\0')
          69 {
          70 k++;
          71 putchar(tx[j]+cc[k]);
          72 j++;
          73 if (k>2)
          74 k=0;
          75 }
          76 k++;
          77 }

          basicall add a k++ at the beggining and the end ..

          thx for the help..this actually finish my my assignment..thx again..

          Comment

          • Jaspreet

            #6
            Re: Help needed Please!


            silverchrono@ho tmail.com wrote:[color=blue]
            > this is my first semester in C
            > and im trying to figure out how to reset a counter.
            >
            > heres why im trying to do.
            >
            > void text()
            >
            > 59 printf("You can end entering the text by using '#'\n");
            > 60 int i=0;
            > 61 int j=0;
            > 62 int k=0;
            > 63 while ((tx[i]=getchar())!='# ')
            > 64 {
            > 65 i++;
            > 66 }
            > 67
            > 68 while(tx[j]!='\0')
            > 69 {
            > 70 putchar(tx[j]+cc[k]);[/color]

            What exactly is the correct line of code at line number 70 ?
            [color=blue]
            > 74 k++; j++;
            > //basically I want [j] to keep on counting until EOF a// no problem
            > there
            > //but i want k to count only from 0 to 2 then resets back to 0 again
            > then 1 then 2, then resets to 0 again Until EOF...now i have a good
            > reason why i want k to count from 0 to 2 only and resets to 0
            >
            > anyone have a clue on how to do this thx.[/color]

            Comment

            • Nick Keighley

              #7
              Re: Help needed Please!

              silverchrono@ho tmail.com wrote:

              <snip>
              [color=blue]
              > void text()
              > 56 {
              > 57
              > 58 printf("Please Enter a Text for coding\n");
              > 59 printf("You can end entering the text by using '#'\n");
              > 60 int i=0;
              > 61 int j=0;
              > 62 int k=0;
              > 63 while ((tx[i]=getchar())!='# ')
              > 64 {
              > 65 i++;
              > 66 }
              > 67
              > 68 while(tx[j]!='\0')
              > 69 {
              > 70 k++;
              > 71 putchar(tx[j]+cc[k]);
              > 72 j++;
              > 73 if (k>2)
              > 74 k=0;
              > 75 }
              > 76 k++;
              > 77 }[/color]

              <snip>

              code is easier to read if its well laid out. Judicious use of blank
              lines, whitespace and proper indentation all help

              void text (void) /*** if no arguments void is good style */
              {
              printf("Please Enter a Text for coding\n");
              printf("You can end entering the text by using '#'\n");
              int i = 0; /*** in C you are not allowed declarations after
              statements */
              int j = 0;
              int k = 0;

              while ((tx[i] = getchar()) != '#')
              {
              i++;
              }

              while(tx[j] != '\0')
              {
              k++;
              putchar (tx[j] + cc[k]);
              j++;
              if (k > 2)
              k = 0;
              }

              k++;
              }

              in fact I would normally use more whitespace and write "tx[j]" as "tx
              [j]"



              --
              Nick Keighley

              Comment

              • Pedro Graca

                #8
                Re: Help needed Please!

                silverchrono@ho tmail.com wrote:[color=blue]
                > but i want k to count only from 0 to 2 then resets back to 0 again
                > then 1 then 2, then resets to 0 again Until EOF...now i have a good
                > reason why i want k to count from 0 to 2 only and resets to 0[/color]

                Another approach. Maybe a bit more cryptic but I like (don't know why,
                though) to save variables ...

                /* 62 */ /* int k=0; */

                /* 68 */ while (tx[j] != '\0')
                /* 69 */ {
                /* 70 */ putchar(tx[j] + cc[j%3]);
                /* 74 */ /* k++; */ j++;

                --
                If you're posting through Google read <http://cfaj.freeshell. org/google>

                Comment

                • stathis gotsis

                  #9
                  Re: Help needed Please!

                  "Nick Keighley" <nick_keighley_ nospam@hotmail. com> wrote in message
                  news:1141287530 .976285.140600@ e56g2000cwe.goo glegroups.com.. .[color=blue]
                  > silverchrono@ho tmail.com wrote:
                  >
                  > <snip>
                  >[color=green]
                  > > void text()
                  > > 56 {
                  > > 57
                  > > 58 printf("Please Enter a Text for coding\n");
                  > > 59 printf("You can end entering the text by using '#'\n");
                  > > 60 int i=0;
                  > > 61 int j=0;
                  > > 62 int k=0;
                  > > 63 while ((tx[i]=getchar())!='# ')
                  > > 64 {
                  > > 65 i++;
                  > > 66 }
                  > > 67
                  > > 68 while(tx[j]!='\0')
                  > > 69 {
                  > > 70 k++;
                  > > 71 putchar(tx[j]+cc[k]);
                  > > 72 j++;
                  > > 73 if (k>2)
                  > > 74 k=0;
                  > > 75 }
                  > > 76 k++;
                  > > 77 }[/color]
                  >
                  > <snip>
                  >
                  > code is easier to read if its well laid out. Judicious use of blank
                  > lines, whitespace and proper indentation all help[/color]

                  Thank you, now i can follow the code. I have some comments for the OP.
                  [color=blue]
                  > void text (void) /*** if no arguments void is good style */
                  > {
                  > printf("Please Enter a Text for coding\n");
                  > printf("You can end entering the text by using '#'\n");
                  > int i = 0; /*** in C you are not allowed declarations after
                  > statements */[/color]

                  That is not true for C99.
                  [color=blue]
                  > int j = 0;
                  > int k = 0;
                  >
                  > while ((tx[i] = getchar()) != '#')[/color]

                  EOF should be considered here.
                  [color=blue]
                  > {
                  > i++;
                  > }[/color]

                  There is a danger that tx[] may be overrun as there is no check for the
                  incrementing value of i. The string is not properly null-terminated, at
                  least not in the part of the code we are presented. One should use something
                  like:

                  tx[i]='\0';
                  [color=blue]
                  > while(tx[j] != '\0')[/color]

                  If the string was not null-terminated that worked out of pure lack. '#',
                  which is used to signal end of user input, is processed, even '\n', i do not
                  know if that is the OP's intention.
                  [color=blue]
                  > {
                  > k++;
                  > putchar (tx[j] + cc[k]);
                  > j++;
                  > if (k > 2)
                  > k = 0;
                  > }
                  >
                  > k++;
                  > }[/color]


                  Comment

                  • CBFalconer

                    #10
                    Re: Help needed Please!

                    Pedro Graca wrote:[color=blue]
                    > silverchrono@ho tmail.com wrote:
                    >[color=green]
                    >> but i want k to count only from 0 to 2 then resets back to 0 again
                    >> then 1 then 2, then resets to 0 again Until EOF...now i have a good
                    >> reason why i want k to count from 0 to 2 only and resets to 0[/color]
                    >
                    > Another approach. Maybe a bit more cryptic but I like (don't know
                    > why, though) to save variables ...[/color]

                    Piggybacking. If you want the range of k to be 0 through 2,
                    increment it with the statement:

                    k = (k + 1) % 3;

                    --
                    "If you want to post a followup via groups.google.c om, don't use
                    the broken "Reply" link at the bottom of the article. Click on
                    "show options" at the top of the article, then click on the
                    "Reply" at the bottom of the article headers." - Keith Thompson
                    More details at: <http://cfaj.freeshell. org/google/>
                    Also see <http://www.safalra.com/special/googlegroupsrep ly/>


                    Comment

                    Working...