473,241 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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.

Mar 2 '06 #1
9 1641
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
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

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

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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jim Hubbard | last post by:
I want to implement the same type of activeX restrictions in my browser application that the new SP2 for XP places in Internet Explorer. I have found 2 web pages dealing with this functionality...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
1
by: worzel | last post by:
Hi All, I am looking for a reg ex that will match email addresses withing <a href=mailto blah > links. Actually, I already crafted my own, but with a slight problem: <a...
2
by: trihanhcie | last post by:
I m currently working on a Unix server with a fedora 3 as an os My current version of mysql is 3.23.58. I'd like to upgrade the version to 5.0.18. After downloading from MYSQL.COM the package on...
14
namcintosh
by: namcintosh | last post by:
Hello, everyone. Well, let me cut to the chase and explain my problem. I am trying to devise a menu plan that uses the if/else if and the while loop. The program calculates the user's weight...
0
by: Christopher | last post by:
Urgent Help Needed: The EPVH-1.1 Visual Hull Library. Dear All, I am a student doing research in computer vision. The EPVH-1.1 Visual Hull Library will really help a lot in my research. I...
5
by: justbovo | last post by:
Hello, My name is Justin. I am working on a part of a Find Memory module for a program. Here's how it works.. I enter '13' at the main menu which branches out to my Find Memory module. I ask...
8
by: 08butoryr | last post by:
Hey guys I could really use your help with some very basic java programming. I know you programming fundis out there will find this child's play but I'm struggling with it a bit because I'm...
13
by: DDragon | last post by:
ok here is the problem, i have to forms which have values i wish to be added together i can add together the values in one form all right but im having problems with adding the values of the other...
4
by: princessfi | last post by:
Hi, I'm new to visual basic. I have a problem. i connected the database to the form and i added the code below to prompt if there is any blank textbox. When nothing is entered into the textbox and...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.