I have had this problem many times and I have never been able to get my while loops corrected, hence the reason I never used them, so maybe I need a professional solution to the problem.

I never really got classes on how they worked, although they had some relevance to structure, the public private and protected always confused me, still does in a way, but I was reading on cprogramming.com and well they made it a bit clear but not the whole way. SO to keep my program running I said "hey lets put a while loop in there to see what all my outputs are" boy did I do something wrong with my loops cause it posts the same thing over and over and I dunno how to fix it, here is my cheesy sample:

// class.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;

class Gender
{
public:
		Gender();
		~Gender();

		void setGender(int G);
		 int readGender();
private:
		int gender;
};

int _tmain(int argc, _TCHAR* argv[])
{
	
	int s;
	Gender being;
	
	cout << "What Gender are you?" << endl;
	cout << "Press:\n(1) For male\n(2) For female, Or\n(3) For not sure." << endl;
	cout << "Your Gender: "; cin >> s;

	being.setGender(s);

	while ( s != '\0' )
	{
	cout << "\n\nYour selected gender was: " << being.readGender();
	
		switch (being.readGender())
		{
			case 1:
					cout << "\n\nDomination!" << endl;
					break;
			case 2:
					cout << "\n\nPuny little girl!" << endl;
					break;
			case 3:
					cout << "\n\nSeek help, stat!" << endl;
					break;
			default:
					cout << "\n\nYou do not exist!" << endl;
		}				
	}
	_getch();
	return 0;
}

Gender::Gender()
{
	gender = '\0';
}
Gender::~Gender()
{

}
void Gender::setGender(int G)
{
	gender = G;
}
int Gender::readGender()
{
	return gender;
}

Recommended Answers

All 2 Replies

Line 33, your infinitely looping because s will never be equal to null. I assume you did that because you think the break statement will take you out of the loop, but it only will take you out of the switch statement. Besides what you are trying to do(I assume for practice) is not very fitting for the context of your solution.

At first on line 24 set the value of s=5 or some value greater then 3.

2ndly change 2 line(line 29 and line 31) before the while loop.

while ( s!= '\0' )
	{
	cout << "Your Gender: "; cin >> s;
	being.setGender(s);
	cout << "\n\nYour selected gender was: " << being.readGender();
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.