mardi 16 novembre 2021

How to make a diagonal line go through a square/rectangle in c++?

So I am almost done with my code, but I don't understand how to code the program to get this pattern.

8???????
?8??????
??8?????
???8????
????8???
?????8??
??????8?
???????8

This is the code i have so far.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    //Variables
    int answer, fillint, size, garbage;
    char fillchar;
    string again;

        //Pattern
        cout << "Your 5 options are: " << endl;
        cout << "Choice 1: Left-to-right diagonal " << endl;
        cout << "Choice 2: Right-to-left diagonal" << endl;
        cout << "Choice 3: Fill left-to-right" << endl;
        cout << "Choice 4: Fill right-to-left" << endl;
        cout << "Choice 5: Exit" << endl;
        cout << "Which choice? ";
        cin >> answer;

        //While loop to have be between 1 and 5
        while (answer < 1 || answer > 5)
        {
            cout << "Not a valid option, try again: ";
            cin >> answer;
        }
        if (answer == 5)
        {
            cout << "You chose to exit, Goodbye! " << endl;

        }
        //Fill Character
        cout << "Which fill character would you like to use? Your 4 options are: " << endl;
        cout << "Choice 1: ?" << endl << "choice 2: &" << endl << "Choice 3: @" << endl << "Choice 4: $" << endl;
        cout << "Which choice? ";
        cin >> fillint;

        //While loop to have be between 1 and 4
        while (fillint < 1 || fillint > 4)
        {
            cout << "Not a valid option, try again: ";
            cin >> fillint;
        }


        //Assinging the fill character to the choice of the user
        if (fillint == 1)
            fillchar = '?';
        else if (fillint == 2)
            fillchar = '&';
        else if (fillint == 3)
            fillchar = '@';
        else if (fillint == 4)
            fillchar = '$';



        //Getting the size
        cout << "What size (1-9)? ";
        cin >> size;

        //While loop to have be between 1 and 9
        while (size < 1 || size > 9)
        {
            cout << "Not a valid option, try again: ";
            cin >> size;
        }


        //switch statement
        switch (answer)
        {
        case 1:

        }



    return 0;
}

These are the instructions for the assignment. I have to make four other patterns, but I think I can hopefully make the rest of the patterns after I understand how to make the first one.

enter image description here

Aucun commentaire:

Enregistrer un commentaire