I have this working code but my teacher wants really specific things. codes always have to be OOP, and this needs to to have no more then 2 cout. i have 3 but cant seem to find away to take one out. my friend told me i should do this in arrays, but honestly im not really good at this so im really confused about what to do. again sorry if im posting this in the wrong place or posting it in a bad way. thanx u in advance. here is the code,

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

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

	class Diamond
	{
	private:
	
	public:
		Diamond(){
		
	int i=0, j=0, NUM=3;
     
    for(i=-NUM; i<=NUM; i++)
     
    {
     
    for(j=-NUM; j<=NUM; j++)
     
    {
     
    if( abs(i)+abs(j)<=NUM)
     
     { cout<<"*"; }
     
    else { cout<<" ";}
     
    }
     

    cout <<endl; 
		}	
	}
};
    int _tmain(int argc, _TCHAR* argv[])
{
     
     Diamond shape;
    
    return 0;
    }

Edited 5 Years Ago by turista: n/a

codes always have to be OOP

OOP for the sake of OOP, eh? Your teacher sounds like an idiot.

i have 3 but cant seem to find away to take one out.

The easiest way would be combining the two from your inner loop with the conditional operator:

#include <cstdlib>
#include <iostream>

using namespace std;

class Diamond
{
public:
    Diamond()
    {
        int i = 0, j = 0, NUM = 3;

        for (i = -NUM; i <= NUM; i++)
        {
            for (j = -NUM; j <= NUM; j++)
            {
                cout << (abs(i) + abs(j) <= NUM ? "*" : " ");
            }

            cout << endl;
        }
    }
};

int main()
{
    Diamond();
}

ty and i see how u merged those together. and now i get this error lol

1>c:\users\h427\desktop\2coutdiamondfinal\2coutdiamondfinal\2coutdiamondfinal.cpp(4) : warning C4627: '#include <cstdlib>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\h427\desktop\2coutdiamondfinal\2coutdiamondfinal\2coutdiamondfinal.cpp(5) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\h427\desktop\2coutdiamondfinal\2coutdiamondfinal\2coutdiamondfinal.cpp(32) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Build log was saved at "file://c:\Users\h427\Desktop\2coutdiamondfinal\2coutdiamondfinal\Debug\BuildLog.htm"
1>2coutdiamondfinal - 1 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

i added

#include "stdafx.h"

and now it runs correctly! :) thanx!

Have you tried adding stdafx.h back? Better yet, have you considered turning off that feature entirely so that you can actually use standard C++ without Microsoft-isms?

Have you tried adding stdafx.h back? Better yet, have you considered turning off that feature entirely so that you can actually use standard C++ without Microsoft-isms?

honestly i have no idea what u mean. im opening visual studio 2008 the exact way my teacher showed us. i have another program homework im doing and i get the same errors about the stdafx.h and even if i add that they still dont work. this class is hard, and i think its hard cause my teacher doesn't really explain well...

I have this "guess my number code" that he showed us. its a single player against the computer. and he wants this done to it, with out him explaining to us how the hell to do it..

Two - Player Game (10 bonus points)

Modify the GMN class to change the game into a two-player game with multiple rounds.

Each player takes a turn and the player who guesses correctly is awarded points at the end of the round.

At the conclusion of a round of play, ask the players if they want to play another round. If the answer is YES, then play another round. If the answer is NO, display the player's scores and declare the player with the most points the winner.

Here is what my friend and I have so far. of course it doesnt work lol.

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


#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>


using namespace std;
const int MAX_NUMBER = 10;
char playAgain='y';

class NumberGenerator
{

private:
	int theNumber;

public:
	//Constructor
	NumberGenerator(){
		srand(time(0)); 
		newNumber();
	}


	
	while (playAgain !='n'){
	int getNumber(){
		return(theNumber);	
	}
	{
//method
	void newNumber(){
		int originalNumber = theNumber;
		
		do{
			theNumber = rand() % MAX_NUMBER + 1; // Random Number between 1 and 10
		}while(originalNumber == theNumber);
	}
	
	//returns true if guess is correct, return false if guess is not correct
	bool isCorrect(int guess){

		if(guess == theNumber){
			return(true);
		}
		else if(guess > theNumber){
			cout << "You Guessed Too High!!" << endl;
		}
		else {
			cout << "You Guessed Too Low!!" << endl;
		}

		return(false);

	}
		
};

	class Player
	{
	private:
		string name;
		int theGuess;
//void means its not going to return
		void init(){
		  theGuess = 0;
		}


	public:// two constructors to personalise the game
		Player(){
			init();

		}

		Player(string playerName){
			init();
			name = playerName;

		}

		string getName(){
			return (name); 
		}

		int takeYourTurn(){
			do{
				cout << "\n" << name << ", Whats Your Guess: ";
				cin >> theGuess;

				if(theGuess > 0 && theGuess <= MAX_NUMBER){
					break;
				
				}
				else{
					cout << " ERROR: Your Guess Must Be Between 1 and "
						<< MAX_NUMBER << endl;
				}


			}while(true);



			class Player2
	{
	private:
		string name;
		int theGuess;

		void init(){
		  theGuess = 0;
		}


	public:
		Player2(){
			init();

		}

		Player2(string playerName){
			init();
			name = playerName;

		}

		string getName(){
			return (name); 
		}

		int takeYourTurn(){
			do{
				cout << "\n" << name << ", Whats Your Guess: ";
				cin >> theGuess;

				if(theGuess > 0 && theGuess <= MAX_NUMBER){
					break;
				
				}
				else{
					cout << " ERROR: Your Guess Must Be Between 1 and "
						<< MAX_NUMBER << endl;
				}


			}while(true);



			return(theGuess);

		
		
		}


	};


int _tmain(int argc, _TCHAR* argv[])
{

	NumberGenerator gen;
	Player p1 ("Chris");
	
NumberGenerator gen;
Player P2 ("BoB");

	p1.takeYourTurn();

	cout << "The Initial Magic Number Is: " << gen.getNumber() << endl;

	gen.newNumber();
	cout << "The Next Magic Number Is: " << gen.getNumber() << endl;

	gen.newNumber();

	for(int i=0; i < 5;i++){
		int guess;
		cout << "guess: ";
		cin >> guess;
		if(gen.isCorrect (guess)== true){
			cout << "You Are Correct!!" << endl;
			
		}else{
			cout << "Sorry, You Suck!!\n" << endl;
		}
	}



P2.takeYourTurn();

	cout << "The Initial Magic Number Is: " << gen.getNumber() << endl;

	gen.newNumber();
	cout << "The Next Magic Number Is: " << gen.getNumber() << endl;

	gen.newNumber();

	for(int i=0; i < 5;i++){
		int guess;
		cout << "guess: ";
		cin >> guess;
		if(gen.isCorrect (guess)== true){
			cout << "You Are Correct!!" << endl;
			
		}else{
			cout << "Sorry, You Suck!!\n" << endl;
		}
		
		
	}

do { 
		cout << "Would you like to play again? y/n ";
		cin >> playAgain;
		} while (playAgain !='y' && playAgain !='n');
	
	return 0;
};


};

}
};
This article has been dead for over six months. Start a new discussion instead.