473,805 Members | 1,981 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Endless loop question

This has been puzzling me all this week. This is actually a homework
assignment
from last semesmter. But the teacher wouldn't tell us why certain
things didn't work, but it didn't just work. My thing was what
actually turn this while loop into an endless loop instead of waiting
for user response it'll would skip right over it. Could someone with
the time explain this to me what would make it behave like this
int pts1, pts2, pts3, wk_exp, p_pay, total_pts, total_pay;
char degree, again;

int main() {
char again = 'Y';

while (again != 'N')
{

printf("\nEnter degree type: (B for Bachelors), (M for Masters), (D
for Doctorate\n");
printf("\t\t\t: ");
scanf("%1c", &degree);
if (degree = 'B') {
pts1 = 3;
}
if (degree = 'M') {
pts1 = 5;
}
if (degree = 'D') {
pts1 =7;
}
printf("\nEnter number of years of work experience: ");
scanf("%2d",&wk _exp);
if (wk_exp <= 3) {
pts2 = 4;
}
if (wk_exp >= 4 && wk_exp <= 6) {
pts2 = 7;
}
if (wk_exp >= 7) {
pts2 = 10;
}

printf("\nEnter current Pay: ");
scanf("%5d",&p_ pay);
if (p_pay <= 15000) {
pts3 = 4;
}
if (p_pay >= 15001 && p_pay <= 22500) {
pts3 = 8;
}
if (p_pay > 22500) {
pts3 = 12;
}

total_pts = pts1 + pts2 + pts3;
if (total_pts <= 19) {
total_pay = 25000;
}
if (total_pts >= 20 && total_pts <= 28) {
total_pay = 30000;
}
else if (total_pts >= 29) {
total_pay = 35000;
}

printf("\nThe pay rate is: %d\n",total_pay );

printf("\nWant to do this again? Press N for NO: ");
/*scanf("%1c", &again); */
getchar();
}

return (0) ;
}
Nov 14 '05 #1
24 1696
ph****@yahoo.co m (Tweaxor) wrote in
news:1c******** *************** ***@posting.goo gle.com:
This has been puzzling me all this week. This is actually a homework
assignment
from last semesmter. But the teacher wouldn't tell us why certain
things didn't work, but it didn't just work. My thing was what
actually turn this while loop into an endless loop instead of waiting
for user response it'll would skip right over it. Could someone with
the time explain this to me what would make it behave like this

None of these seem to need to be defined outside of main().
int pts1, pts2, pts3, wk_exp, p_pay, total_pts, total_pay;
char degree, again;

int main() { Ick ^^^^^^^^

int main(void)
{
char again = 'Y';

while (again != 'N')
{
/*scanf("%1c", &again); */
getchar();
}

return (0) ;

}


Since again is initially == 'Y', explain how again will ever be anything
but 'Y'. Then see why the while loop becomes endless.

--
- Mark ->
--
Nov 14 '05 #2
Mark A. Odell <od*******@hotm ail.com> wrote:
ph****@yahoo.co m (Tweaxor) wrote in
news:1c******** *************** ***@posting.goo gle.com:
int main() {

Ick ^^^^^^^^

int main(void)
{


No, why? You can drop `void' in function definition, can't you?

--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #3
On 29 Sep 2004 18:11:07 GMT, "S.Tobias" <sN*******@amu. edu.pl> wrote:
Mark A. Odell <od*******@hotm ail.com> wrote:
ph****@yahoo.co m (Tweaxor) wrote in
news:1c******** *************** ***@posting.goo gle.com:

> int main() {

Ick ^^^^^^^^

int main(void)
{


No, why? You can drop `void' in function definition, can't you?


They mean different things. '(void)' tells the compiler there are no
arguments. '()' tells the compiler that the arguments are unspecified.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #4
In article <07************ *************** *****@4ax.com>,
Alan Balmer <al******@spamc op.net> wrote:
On 29 Sep 2004 18:11:07 GMT, "S.Tobias" <sN*******@amu. edu.pl> wrote:
Mark A. Odell <od*******@hotm ail.com> wrote:
ph****@yahoo.co m (Tweaxor) wrote in
news:1c******** *************** ***@posting.goo gle.com:

> int main() {
Ick ^^^^^^^^

int main(void)
{


No, why? You can drop `void' in function definition, can't you?


They mean different things. '(void)' tells the compiler there are no
arguments. '()' tells the compiler that the arguments are unspecified.


In this case, though, the distinction doesn't make any difference; one
form means "Here's the definition of a function called main, returning
int and taking no arguments, without a prototype" and the other form
means "Here's the definition of a function called main, returning int
and taking no arguments, with a prototype".

Unless, of course, you're planning on calling main later on with a
nonempty argument list. But if you really want to lie to the compiler,
a prototype won't stop you either.
dave
((int (*)())main)(arg v,argc);

--
Dave Vandervies dj******@csclub .uwaterloo.ca
Shoot. Maybe I should port [GCC] to MMIX to get some [experience]
on my own.
--Ben Pfaff in comp.lang.c
Nov 14 '05 #5
Alan Balmer <al******@att.n et> wrote:
On 29 Sep 2004 18:11:07 GMT, "S.Tobias" <sN*******@amu. edu.pl> wrote:
No, why? You can drop `void' in function definition, can't you?

They mean different things. '(void)' tells the compiler there are no
arguments. '()' tells the compiler that the arguments are unspecified.


In declarations that are not definitions.

What can be unspecified in function definition?
int f() {}

Grep through n869.txt - there are two examples using "int main()"
(and one "int main(void)") declaration. They wouldn't miss an error
like that, would they?

--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #6
187
Alan Balmer wrote:
On 29 Sep 2004 18:11:07 GMT, "S.Tobias" <sN*******@amu. edu.pl> wrote:
Mark A. Odell <od*******@hotm ail.com> wrote:
ph****@yahoo.co m (Tweaxor) wrote in
news:1c******** *************** ***@posting.goo gle.com:

int main() {
Ick ^^^^^^^^

int main(void)
{


No, why? You can drop `void' in function definition, can't you?


They mean different things. '(void)' tells the compiler there are no
arguments. '()' tells the compiler that the arguments are unspecified.


but when it coems down to it, I've never seen any difference in actual
behavior between the two. When it comes to normal functions, there is no
difference.
Nov 14 '05 #7
On Wed, 29 Sep 2004 18:30:11 +0000 (UTC)
dj******@csclub .uwaterloo.ca (Dave Vandervies) wrote:
In article <07************ *************** *****@4ax.com>,
Alan Balmer <al******@spamc op.net> wrote:
On 29 Sep 2004 18:11:07 GMT, "S.Tobias" <sN*******@amu. edu.pl> wrote:
Mark A. Odell <od*******@hotm ail.com> wrote:
ph****@yahoo.co m (Tweaxor) wrote in
news:1c******** *************** ***@posting.goo gle.com:

> int main() {
Ick ^^^^^^^^

int main(void)
{

No, why? You can drop `void' in function definition, can't you?
They mean different things. '(void)' tells the compiler there are no
arguments. '()' tells the compiler that the arguments are
unspecified.


In this case, though, the distinction doesn't make any difference; one
form means "Here's the definition of a function called main, returning
int and taking no arguments, without a prototype" and the other form
means "Here's the definition of a function called main, returning int
and taking no arguments, with a prototype".


So why not give the compiler all the information you have? It's
generally a good habit and if you[1] break it for main you are likely to
break it for other functions.
Unless, of course, you're planning on calling main later on with a
nonempty argument list. But if you really want to lie to the
compiler, a prototype won't stop you either.


Actually, if there is a prototype in scope and you pass an incorrect
number of parameters the compiler is (I believe) *required* to produce a
diagnostic.

[1] The generic you, not the specific you.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #8
"S.Tobias" <sN*******@amu. edu.pl> wrote in
news:2s******** *****@uni-berlin.de:

> int main() {

Ick ^^^^^^^^

int main(void)
{


No, why? You can drop `void' in function definition, can't you?


I just said "Ick", it's not wrong, just lazy IMHO.

--
- Mark ->
--
Nov 14 '05 #9
On 29 Sep 2004 19:00:34 GMT, "S.Tobias" <sN*******@amu. edu.pl> wrote:
Alan Balmer <al******@att.n et> wrote:
On 29 Sep 2004 18:11:07 GMT, "S.Tobias" <sN*******@amu. edu.pl> wrote:

>No, why? You can drop `void' in function definition, can't you?

They mean different things. '(void)' tells the compiler there are no
arguments. '()' tells the compiler that the arguments are unspecified.


In declarations that are not definitions.

What can be unspecified in function definition?
int f() {}

Grep through n869.txt - there are two examples using "int main()"
(and one "int main(void)") declaration. They wouldn't miss an error
like that, would they?


The examples are not the standard. Your copy of the draft probably
includes section 5.1.2.2.1, paragraph 1, which gives the two allowable
forms, plus allows other *implementation-defined* forms. If the OP's
implementation documents that usage, it's OK. It probably doesn't.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2862
by: John F. | last post by:
Hi all, I'm searching fulltime for days now, and I don't find the solution, so I'm thinking this is more a bug :( If i use following code in form&subforms:
30
2208
by: Skybuck Flying | last post by:
I was just trying to figure out how some C code worked... I needed to make a loop to test all possible values for a 16 bit word. Surprise Surprise... C sucks at it... once again :D lol... C is such a bad language it amazes me everytime :D Just look at this shit: unsigned short int i;
13
1792
by: Bev in TX | last post by:
We are using Visual Studio .NET 2003. When using that compiler, the following example code goes into an endless loop in the "while" loop when the /Og optimization option is used: #include <stdlib.h> int resize(int *incsize, int min_size) { while(*incsize <= min_size) { *incsize = (int)(*incsize * 1.25); } if ( min_size > 60 ) return 0;
73
4639
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an endless loop in a line with: if a==b: print 'OK' I mean, it would be of much help to me on my way to understanding Python to know how such prefix code leading to an endless loop can look like and if it is eventually not possible to write such...
1
1849
by: cory6903 | last post by:
i am having a problem with an endless loop **int x; **a: **cout << "enter a #" << endl; **cin >> x **switch(x) **{ ** case 1:
6
500
by: uche | last post by:
This function that I have implemented gives me an infinite loop. I am trying to produce a hexdum program, however, this function is not functioning correctly.....Please help. void output(unsigned char ret_buffer, int curr_buffer_size, bool& endoffile) { int index2=0; int addr=0; unsigned char outBuff;
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10105
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.