1,105,391 Community Members

I need help learning how to do a loop.

Member Avatar
skiboy209
Newbie Poster
10 posts since Sep 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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)";
  } 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

Member Avatar
rubberman
Senior Poster
3,997 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 501 [?]
Skill Endorsements: 87 [?]
 
1
 

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... :-)

Member Avatar
skiboy209
Newbie Poster
10 posts since Sep 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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');
Question Answered as of 2 Years Ago by rubberman
Member Avatar
cherrymae.calma
Light Poster
33 posts since Aug 2011
Reputation Points: -2 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
rubberman
Senior Poster
3,997 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 501 [?]
Skill Endorsements: 87 [?]
 
0
 

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.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: