Help needed Please!

S

silverchrono

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=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.
 
J

Jack Klein

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=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;
}
 
S

silverchrono

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..
 
J

Jaspreet

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=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;
}
 
S

silverchrono

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=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..
 
J

Jaspreet

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=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.
 
N

Nick Keighley

(e-mail address removed) wrote:

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=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 = 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]"
 
P

Pedro Graca

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++;
 
C

CBFalconer

Pedro said:
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/>
 
S

stathis gotsis

Nick Keighley said:
(e-mail address removed) wrote:

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=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 = getchar()) != '#')


EOF should be considered here.

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='\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++;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top