By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

Help needed Please!

P: n/a
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.

Mar 2 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On 1 Mar 2006 21:32:54 -0800, si**********@hotmail.com wrote in
comp.lang.c:
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.


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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 2 '06 #2

P: n/a
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..

Mar 2 '06 #3

P: n/a

si**********@hotmail.com wrote:
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]);
What doess cc mean ? You probably meant tx.
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's solution seems to be correct.
After line number 74:

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

Mar 2 '06 #4

P: n/a
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..

Mar 2 '06 #5

P: n/a

si**********@hotmail.com wrote:
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]);
What exactly is the correct line of code at line number 70 ?
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.


Mar 2 '06 #6

P: n/a
si**********@hotmail.com wrote:

<snip>
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 }


<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

Mar 2 '06 #7

P: n/a
si**********@hotmail.com wrote:
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


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>
Mar 2 '06 #8

P: n/a
"Nick Keighley" <ni******************@hotmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
si**********@hotmail.com wrote:

<snip>
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 }
<snip>

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


Thank you, now i can follow the code. I have some comments for the OP.
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 */
That is not true for C99.
int j = 0;
int k = 0;

while ((tx[i] = getchar()) != '#')
EOF should be considered here.
{
i++;
}
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';
while(tx[j] != '\0')
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.
{
k++;
putchar (tx[j] + cc[k]);
j++;
if (k > 2)
k = 0;
}

k++;
}

Mar 3 '06 #9

P: n/a
Pedro Graca wrote:
si**********@hotmail.com wrote:
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


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


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.com, 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/googlegroupsreply/>
Mar 3 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.