C/C++ :: Convert A While Loop To A For Loop?

Mar 25, 2012

I need to convert the following while loop:

count = 1;
while (caloriesForItem != 0, count != numberOfItems )
{
cout<<"Enter the calories: ";
cin >> caloriesForItem;
totalCalories += caloriesForItem;
count++;
}

This is what I have come up with:

totalCalories += caloriesForItem;
for (count = 1; caloriesForItem != 0; count != numberOfItems)
{
cout<<"Enter the calories: ";
cin >> caloriesForItem;
count++;
cout << "Total calories eaten today = " << totalCalories << endl;
}

However the output is not the same?

View 6 Replies


ADVERTISEMENT

C++ :: Convert A Recursive Function To A Loop?

Dec 13, 2014

I am trying to convert a recursive function to a loop. However, it is not working.

//recursive function
int function(int x){
if (x % 3 == 0){

[Code]....

View 1 Replies View Related

C :: Loop Does Not Work And Program Quits Without Displaying For Loop Function

Oct 13, 2014

We are making a program--but every time we input a value for scanf, the following for loop does not work and the program quits without displaying the for loop's function. We are not getting any errors.

View 11 Replies View Related

C# :: Use Loop To Load Two Forms Back And Forth Then Stop At Loop 4

Feb 15, 2015

I have tried to submit this topic before but i didn't submit my whole code and it was removed. So here it is. All I am trying to do is load form2 from form1 then back to form1 from form2 for a certain number of times the get out of the loop. I am new to C-Sharp and it seems as though I cant seem to figure out a way to do this.

Here is form1 and form2 code. I have commented out a few things I have tried.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

[Code]....

View 3 Replies View Related

C :: Extra Loop Being Executed In Do-while Loop

Jul 1, 2013

Code:
#include <stdio.h>
#include<ctype.h>
void try5t(){

char choice;
int choiceint;

[Code] .....

Loop is repeated an additional time as shown in the screenshot:

View 9 Replies View Related

C++ :: How Much Milliseconds To Loop Through For Loop

Feb 20, 2014

Given a for loop:

int i;
for(i = 0; i < 1000; i++)
;

How many nanoseconds, or microseconds or milliseconds does it take for each iteration? And why do some use it as a timer mechanism?

View 3 Replies View Related

C/C++ :: How To Change For Loop Into Do While Loop

Apr 19, 2014

I already made a nested for loop into a while loop (below this) and now I'm trying to make the outer for loop into a do while loop, but it's not working.

#include <iostream>
using namespace std;
int main () {
int len;
int j;
int i;

[Code]....

And I can't make this code do the same thing. It stops after one loop, instead of continuing to the end. Why won't the loops continue?

#include <iostream>
using namespace std;
int main(){
int len;
cout << "Enter a number: ";

[Code] ....

View 2 Replies View Related

C# :: Loop Within A Loop?

Sep 25, 2014

I for the life of me can't get my logic straight. I have 2 lists, 1 a list of text, and the other a list of int.

Here's my main list: List<string> ListNeedToADD = ListOfSPSExt.Except(ListOfReportExt).ToList();
Here's my second list:IEnumerable<int> result2 = Enumerable.Range(1, Convert.ToInt32(myValue)).Except(hostList);

I need to loop through my main list, ListNeedToADD, and for each item in that list, grab 1 value from the 2nd list and send those 2 items to a method. Something like this:

foreach (string myString in ListNeedToADD)
{
foreach (var item in result2)
{
intSort = item;
insertReportLimits(attribTable, myString, intSort, con);
}
}

The problem with doing it the way I am above is it loops through ALL the items for each myString.

For my method, it needs to look like this: insertReportLimits(attribTable, "34257", 22, con); then it would move on to the next: insertReportLimits(attribTable, "34854", 27, con); etc etc.

View 4 Replies View Related

C/C++ :: Cannot Get Out Of Loop

Feb 22, 2015

I can't get out of my loop to fill the array. This array should stop and print after 10 inputs. but it keeps asking for input.

This is what my code is supposed to do. Use 0 as sentinel for an array. Only positive ints will be entered into the array. A negative int should result in an error message, then input will continue. The numbers will be inserted into the array in ascending order. Do not insert them then sort the array.

#include <iostream>
#include <cmath>
using namespace std;
const int arraySize =10;

[Code].....

View 4 Replies View Related

C++ :: For Loop To Count Down From 10 To 0

Apr 16, 2013

I am using a for loop to count down from 10 to 0 it's working to count down from 10 to 1 but when the program cames to the 0 then the program freezes by any reason.

Code:
#include <iostream>
using namespace std;
int main()
{
int number[2];
cout << "Enter number: ";
cin >> number[0];
if (number[0] == 1)

[Code] ....

View 11 Replies View Related

C :: Function To End A Loop?

Nov 8, 2013

Is there a way to end a loop ?

Code:
while ( menuopt == 1)
{
printf("Lets start
");
printf("12 x 4 = ?
");

[Code] ....

View 2 Replies View Related

C :: How To Get Rid Of Infinite Loop

Mar 1, 2013

What I need to do to get rid of the infinite loop?

Code:
do {
printf("Enter the number of tests:");
scanf("%d", &test);
if (test< 0 || test> 4)
printf("Wrong number. Please try again!
");
}
while (test< 0 || test>4);

View 9 Replies View Related

C :: Two Repeat One While Loop

Apr 17, 2014

So I have a programming assignment.

Code:
#include <stdio.h>
int main(void)
{
int input;
int sum;
int i;
int t;

[Code] .....

So you input an integer ex) 30

the printing of t =(i+i)+1 and i++ is carried out until i=30, which is when the sum of all the t's is printed.

If the value you entered is not an integer, the loop ends.

I'm supposed to do this with one while loop, and no more. I can't use the for loop either.

How can I do this?

View 7 Replies View Related

C :: Possible To Output By Using Only One Loop

Jul 21, 2013

is it possible to output like this using only one loop? if yes, how?

target output Code: ABCDE

EDCBA here is my code but using one loop im not getting my target output Code: #include <stdio.h>

main()
{
int x, y;

for(x='a', y='e'; x<='e'; x++, y--)
{
printf("%c
%c", x, y);
}
getch();
}

View 6 Replies View Related

C :: While Loop Not Disengaging

Nov 16, 2013

I have a while loop that will not disengage when the input given as "exit".

Code:
while (orders != "exit"){
for (i=0;i<25;i++){
for (j = 0; j<50;j++){
if (i == x && y == j){
printf("H");}
else {
printf("%c", map[i][j]);}

[Code]....

View 1 Replies View Related

C :: Can't Stop While Loop

Dec 6, 2013

I'm trying to create a connect four game, but I've ran into a problem while trying to read valid user input.

Code:

// 4 in a row game
#include <stdio.h>
#include <stdlib.h>
#define NUM_ROWS 6
#define NUM_COLS 7
}

[code]...

If the user enters an invalid number, then it works fine, but if a character is entered, the while loop in main never breaks.

View 9 Replies View Related

C :: Variable In While Loop

Feb 6, 2013

I'm writing a program to sum up the even numbers in between 1 and 100, using a while loop. At the beginning of my program, I initialize a variable "sum" to 0, and a variable "temp" to 1. Afterwards I enter a loop where I determine if "temp" is even or not, and if so add it to sum. However, at the end of my program, when I print "sum", I get a result of 0. Below is my code.

Code:

#include <stdio.h>
int main(void)
{
int sum = 0, temp = 1;
}

[code]...

View 4 Replies View Related

C :: If Loop Not Activating

Nov 24, 2013

Why my if statement for argc <3 will not execute? as the one for argc >3 does...

Code:

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
int i;
int fnlength = strlen(argv[2]);
char *namein = argv[2];
int j = (fnlength-1);
char input[100000];
}

[code]....

View 2 Replies View Related

C :: Do While Loop Error

Feb 19, 2015

While trying to redo everything in c i have an error on a do while loop and i don't understand i've corrected everything else but i don't understand why the error occurs even though it says how to fix it it says error expected ) before token -line 18 part of code

Code:

int main(){
Beep (523,1000); // sound at 523 hertz for 1 000 milliseconds
char cPresent;
do
}

[code]....

this is just extra bits i've added on to the assignment but I've worked on the c++ code for a week now i have less than a day to redo it

View 2 Replies View Related

C++ :: While Loop Keeps Asking For Input After EOF

May 11, 2013

This is an example from C++ Primer on while loops shortened for simplicity:

int main() {
int value = 0;
while(cin >> value) cout << value;
return 0;
}

When I compile and run the above code the program keeps asking for input after I input nothing but pressing ENTER no matter how many times. The only way I can get it to stop asking for input is to input something other than an int such as a char or string. Program executes as intended after that. I have googled this issue and read all seemingly relevant results and nothing seems to pertain to my exact problem. I think it may have something to do with my computer's own settings or something and am baffled as to what it may be.

View 10 Replies View Related

C++ :: Using While Loop For Factorial

Mar 2, 2014

I need to write a complete program using "While Loop" to calculate 1! to 12! using just "int" variables. Only from 1 to 12 and there are no other inputs.. This is my first time using While loop.

View 2 Replies View Related

C++ :: While Loop Age Guess

Feb 20, 2013

Write a C++ program that uses a while loop to guess a persons age, ask the user if the want to try again if no end the loop. Of course if they guess it the loop also ends

View 3 Replies View Related

C++ :: End Loop After Answer Is 0

Mar 23, 2014

I am writing a program to tell how long it will take for a loan to be paid off supposing the desired loan is 100,000, the annual percentage rate is 6, and the monthly payment is 1,000. What happens is after I run the program, I get the output of "-24.60" after the last month is paid off. I'm trying to get it to where the last monthly payment is only enough to make the loan 0. Then I want the program to end.

#include <iostream>
#include <iomanip>
using namespace std;

[Code]....

View 4 Replies View Related

C++ :: Value In Loop Won't Change?

Sep 3, 2014

I am working on a program that is supposed to do I/O file streaming. I dont know if I can properly explain how its supposed to work but I have found the error on my code, I just dont know WHY the error is happening? If you look in the while loop, count changes values like it should, but MOVE and TIP doesnt. Count starts at value 2. So move is 108. Then count becomes 4, but move stays at 108?

tring rec;
fstream myfile("File.txt", ios::in | ios::out);
myfile.seekg(myfile.tellg(), ios::beg);
myfile.seekp(51, ios::beg);

[Code]....

The lines of code im getting errors on is

int move = count * 54;
int tip = move + 51;

Move wont change its value? It stays 108, causing this infinite while loop. Where is the error in my code to cause move to not change values like count does?

I seem to be struggling with I/O File streaming :(

View 2 Replies View Related

C++ :: Why Cannot Jump Out Of For Loop

Jun 6, 2013

I got code like this:

#include <iostream>
#include <vector>
using namespace std;
const long nx(3);
const long ny(3);
const long seal(-1);
long TotP(0);
class pore {

[Code]...

I can print out all those TotP, but cannot get Finish loop print out, why? If I put any statement below, which seems won't be executed, it is like it never jump out from the loop and die inside.

View 9 Replies View Related

C++ :: Stuck With A Do While Loop

Mar 27, 2013

Alright I have a program that needs to calculate different rates of commission for a particular sale

I'm stuck in a loop and I'm not sure how to update the final cout statements as well.

#include <iostream>
#include <cmath>
using namespace std;

[Code].....

and yes those are the actual rates i need to calculate and they are in increments of 5%

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved