#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
    string Choice;
    string On, off;
    char a = 'A';
    int  i = 0, j= 0 , NUM = 2 , z = 1;

    cout << "Please enter 'start' to start the program\n";
    cin >> On;
    if (On == "start")
    {
        cout << endl << "Please enter 'choice1' for pattern A or 'choice2' for pattern B \n ";
        cin >> Choice; 
        cout << endl;
        if (Choice == "choice1")
        {

            for(j=-NUM; j<=NUM; j++){

            for(i=-NUM; i<=NUM; i++){

                if( abs(j)==abs(i) && j < 1 ) 
                    {
                        cout<<a;
                        a++;
                    }

                else if (abs(j)==abs(i) && j < 3)
                    {
                        cout<<z;
                        z++;
                    }
                else
                { 
                cout<<" ";
                }
                }
                cout<<endl;
                }
        }

        }

        if (Choice == "choice2")
        {


            for(j=-NUM; j<=NUM; j++){

            for(i=-NUM; i<=NUM; i++){

                if( abs(j)==abs(i) && j < 1 ) 
                    {
                        cout<<z;
                        z++;
                    }

                else if (abs(j)==abs(i) && j < 3)
                    {
                        cout<<a;
                        a++;
                    }
                else
                    { 
                    cout<<" ";
                    }
                }
                cout<<endl;
                }
}


    cout << "enter 'stop' to stop the program\n";
    cin >> off;
    while (off == "stop") {
    {
    return 0;
    }
    }
    cin.get();
    return 0;
    }

Recommended Answers

All 4 Replies

Design a C++ program by considering the following conditions;
1. User must keyed-in “start” to start the program and “stop” to
end the program.
.
2. If the user keyed-in “choice1”, print out PATTERN A.
3. If the user keyed-in “choice2”, print out PATTERN B.
Use if else statement, while loop, nested for loop and switch case statement
in your program.

// the patterns are like X with nums and letters

yes I know that, that's the catch. there is a way around it

Use an enumeration http://en.cppreference.com/w/cpp/language/enum

Something like:

#include <iostream>
#include <string>

enum class choice_t : std::size_t { one = 1, two = 2 /* ... */, invalid = std::size_t(-1) };

std::ostream& operator << ( std::ostream& stm, choice_t c )
{
    std::cout << "choice_t::" ;

    static const std::string text[] = { "invalid", "one", "two", /* ... */ } ;

    std::size_t v = std::size_t(c) ;
    if( v >= sizeof(text) / sizeof( text[0] ) ) v = 0 ; // invalid choice

    return stm << text[v] ;
}

std::istream& get_choice( std::istream& stm, choice_t& choice )
{
    std::string c ;
    if( stm >> c )
    {
        if( c == "1" || c == "one" || c == "choice1" ) choice = choice_t::one ;
        else if( c == "2" || c == "two" || c == "choice2" ) choice = choice_t::two ;
        // else if ...
        else
        {
            choice = choice_t::invalid ; // invalid input
            stm.clear( std::ios::failbit ) ; // force the stream into a failed state
        }
    }
    return stm ;
}

int main()
{
    std::cout << "\nPlease enter 'choice1' for pattern A or 'choice2' for pattern B: " ;
    choice_t choice ;
    get_choice( std::cin, choice );

    switch(choice)
    {
        case choice_t::one:
            std::cout << "your choice is: " << choice << " => pattern A\n" ;
            // ...
            break ;

        case choice_t::two:
            std::cout << "your choice is: " << choice << " => pattern B\n" ;
            // ...
            break ;

        // case ...

        default: std::cout << "this is an invalid choice (" << choice << ")\n" ;
    }
}
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.