Click here to Skip to main content
15,884,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my code(C++). The problem I am facing now is that whenever i pick more than 2 colours and when displaying them, it only displays the last choice of mine twice. For example, when i pick 2 colours or more, for example, Pink and Blue, instead of outputting Pink Blue, it outputs Blue Blue (twice) or when i pick 3 colours, Pink Blue Yellow, it outputs Yellow Yellow Yellow. How can i fix this?

What I have tried:

#include <iostream>

using namespace std;

int main()
{
	
        int r, value;
        int v1[6];
        cout<<"How many colours?: ";
        cin>>r;
        for(int i=0; i<r; i++)
        {
            cout<<"\nColours:";
	        cout<<"\n(1) Pink";
	        cout<<"\n(2) Blue";
	        cout<<"\n(3) Green";
	        cout<<"\n(4) Yellow";
	        cout<<"\n(5) Red";
	        cout<<"\n(6) Orange\n";
	        cout<<"\n Pick a colour: ";
        	cin>>v1[6];
        	cout<<"How many?: ";
        	cin>>value;	
		}
		
		for(int i=0; i<r; i++)
	    {
	        
	    if(v1[6] == 1)
	    {
	        cout<<"Pink";
	    }
	    else if(v1[6] == 2)
	    {
	        
	        cout<<"Blue";
	    }
	    else if(v1[6] == 3)
	    {
	        
	        cout<<"Green";
	    }
	    else if(v1[6] == 4)
	    {
	        
	        cout<<"Yellow";
	    }
	    else if(v1[6] == 5)
	    {
	        
	        cout<<"Red";
	    }
	    else if(v1[6] == 6)
	    {
	        
	        cout<<"Orange";
	    }
	    
	    }
	


        return 0;

	}
Posted
Updated 31-May-21 21:24pm

You are not using correctly the input array v1. It should represent the user selections, so, it should contain r items and, then, your code should assign proper values to each of them.
Try, for instance
C++
#include <iostream>
using namespace std;

void show_avail_colours();
void show_colour( unsigned colour );

int main()
{
  unsigned colours;
  cout << "how many colors? ";
  cin >> colours;
  unsigned selected_colour[colours];

  for ( unsigned colour = 0; colour < colours; ++colour)
  {
    show_avail_colours();
    cout<<"\n Pick a colour: ";
    cin>>selected_colour[colour];
  }
  cout << "selected colours: \n";
  for ( unsigned colour = 0; colour < colours; ++colour)
  {
    show_colour( selected_colour[colour] );
    cout << "\n";
  }
  cout << endl;
}


void show_avail_colours()
{
  cout<<"\nColours:";
  cout<<"\n(1) Pink";
  cout<<"\n(2) Blue";
  cout<<"\n(3) Green";
  cout<<"\n(4) Yellow";
  cout<<"\n(5) Red";
  cout<<"\n(6) Orange\n";
}


void show_colour( unsigned colour )
{
  string name;
  switch (colour)
  {
  case 1:
    name = "Pink";
    break;
  case 2:
    name = "Blue";
    break;
  case 3:
    name = "Green";
    break;
  case 4:
    name = "Yellow";
    break;
  case 5:
    name = "Red";
    break;
  case 6:
    name = "Orange";
    break;
  default:
    name = "Unknown";
  }
  cout << name;
}
or
C++
#include <iostream>
#include <array>
using namespace std;

void show_avail_colours( const array<string, 6> & colour_name);

int main()
{
  const array<string, 6> colour_name { "Pink", "Blue", "Green", "Yellow", "Red", "Orange" };

  unsigned colours;
  cout << "How many colours?\n";
  cin >> colours;
  unsigned selected_colour[colours];

  for (unsigned colour = 0; colour < colours; ++colour)
  {
    show_avail_colours(colour_name);
    cout<<"\n Pick a colour: ";
    cin>>selected_colour[colour];
    --selected_colour[colour]; // save it  0-based
  }
  cout << "Selected colours: \n";

  for (unsigned colour = 0; colour < colours; ++colour)
  {
    string name;
    if ( selected_colour[colour] < colour_name.size() )
      name = colour_name[ selected_colour[colour]];
    else
      name = "Unknown";
    cout << name << "\n";
  }
}

void show_avail_colours( const array<string, 6> & colour_name)
{
  cout << "\nColours";
  for (unsigned n=0; n<colour_name.size(); ++n)
    cout << "\n("<< (n+1) <<") " << colour_name[n];
}
 
Share this answer
 
Comments
Joe Woodbury 1-Jun-21 3:20am    
unsigned selected_colour[colours]; is an error. I prefer a vector here.
CPallini 3-Jun-21 2:11am    
No, it is not an error.
Joe Woodbury 3-Jun-21 2:20am    
This is valid for C99, but not C++. gcc and clang allow it as a non-standard extension. VC++ does not.
CPallini 3-Jun-21 2:50am    
OOPS. It looks you're right. I stand corrected.
Simple: you are declaring an array of six values:
int v1[6];
But they you always use the same value to store or access data:
cin>>v1[6];
...
if(v1[6] == 1)
...
else if(v1[6] == 2)
And worse: that value doesn't exist!
When you declare an array of N values, you access each of them by using an index, which is a numeric value betwen 0 and N - 1.
So if you declare a three value array of integers:
C++
int arr[3];
You can access then using indexes between 0 and 2 inclusive:
C++
cin >> arr[0];
cout << arr[1];
arr[2] = 666;
Elements with values below zero and above 2 do not exist, and will cause some very odd bugs in your code (if the compiler you use isn't set to check array bounds and throw an error is you go outside them).

To fill an array of n values in a loop is easy:
C++
int n = 3;
int arr[n];
for (int i = 0; i < n; i++)
   {
   arr[i] = i + 100;
   }
And to print them is equally easy:
C++
for (int i = 0; i < n; i++)
   {
   cout << arr[i] << ", ";
   }


Can I also suggest that you look at the C++ Switch statement[^] which will make your code a lot easier to read!
 
Share this answer
 
This is a modified version of CPallini's C++ solution.

The selected_colour array was changed to a vector. I also changed the colour_names to a simple array, which allows it to be more easily extended (or reduced).

C++
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    static const char* colour_names[] = { "Pink", "Blue", "Green", "Yellow", "Red", "Orange" };

    unsigned colours;
    cout << "How many colours? ";
    cin >> colours;

    vector<size_t> selected_colours;

    for (unsigned colour = 0; colour < colours; ++colour)
    {
        cout << "\nColours";

        for (size_t i = 0; i < std::size(colour_names); ++i)
        {
            cout << "\n (" << (i + 1) << ") " << colour_names[i];
        }

        cout << "\nPick a colour: ";

        size_t selected_colour;
        cin >> selected_colour;
        selected_colours.push_back(--selected_colour);
    }

    cout << "\nSelected colours: \n";

    for (auto colour : selected_colours)
    {
        cout << (colour < std::size(colour_names) ? colour_names[colour] : "Unknown") << "\n";
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900