473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

I need help to limit the loop into the next 4 interations

I have a user input, and I need the loop to continue for the next 4 iterations of the user input. I have no idea how because everytime I try to do it, it becomes either and infinite loop or just crashes.. Please help
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main ()
  4. {
  5.     double H, t;
  6.     double a, b;
  7.  
  8.     a = 2.13;
  9.     b = 0.05;
  10.  
  11.     printf("Please enter input fot t:");
  12.     scanf("%lf", &t);
  13.  
  14.     H = ((a * t)*(a * t))-((b * t)*(b * t)*(b * t));
  15.  
  16.     if (H > 100)
  17.     {
  18.         printf("\nHigh\n");
  19.     }
  20.     else if (H > 50)
  21.     {
  22.         printf("Average\n");
  23.     }
  24.     else 
  25.     {
  26.         printf("Low\n");
  27.     }
  28.  
  29.     printf("\nt\t\t   H\n");
  30.     printf("----------------------\n");
  31.  
  32.     for (t=t; t < (t + 5); t++)
  33.     {
  34.         H = ((a * t)*(a * t))-((b * t)*(b * t)*(b * t));
  35.         printf("%.0f\t\t%.2f\n", i, H);
  36.  
  37.     }
  38.  
  39.     return 0;
  40. }     
  41.  
  42.  
Apr 10 '14 #1
2 1202
Banfa
9,065 Expert Mod 8TB
t < (t + 5) will always be true by definition.

Think of any number, add 5 to it. Is the result greater than the original number?

You need to use 2 variables, t, which you increment and another variable that you set up before the loop starts to contain the end point for the loop.

Expand|Select|Wrap|Line Numbers
  1. max = t + 5;
  2. for( ; t < max; t++)
  3. {
  4.   ...
  5. }
  6.  
Apr 10 '14 #2
donbock
2,426 Expert 2GB
Is your intent to loop through your program a fixed number of times? If so, then I wouldn't use user input t as a loop variable. Better to add a separate variable whose only purpose is to control the loop.

Expand|Select|Wrap|Line Numbers
  1. int i;
  2. for (i=0; i<5; i++)
  3. {
  4.    ...
  5. }
  6.  
By the way, it is unusual to use a floating point variable (t) to control a loop.
Apr 10 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Eric Haskins | last post by:
Ok I am trying to get these loops figured out I have 10 files being uploaded- $_FILES I need to run str_replace on all 10 to eliminate any oddities " ","%20" to clean up the filenames before...
1
by: Jeremy Langworthy | last post by:
Hi I have a dynamicly generated form (well the elements are at least) that looks something like this: while( not end of returned records): <input name="plan_id" type="checkbox" id=""...
1
by: Geoff Lane | last post by:
Browser: IE5+ I have a form with an unknown number of checkboxes (the page is generated by a server-side script). I need to loop through those checkboxes, test whether the name of each checkbox...
4
by: CMW | last post by:
In VB.NET I'm retrieving a single table from a SQL Server (tblformdata). I need to search each record in the dataset to see if any data is missing from the records (i.e., missing first name,...
0
by: EiEiO | last post by:
Hello all. I am hoping I could get some suggestions on moving forward. My Challenge: Importing data from one table to another where the field names almost never match. My Form has a list...
2
by: kristopher.erickson | last post by:
Hereis some basic code that works fine: For Each itm In fld.Items I = I + 1 j = 1 Set rng = wks.Cells(I, j) If itm.Start <"" Then rng.Value = itm.Start j = j + 1
1
by: redgrL86 | last post by:
Hi, I am trying to create an XSL file that can be applied to numerous XML files (all XML files have similar structure but varying length). The XSL and abbreviated XML codes are below. Within the XSL...
4
toxicpaint
by: toxicpaint | last post by:
Hi, can anyone give me a hand. I'm currently displaying 4 random images at the top of a page. I did this using an array of 35 pictures and then writing them to page. The problem I have is that in...
2
by: moondaddy | last post by:
Using c# 3.0: I need to do a loop and for each iteration I need to call the next letter of the alphabet. Sometimes this code may only loop 10 times, and other cases it may loop 100 times. ...
2
by: fran7 | last post by:
Hi, I know this is simple thing but always beats me. I have this script that works fine, I just want to add a loop so that it returns all the entries instead of just one. <% ' ADO Constant....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.