#include <iostream>
#include <string>
#include <windows.h>
using std:: cout;
using std:: cin;
using namespace std;


int main()
{
 unsigned long n;
 char Answer;
 string mystr;
 do {
 cout << " What is your name? ";
 getline (cin, mystr);
 cout << "Hello " << mystr << ".\n";
 cout << "What is one of your hobbies? ";
 getline (cin, mystr);
 cout << "Cool, I also like " << mystr << "!\n";
 cout << "Can you guess my name? ";
 getline (cin, mystr);
 cout << "Haha, good guess " << mystr << " that is funny, trick question I do not have a name! \n";
 cout << "Do you like parkour?(y/n)? ";
	cin >> Answer;

	if(Answer == 'y' || Answer == 'Y')
		cout << "\nYou have been promoted to cool.\n";
	
	if(Answer == 'n' || Answer == 'N')
        cout << "\nDo you know what it is?(y/n)? ";
        cin >> Answer;
        
    if(Answer == 'y' || Answer == 'Y')
        cout << "\nGood.";
        
    if(Answer == 'n' || Answer == 'N')    
       	cout << "\nGo and Google it, then talk to me again.";
       	
  cout << " I am tired, I will go to sleep now. ";
  cout << "Do you want to do it again? (y/n)";
  } while ( Answer = n);
  return 0;
}

I am writing a program, this is my first program not copied out of a book. I am trying to loop from the while back up to the do. I do not know if this is the correct form of a loop to use because I am a noob. All help and pointers will help :) *edit* When i run it and get to the end it meshes together and asks 2 questions at the same time

There are a variety of loop constructs. Which to use depends upon a number of factors. In your case, a do {...} while (condition) block would be a reasonable approach. Unfortunately, your control condition statement at the end is bogus. The variable n has not been set, it is a different type than the Answer variable, and you are using an assignment operator instead of a comparison. So change the line

} while ( Answer = n );

to

} while ( Answer == 'n' || Answer == 'N' );

One last issue, is that after you ask the question "Do you want to do it again? (y/n)" you don't take input from the user, so the conditional clause at the end of the loop will not be properly set, at least as per your apparent intent... :-)

commented: Very helpful and not critical +0
#include <iostream>
#include <string>
#include <windows.h>
using std:: cout;
using std:: cin;
using namespace std;


int main()
{
 unsigned long n;
 char Answer;
 string mystr;
 do {
 cout << " What is your name? ";
 getline (cin, mystr);
 cout << "Hello " << mystr << ".\n";
 cout << "What is one of your hobbies? ";
 getline (cin, mystr);
 cout << "Cool, I also like " << mystr << "!\n";
 cout << "Can you guess my name? ";
 getline (cin, mystr);
 cout << "Haha, good guess " << mystr << " that is funny, trick question I do not have a name! \n";
 cout << "Do you like parkour?(y/n)? ";
	cin >> Answer;

	if(Answer == 'y' || Answer == 'Y')
		cout << "\nYou have been promoted to cool.\n";
	
	if(Answer == 'n' || Answer == 'N')
        cout << "\nDo you know what it is?(y/n)? ";
        cin >> Answer;
        
    if(Answer == 'y' || Answer == 'Y')
        cout << "\nGood.";
        
    if(Answer == 'n' || Answer == 'N')    
       	cout << "\nGo and Google it, then talk to me again.";
       	
  cout << " I am tired, I will go to sleep now. ";
  cout << "Do you want to do it again? (y/n)";
  cin >> Answer;
  } while ( Answer = 'n' || 'N');
  return 0;
}

So I changed the code to this but when i do "n" to close the program it just restarts. Haha sorry didnt catch the

} while ( Answer = 'n' || 'N');

instead of

} while ( Answer == 'n' || Answer == 'N');

Even after fixing that it still restarts when pushing "n" After playing around with it i figured out the problem. It needs to be

} while ( Answer == 'y' || Answer == 'Y');

not

} while ( Answer == 'n' || Answer == 'N');

what kind of loop you want to learn? there are three kind of looping statements the for loop,while loop and the do while loop.

Re: while (Answer == 'y' || Answer == 'Y') vs. while (Answer == 'n' || Answer == 'N') - that was a bit of a trick to see if you would come up with the correct answer... :-) Good start. As cherrymae.calma said, there are generally 3 types of loop constructs. You have used the do {} while (condition); loop. There are also for (start-condition, test-condition, next-step) and while (condition) {} loops. There is a fourth one, that should cause you to fail a project - a label + goto statement. It is rare for this fourth kind to be needed, though I have seen some kernel and driver code where it may have been appropriate due to behavior of volatile data and hardware event handling. With luck, you will never have to use it. In 30+ years of C and C++ development, I have possibly needed to use the label+goto twice under these conditions (kernel and/or hardware driver support), and the last time was over 20 years ago when developing TCP/IP driver code for a hard real-time operating system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.