Like Tree1Likes
  • 1 Post By iMalc

Nested Loops for begginers

This is a discussion on Nested Loops for begginers within the C++ Programming forums, part of the General Programming Boards category; How do I get this block of code to loop conditionally? do I need to put this if else inside ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    2,457
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    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
    21,794
    For starters, you should indent your code properly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    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,307
    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.
    Elysia likes this.
    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-01-2011, 11:49 PM
  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, 05:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21