Basic Loop back to beginning of cin

Im trying to loop back to the input with updated values to my variable

Page 1 of 1

2 Replies - 7093 Views - Last Post: 12 August 2008 - 10:18 PM Rate Topic: -----

#1 zartek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-May 08

Basic Loop back to beginning of cin

Post icon  Posted 12 August 2008 - 09:48 PM

Im trying to loop back to the input with updated values to my variables, using the result from my if statement to send back up to cin and use again.

anybody have an idea on how to do this?
here is my code


#include <cstdlib>
#include <iostream>

using namespace std;

main()
{
	  
	  int n = 0;
	  
	  int made = 0;
	  int total = 0;
	  

cout << "Enter number:" << endl;
cin >> n;
if (n==1)
{
made == made++;
total == total++;

cout << " You are " << made << " of " << total << endl;
	  
}
else
{
total++;	
 cout << " You are " << made << " of " << total << endl;   
}



system ("pause");//i know im not supposed to use this but my program wont stay open without it.

cin.get();


}





Thanks again for all the help

Zartek Galicia

This post has been edited by zartek: 12 August 2008 - 09:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Basic Loop back to beginning of cin

#2 ibaraku  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 12-May 07

Re: Basic Loop back to beginning of cin

Posted 12 August 2008 - 10:07 PM

View Postzartek, on 12 Aug, 2008 - 09:48 PM, said:

Im trying to loop back to the input with updated values to my variables, using the result from my if statement to send back up to cin and use again.

anybody have an idea on how to do this?
here is my code


#include <cstdlib>
#include <iostream>

using namespace std;

main()
{
	  
	  int n = 0;
	  
	  int made = 0;
	  int total = 0;
	  

cout << "Enter number:" << endl;
cin >> n;
if (n==1)
{
made == made++;
total == total++;

cout << " You are " << made << " of " << total << endl;
	  
}
else
{
total++;	
 cout << " You are " << made << " of " << total << endl;   
}



system ("pause");//i know im not supposed to use this but my program wont stay open without it.

cin.get();


}





Thanks again for all the help

Zartek Galicia


Well, usually went you want to loop back you can use a do while loop in your program, so it would look something like this


cout << "Enter number:" << endl;
cin >> n;
do
{
if(n == 1)
{
made == made++;
total == total++;

cout << " You are " << made << " of " << total << endl;
}
	  
else
{
total++;	
 cout << " You are " << made << " of " << total << endl;   
}

}while(condition that would make you exit the loop);




I hope that helps
Was This Post Helpful? 0
  • +
  • -

#3 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 556
  • Joined: 24-July 08

Re: Basic Loop back to beginning of cin

Posted 12 August 2008 - 10:18 PM

Ugh...I have problems with the while loop with conditions other than while(true) or simple like that so i work around that by
#include <iostream>
using namespace std;


int main(void)
{
   int x = 0;
   while (true)
   {
       x++;
       if (x == 100)
       {
             break;
       }
       cout << "This is annoying =)" << endl;
   }
}



You could do something like that with x being your var and 100 being your condition
such as
if (turns == 3)
{
   break;
}


Just put your input at the begining of the while loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1