about enum

dear guru,
here am i again i need some help from you please.
i m writing a program that i should read from a file a tvprograms. the file is given with this format

hour: minute day channel
tvprogram

i reading it without problem but i need to check that the hours is in the range of 1 to 24 and the minute 1 to 60 and the days only in english i have the idea below for the days but i don't know if it's good enough actually i m trying to use enum but i don't know how to use it if you have any sugestions please let me know
1
2
3
4
5
6
7
8
9
10
11
ยจenum Programtv{ MONDAY,TUESDAY, WEDNESDAY, THURSDAY,FRIDAY, SATURDAY,SUNDAY}

struct Programtv{
    string day;
    string channel;
   string name_of_show;
    int hour;
    int minute;
    
}; 
 
The enum won't help you. Enums are essentially integers. MONDAY is 0, TUESDAY is 1, etc.

I think you need to compare against English strings -- "Monday", "Tuesday", etc.

please i don't undrstand can make it a bit clearer
what do you means by compare against English strings
The file you are reading looks like:

10: 30 Monday 6
Nova

You will read in the string "Monday" into day (in your struct).
You cannot write this code:

1
2
3
Programtv program;
/* read in program from file */
if( program.day == MONDAY )


because you would be comparing the string "Monday" against the integer 0.
You can compare integers to integers or strings to strings.

You would need

 
if( program.day == "Monday" )


hello dear gurus it is me again i solved my problem with your help but i don't know if it is a smart way to do it here is the code for the cplusplus community.
frist my program suppose to stop if i m reading a invaild input from the file so i compare it this way with a function that i named checkInput


here is the function :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
bool CheckInput ( string input,bool& is_valid )
{
    if ( input != "Monday")
    { 
       if ( input != "Tuesday")
       {
           if ( input != "Wednesday")
           { 
               if ( input != "thursday")
               { 
                   if ( input != "Friday")
                   { 
                      if ( input != "Saturday")
                      {
                     
                          if ( input != "Sunday")
                          {
                             cout<<" i was here"<< endl;
                             is_valid = false;
                             return is_valid;
                          }
                      }
                   }
               }
           }
       } 
     }
    return is_valid;
}


and when i call it use the bool to stop reading and it works but if some one has a better way of doing it i will love to see how it should
Wy are you passing is_valid as parameter? The purpose of this function is o check or the input is equal to the name of a day of the week, right? So it should return true if it is, and else it should return false. I dont see the need of this parameter. Easier would be to declare this variable in the beginning of the function.

It is easier and shorter to create an array with all the names like this:

string weekDay[7]={"Monday", "Tuesday",...};

and then use a for-loop to compare the input with every element of this array:

1
2
3
4
5
6
for (int i=0;i<=6;i++){
if (input==weekDay[i]{
      is_valid=true;
      break;
      }
}


You must initialize is_valid as false when you declare it.

Hope this helps. If you got any questions, please ask

Last edited on

thank you for showing interesse in my post
for the question that you asked why i passed is_valid by reference it is because i need is value for stopping the reading

here is the bit code when i m calling my function

1
2
3
4
5
6
7
8
file >> programsarray[size].days;
        bool is_valid = true;
        CheckInput (programsarray[size].days, is_valid ); // function call
        if (!file || !is_valid)
        {
            cerr << "ERROR : incorrect file format, line "<< size+1 <<endl;
            return false;
        }


i tried with array but i was a bit harder for me so i did with my hands
i really want to learn how can do it with an array
any help from you gurus will highly appreciated * bau*
You declare is_valid as true just before you use it as argument. That means that argument will always hold the value true, wich makes there is no need to use it as parameter. You can also declare that value inside the function itself.

The problem with the above code is that you pass the variable is_valid to the function, while you should use a pointer to that variable as argument.

You dont do anything with the value CheckInput returns. There is no need to use complex pointers here, but if you do, make the type of CheckInput void.

This is the solution i would recommend:
Use this as the function CheckInput:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool CheckInput (string input){
//local variables:
bool is_valid=false;
string weekDay[7]={"Monday","Tuesday",...}

//check or the input is correct
for (int i=0;i<=6;i++){
if (input==weekDay[i]{
      is_valid=true;  //set is_valid to true if the input is correct:
                             //when the input is incorrect this wont happen and is_valid would stay false
      break;
      }
}
return is_valid; //return true if the input is correct, else false
}


Then you should call it like this:

1
2
3
4
5
6
7
8
9
10
11
12
string userChoose; //try to avoid the same name for different local variables
bool correctChoose;
cout<<"Enter a day of the week:\n";
cin>>userChoose;
correctChoose=CheckInput(userChoose); //store the value returned by the function into correctChoose
if (correctChoose){
//do what you want to do if input is correct
}
else
{
//do what you want to do if input is incorrect
}


I havent tested those codes, there may be some mistakes in it.

Hope you understand the above codes. It is more important that you understand how it works then that it works. Please ask if you got questions
Last edited on
thank you very much i got now i change my bool checkInput to void and i m still passing is_vaild by reference and everything is working perfectely good and i understand the point as well thank you very much guru scipio(172)
Topic archived. No new replies allowed.