Simple Problem (I Imagine It's Simple)

Hello and thank you for your time today. I'm having trouble putting this program together. Basically, a user is prompted to input a number between 10 and 100 and that number determines a note to be output. I'm having trouble determining how to check the input number for a value.

1
2
3
4
5
6
7
8
9
  while( (inspiration_number >= 10) && (inspiration_number <= 100) )
  {
    cout<<"Please enter a positive inspiration number:  "<<endl;
    cin>>inspiration_number;

  if (inspiration_number = ??? )
  {
    cout<<NOTE_A;
  }


Here is a portion of the code where NOTE_A is the sound output appropriate to
the note value input.

Notes correspond as follows:

note_a = 0, 7, 14, 21... etc
note_bb = 1, 8, 15, 22... etc
note_c++ = 2, 9, 16, 23...
note_d- = 3, 10, 17, 24
note_esharp = 4, 11, 18, 25

First, I'll let you know how I thought about doing this..

is it possible to assign multiple values to a int variable, such as:

int note_a = 14, 21, 28, 35;
int note_bb = 15, 22, 29, 36;
int note_c++ = 16, 23, 30, 37;
int note_d- = 10, 17, 24, 31, 38;
The general form that I like for inputting and validating data is:
1
2
3
4
5
6
7
8
9
while (true) {
    cout << "enter data: ";
    cin >> data;
    if (cin && data is valid) {
        break;
    } else {
        deal with error
    }
}


This seems odd to beginners because you break out of the middle of the loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <limits>

using std::cin;
using std::cout;


int main()
{
    int num;
    while (true) {
        cout << "Please enter a number between 10 and 100\n";
        cin >> num;
        if (cin && num >= 10 && num <= 100) {
            break;
        }
        cin.clear();
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    cout << "The number is " << num << '\n';
}
Topic archived. No new replies allowed.