English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'll edit because it's in spanish (the text) but bascially:


int loop;

do
{
printf( "\nSi desea continuar, presione la tecla 1. Sino, presione la tecla 2\n" );
scanf( "%d", &loop);
} while ( loop = 5 );


it isnt' working. no matter what value I give to loop when it asks, it keeps repeating.

2007-04-13 17:58:21 · 12 answers · asked by John D 1 in Computers & Internet Programming & Design

12 answers

On your last line of the do...while, you "test" with "loop = 5". With a single equal sign, this becomes an assignment statement, setting loop to 5, and returning a value of 5 (true) for the while test. This has been a syntactic bug ... er, I mean a feature, since the first days of the C language. Use the == for tests.

Your compiler may have a setting that detects possible bugs like this. If so, use it. In fact, never ignore even the most trivial warning message from a compiler. I once ignored three such warnings, considering them innocuous. I later had to debug and fix all three because they caused problems.

If your compiler doesn't have such a setting, it may have come with a utility called lint. Use it instead.

By the way, the code as written still won't work: You're inputting 1 to continue, not 5.

2007-04-13 18:17:28 · answer #1 · answered by The Phlebob 7 · 0 0

wow, I havent seen that syntax in a long time o_o. I would suggest doing this ...

int loop=0;
while (loop<=5){
loop++;
printf( "\nSi desea continuar, presione la tecla 1. Sino, presione la tecla 2\n" );
scanf( "%d", &loop);
}

//This will do the corresponding action 6 times .. I believe thats the effect you want, if not you can chage it accordingly but the basics of the commands are right here

hope this helps!
-Bob

2007-04-13 18:51:51 · answer #2 · answered by Mr. Bob 3 · 1 0

I don't really know C but try using a while loop instead. While i is less than 25, increment by 5.

2016-04-01 01:00:42 · answer #3 · answered by Anonymous · 0 0

What does your debugger say?
Try
(loop <> 5);
as a trial.
I read: do, while loop = 5. meaning, while the loop = 5 keep doing it. Is the program waiting for 5 entries or a value of 5?

2007-04-13 18:09:39 · answer #4 · answered by Bert H 4 · 0 1

last statement you wrote is (loop=5)
means computer assignes the value of loop is 5 and take the condition that (loop != 0 ) .....
so this condition becomes always true....
so your loop goes to infinite....and so it is repeated....
and no matter for the value of the loop variable you scan...
and the statement you write in printf is displayed infinite times....

2007-04-13 18:14:35 · answer #5 · answered by Tejas J 1 · 0 0

Change the last line to while (loop == 5)

2007-04-13 18:05:00 · answer #6 · answered by Nisovin 5 · 0 0

loop <= 5
increment loop inside while

2007-04-13 18:45:43 · answer #7 · answered by Anonymous · 0 0

int loop;

do
{
printf( "\nIf it wishes to continue, press a key. Otherwise press key 2\n" );
scanf( "%d", &loop);
} while ( loop != 2 );

2007-04-13 18:46:14 · answer #8 · answered by iyiogrenci 6 · 0 0

Do you mean your loop doesn't stop executing?

2007-04-13 18:08:18 · answer #9 · answered by Jec 2 · 0 1

You should use == instead of = when you're testing for equality.

= is the assignment operator

2007-04-13 18:48:55 · answer #10 · answered by Graundan 1 · 0 0

fedest.com, questions and answers