473,326 Members | 2,337 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,326 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 1644
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: 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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.