Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this piece of code that I am trying to exit when the user inputs a number for K, how to i terminate the loop with that number?

for ( int K=0; K<=9; K++)
   {
    cout << "Enter the amount of Calories, Fats, Proteins & Other" << endl;
    cout << "Calories:" << endl;
        cin >> C;
    cout << "Fat:" << endl;
        cin >> F;
    cout << "Protein:" << endl;
        cin >> P;
    cout << "Other:" << endl;
        cin >> O;
    }


What I have tried:

I have tried using different loops and nothing seems to work
Posted
Updated 23-Mar-17 15:17pm
Comments
[no name] 23-Mar-17 20:40pm    
for ( int i=0; i<=K; i++)
Member 13080538 23-Mar-17 20:57pm    
so i have to make a new variable which is I?
Member 13080538 23-Mar-17 20:59pm    
it worked but if i plug in 3 foods it stops at 4
Member 13080538 23-Mar-17 21:45pm    
is there a way to make it repeat it just one less time?
[no name] 24-Mar-17 9:30am    
for ( int i=0; i < K; i++)

1 solution

Quote:
I have this piece of code that I am trying to exit when the user inputs a number for K, how to i terminate the loop with that number?

Since the user do not input anything for K, I fear we can't help you on this.
You need to refine the question with details on what you try to do.

For what I see, the for loop can completely be removed.
 
Share this answer
 
Comments
Member 13080538 23-Mar-17 21:21pm    
The solution given by NotPoliticallyCorrect works with just one extra output, the for statement cannot be taken out because the process needs to loop until the inputted K value is reached, the user does input a K value in the piece of code above the for loop that was not included
Patrice T 23-Mar-17 21:36pm    
Can't guess the code you didn't post.
Use Improve question to update your question.
Member 13080538 23-Mar-17 21:37pm    
thank you for the snarky comment, completely unnecessary to answering the question
Patrice T 23-Mar-17 21:40pm    
Give useful informations and you will get smart answers.
Member 13080538 23-Mar-17 21:41pm    
I don't care how "knowledgable" you are, don't be an ass.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900