Thread: Nested Loops for begginers

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Huintsville, AL.
    Posts
    14

    Nested Loops for begginers

    How do I get this block of code to loop conditionally?
    do I need to put this if else inside a while loop, do while, or another if else

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int Answer;
    	char CreditCardNumber[40];
    
    //I dont know if I should start with do, while, if, or what********
    	
          do
    	{
    	
    	cout<<"if your ready to enter credit card number: "<<endl; 
    	cout<<"(Enter: [1 for yes]- [2 for no])";
    	cin>>Answer;
    	cin.sync();
    	cin.get ();
    	
    	
    	if (Answer==1)
    	{ 
    		cout<<"ready to process transaction";
    		cout<<"please enter credit card number";
    		cin>>CreditCardNumber;
    		cin.sync();
    		cin.get();
    	}
    	
    //this is where my condition to start all over is*******
    	
               while (Answer==1)
    	{
    			cout<<"would you like another transaction?"<<endl;
    			cout<<"(Enter [1 for yes] - [2 for no])";
    			cin>>Answer;
    			cin.sync();
    			cin.get();
    
    // I want it to loop back to the top*******
    			
    	{
    	
    	}
    		
    			return 0;
    		}
    	
    				
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    It doesn't matter how you loop(other that if-else-goto !! ...avoid that)... just keep the logic correct, instead of trying to write code mechanically.

    That part you want to loop continuously would be in a "while(true)" block. Just remember to break; out when the time comes.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    Huintsville, AL.
    Posts
    14
    I made changes and now the command prompt wont stay put long enough for me to test it out
    Do I need a break some where in my code or do I need another cin.get() inside somewhere?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int Answer, AllDone=2;
    	char CreditCardNumber[40];
    	
    	if (AllDone==1)
    	{
    	
    	cout<<"if your ready to enter credit card number: "<<endl; 
    	cout<<"(Enter: [1 for yes]- [2 for no])";
    	cin>>Answer;
    	cin.sync();
    	cin.get ();
    	
    	
    	if (Answer==1)
    	{ 
    		cout<<"ready to process transaction";
    		cout<<"please enter credit card number";
    		cin>>CreditCardNumber;
    		cin.sync();
    		cin.get();
    	}
    	
    	
    	
    			cout<<"would you like another transaction?"<<endl;
    			cout<<"(Enter [1 for yes] - [2 for no])";
    			cin>>AllDone;
    			cin.sync();
    			cin.get();
    	
    	
    // I want it to loop back to the top*******
    			
    	         
    			
    	
    
    	
    	
    			return 0;

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    this seems like an ideal situation for a do-while loop. you always want the loop to run at least once.

    the basic structure of a do-while loop is as follows:
    Code:
    do
    {
      // code goes here
    } while (condition)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to indent more consistently.
    As for your question, SourceForge.net: Pause console - cpwiki (also verify that you can access this without a sourceforge account).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Location
    Huintsville, AL.
    Posts
    14
    this is what I got but it's one really small problem that I still have and cannot find



    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int Answer, AllDone = 1;
    	char CreditCardNumber[40];
    	
    	
    	do
    	{
    	
    	cout<<"if your ready to enter credit card number: "<<endl; 
    	cout<<"(Enter: [1 for yes]- [2 for no])";
    	cin>>Answer;
    	cin.sync();
    	cin.get ();
    	
    	if (Answer==1)
    	{ 
    		cout<<"ready to process transaction\n";
    		cout<<"please enter credit card number";
    		cin>>CreditCardNumber;
    		cin.sync();
    		cin.get();
    	}else
    	//between this else and while is where the problem beggins
    	//heres where it loops back to the beggining without displaying the rest
    	}while (AllDone==1)
    	   
    			cout<<"would you like another transaction?"<<endl;
    			cout<<"(Enter [1 for yes] - [2 for no])";
    			cin>>AllDone;
    			cin.sync();
    			cin.get();
    			
    			
    	   
    // I want it to loop back to the top only if they have another transaction*******
    			
    	{ cout<<"thank you!!!";
    	}
    	         
    			return 0;
    		}
    what is my problem here???

    P.S. this isn't homework I'm just trying to relearn C++

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    For starters, you should indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2009
    Location
    Huintsville, AL.
    Posts
    14
    finally got it to work
    Thank You everybody

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int Answer, AllDone = 1;
    	char CreditCardNumber[40];
    	
    	
    	do
        {
    	
    	cout<<"if your ready to enter credit card number: "<<endl; 
    	cout<<"(Enter: [1 for yes]- [2 for no])";
    	cin>>Answer;
    	cin.sync();
    	cin.get ();
    	
    	     if (Answer==1)
    	    { 
    		cout<<"ready to process transaction\n";
    		cout<<"please enter credit card number";
    		cin>>CreditCardNumber;
    		cin.sync();
    		cin.get();
    	    }
    	//between this else and while is where the problem beggins
    	//heres where it loops back to the beggining without displaying the rest
        
    	   
    			cout<<"would you like another transaction?"<<endl;
    			cout<<"(Enter [1 for yes] - [2 for no])";
    			cin>>AllDone;
    		
    			cin.get();
    	}while (AllDone==1);
    			
    	   
    // I want it to loop back to the top*******
    			
    	   { cout<<"thank you!!!";
    	   cin.get();
    	   }
    	         
    	return 0;
    }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You still haven't indented properly. Why?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2009
    Location
    Huintsville, AL.
    Posts
    14
    I don't know I keep forgetting - I'm really just trying to relearn C++ but I'm not to sure how the indentation should look

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by jason007thomas View Post
    I don't know I keep forgetting - I'm really just trying to relearn C++ but I'm not to sure how the indentation should look
    Correct indentation is not a luxury, it is the bare essentials and should be learned from day 1, especially if you want to post on forums where others read your code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested loops
    By laurenlb in forum C Programming
    Replies: 5
    Last Post: 03-02-2011, 12:49 AM
  2. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 06:58 AM
  3. nested while loops possible?
    By prepare in forum C Programming
    Replies: 15
    Last Post: 10-18-2004, 02:10 AM
  4. help with nested loops
    By geo_c in forum C Programming
    Replies: 15
    Last Post: 07-11-2004, 01:35 PM
  5. nested for loops
    By akub3 in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 06:21 AM