0

In class we have to make a code that give a color etc etc, I am very bad to explain things (I'm french). So the problem is that in my code (under), I would like to prevent the user to input a number up to 255, but it actually don't work, how can I fix it ;p Thanks all

#include <iostream>
using namespace std;

//Déclarations :

struct Srgb
{

    unsigned char r ;

    unsigned char g;

    unsigned char b;
};

union UColor
{

    unsigned int val;

    Srgb components;

    unsigned char tabCol[3];
};

int valeur_unique;


int main()
{

    int entier;
    UColor  rgb1;
    int choix;

    cout << "Vous preferez entrer valeur unique(1) ou RGB(2) ? : " << endl;
    cin >> choix;
    switch (choix) {

    case 1:        //Valeur unique
        cout << " Entrez la valeur totale : " << endl;
        cin >> valeur_unique;
        break; //CASE VALEUR UNIQUE


    case 2:    //RGB

               ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// ROUGE
        cout << " Entrez la valeur du rouge :" << endl;
        cin >> entier;
        rgb1.components.r = entier;

        while (rgb1.components.r >= 256) {
            cout << "Rouge ne peux pas etre superieur a 255" << endl;
            cout << " Entrez la valeur du rouge : " << endl;
            cin >> entier;
            rgb1.components.r = entier;
        }

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////  VERT
        cout << " Entrez la valeur du vert :" << endl;
        cin >> entier;
            rgb1.components.g = entier;

            while (rgb1.components.g >= 256) {
                cout << "Vert ne peux pas etre superieur a 255" << endl;
                cout << " Entrez la valeur du vert : " << endl;
                cin >> entier;
                rgb1.components.g = entier;
            }

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// BLEU
        cout << " Entrez la valeur du bleu :" << endl;
        cin >> entier;
            rgb1.components.b = entier;

            while (rgb1.components.b >= 256) {
                cout << "Bleu ne peux pas etre superieur a 255" << endl;
                cout << " Entrez la valeur du bleu : " << endl;
                cin >> entier;
                rgb1.components.b = entier;
            }

        cout << (int) rgb1.components.r << endl;
        cout << (int)rgb1.components.g << endl;
        cout << (int)rgb1.components.b<< endl;
        break; //CASE RGB
    }
}

Sorry if I don't properly use the thing. So As you can see, with the while loop I try to do that if the user enter something sup or equals to 256 it still ask to enter something, but it don't, why ?

scohe001
  • 15,110
  • 2
  • 31
  • 51
Céleste
  • 7
  • 3

1 Answers1

8

Check if the input is valid before assigning into your char!

If you assign into your char immediately and then check to see if the char is under 256, it will always be!

You need to check the int before you assign (and ever better, check that it's in the range [0, 255] like you want):

cout << " Entrez la valeur du rouge :" << endl;
cin >> entier;

while (entier < 0 || entier > 255) {
    cout << "Rouge ne peux pas etre superieur a 255" << endl;
    cout << " Entrez la valeur du rouge : " << endl;
    cin >> entier;
}

// Only assign after we know entier is good!
rgb1.components.r = entier;
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • Is it part of the standard that assigning an `int` outside the range of valid values for an `unsigned char` performs modular arithmetic, or is it undefined what happens when you say something like `unsigned char = 300;`? – Nathan Pierson Nov 11 '20 at 16:25
  • `int` to `unsigned char` well defined: In essence, mod math. – chux - Reinstate Monica Nov 11 '20 at 16:29
  • Wow I didn't fast answers, and that much ! @chux-ReinstateMonica I finaly understood my mistake and yes the condition had to be with "entier" up to zero. scohe001 Thanks for the part of the code, You resolved my problem and the code is more optimised like that. Thanks – Céleste Nov 11 '20 at 16:34