Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,116 people online right now. Registration is fast and FREE... Join Now!




output loop

 
Reply to this topicStart new topic

output loop

chrisbenoit06
24 Oct, 2006 - 04:38 PM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 13


My Contributions
Hey Everyone, Can anyone help me out with my question?
How would I be able to make the program loop so that it allows me to repeatedly test the function during the output instead of running it over and over each time for different values of Z? Any ideas? Thanks

CODE

#include <iostream>



double f(double x);

int main()
{
double z;
cout<<"Enter a value for z\n";
cin>>z;
cout<<f(z)<<endl;

}
double f(double x)
{
if(x>0){
double sum=0;
for(int i=1;i<=(int)(x);i++){
sum+=i;
}
return sum;
}
else if(x==0){
return 0;
}
else{
return x+2;
}
}

User is offlineProfile CardPM
+Quote Post

dragonlady
RE: Output Loop
24 Oct, 2006 - 08:16 PM
Post #2

D.I.C Head
**

Joined: 7 Aug, 2005
Posts: 57


My Contributions
Nice screen name smile.gif ; I assume you're a fellow wrestling fan?
Anyway, as for the question at hand, try a while loop. You could run it for a predetermined number of iterations (so then you'd have a counter, and you'd update it by 1 after each iteration, and then quit when you've reached the number of tests you want).

Alternatively, you could run the while loop until the user enters STOP or some other response. Then you would test the input to see if the user said stop or entered a number to test. If you took this route, you'd have to change the z variable to a string (or char; you could ask the user to enter 'n' to end the tests), check the value of z, and if it's a request to quit, end the loop, and if it's a number, convert it to a double.
User is offlineProfile CardPM
+Quote Post

chrisbenoit06
RE: Output Loop
25 Oct, 2006 - 07:30 AM
Post #3

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 13


My Contributions
Thanks for replying dragonlady. Yes I am a huge wrestling fan and wrestler myself. I am new to programming however. I think i will go with the while loop up to z=5. Where abouts should I insert it in the program?
Thanks
User is offlineProfile CardPM
+Quote Post

dragonlady
RE: Output Loop
25 Oct, 2006 - 08:13 AM
Post #4

D.I.C Head
**

Joined: 7 Aug, 2005
Posts: 57


My Contributions
You can put the while loop in the main function, where you're prompting the user for a value and then outputting the result. Do you know how to use a while loop? Since you said you are new to programming, I don't want to automatically assume that you know. Here's an example:

CODE

int i = 0;
while(i < 5)
{
  cout<<i<<endl;
  i++;
}


In this example I print out numbers 0 through 4. Notice that I updated i in the while loop; if you forget that you'll have an infinite loop.

I could have achieved the same thing with a for loop:
CODE

for(int i = 0; i<5; i++)
{
  cout<<i<<endl;
}


Either type of loop should work in your case.

Oh and its cool that you're a wrestler smile.gif I have the utmost respect for what wrestlers do and how they put their bodies on the line to entertain fans cool.gif
User is offlineProfile CardPM
+Quote Post

chrisbenoit06
RE: Output Loop
25 Oct, 2006 - 12:53 PM
Post #5

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 13


My Contributions
Thank-You very much. Your very kind. However I am still having difficulties with my code. Essentially what I’m trying to output is “type a vale for z” and then produces the answer in which I can repeatedly test the next value of z.
Example:
Enter a value for z
1
1
Enter next value for z
2
Etc

Is that possible?
User is offlineProfile CardPM
+Quote Post

chrisbenoit06
RE: Output Loop
25 Oct, 2006 - 04:21 PM
Post #6

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 13


My Contributions
I got it....but is it write?

CODE


#include <iostream>

double f(double x);

int main()
{
double z;
cout<<"Enter a value for z: ";
cin>>z;
cout<<"f(x)="<<f(z)<<endl;

cout<<"Enter a value for z: ";
cin>>z;
cout<<"f(x)="<<f(z)<<endl;

cout<<"Enter a value for z: ";
cin>>z;
cout<<"f(x)="<<f(z)<<endl;
}

double f(double x)
{
if(x>0){
double sum=0;
for(int i=1;i<=(int)(x);i++){
sum+=i;
}
return sum;
}
else if(x==0){
return 0;
}
else{
return x+2;
}
}

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Output Loop
25 Oct, 2006 - 05:41 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Well that would most certainly work. Although its not very efficient and you have some code redundancy.

It would be much better if you put this inside a FOR loop that iterates 3 times. I am assuming based on your code that you only want to get 3 responses. Otherwise it would be better to use a WHILE loop with a sentinal value that the user would enter to cause the loop to exit.

Like this:
CODE


#include <iostream>

double f(double x);

int main()
{
double z;

for(int i = 0; i<3; i++)
{
   cout<<"Enter a value for z: ";
   cin>>z;
   cout<<"f(x)="<<f(z)<<endl;  
}

}

double f(double x)
{
if(x>0){
double sum=0;
for(int i=1;i<=(int)(x);i++){
sum+=i;
}
return sum;
}
else if(x==0){
return 0;
}
else{
return x+2;
}
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:00AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month