Using a While Loop to Control Program Termination in C

Click For Summary

Discussion Overview

The discussion revolves around using a while loop in the C programming language to control program termination based on user input. Participants explore how to implement a loop that allows users to either repeat the program or exit it, addressing issues related to infinite loops and proper syntax.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant asks how to set up a while loop to ask the user if they want to terminate or repeat the program.
  • Another participant suggests that a while loop is just one component of the program and emphasizes the importance of outlining program behavior first.
  • A participant proposes a basic structure for the while loop, indicating a desire to execute the program while a variable equals 2.
  • Several participants discuss issues with infinite loops, with one noting that the program runs correctly the first time but fails to exit on subsequent iterations.
  • A suggestion is made to ensure the correct use of the address-of operator (&) with scanf to avoid incorrect memory assignments.
  • Another participant emphasizes the need for the equality operator (==) instead of the assignment operator (=) in the while condition to prevent infinite loops.
  • A participant mentions the importance of distinguishing between integer and character inputs when using scanf.
  • There is a request for help on a separate task involving outputting even numbers using a while loop.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the implementation of while loops, with some agreeing on the syntax and others highlighting potential pitfalls. There is no consensus on the specific solutions to the infinite loop issue, as participants offer different suggestions and corrections.

Contextual Notes

Participants note the importance of correct syntax and data types when using scanf, as well as the potential for infinite loops if the loop condition is not properly defined. There are unresolved issues related to the specific implementation details of the while loop.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those interested in control structures and user input handling.

Nothing000
Messages
403
Reaction score
0
How would one use a while loop in the C programmin language to ask the user if they want to terminate the program, or execute it again. You could say press 1 to terminate and press 2 to execute again. How would someone set that up with a while loop?
 
Computer science news on Phys.org
That's not what a while loop does; a while loop is merely one of the components you will use in your program.

If you write down, step by step, exactly how you want your program to behave, it will hopefully be clear how to write it!
 
I want to say:
while variable = 2
execute program

and I will ask the user to press 1 to use the program again, or press to to terminate the program.
 
are you home hurkyl?
 
user_choice = 1
while (user_choice == 1)
{
program code;
ask "what do you want to do (1 for repeat 2 for exit)";
scanf into user_choice;
}
 
That is exactly what I am doing and I am getting caugt in an infinite loop.
 
when I run the program it goes through it the first time fine, then it asks enter 1 to repeat or 2 to exit. When I enter 1 it goes into an infinite loop.
 
make sure you used &user_choice with the scanf argument, if you just put user_choice scanf will put 2 at the memory location 1...

you can printf user_choice and see if it got the right value just before the loop ends, so you'd be sure its 2...
or put your code here so we can see what's wrong...by the way... it's just style, but i suggest using boolean for that kind of thing:
Code:
run=1;
while(run) {
     ...
     scanf("%d",&user_choice);
     if (user_choice==2) run=0;
}
it's just nicer.

i once was an efficiancy freak, id use one integer to store 16 boolenas...
 
Last edited:
Nothing000 said:
I want to say:
while variable = 2
execute program

It should be:

while variable == 2.

You want == not = here, else it loops forever.
 
  • #10
I have the == sign there, I just wrote it wrong in this thread. Sorry. I will post the souce code soon so you guys can look it over to see if you can help me.
 
  • #11
help!

How will i make an output of all even numbers from 2-18 using a while loop structure?:confused:
 
  • #12
note that you must differ between 1 and '1'. one is a int the other is a char. This is important in the type of data you read from your scanf.

You could also look into the "break;" keyword. and figure out how to do an infinite loop with just while(?) {}. for instance while(true) {} is a C++ option.
 

Similar threads

Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
865
Replies
11
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K