Two Easy Questions

This is a discussion on Two Easy Questions within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm just starting with C++, and I'm making a couple really simple programs. I have two questions about the ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    22

    Two Easy Questions

    Hello,

    I'm just starting with C++, and I'm making a couple really simple programs. I have two questions about the one I'm on right now. They are:

    1. Is there a function that will stop the script from running like exit(); or something? For example, if I only wanted the script to run if a user entered a number between 0 and 6 and they entered 20, I'd want the script to return a message saying "That was an invalid number. Please try again!" and stop from going on. How would I do this?

    2. How do you get the actual value of something in an enum and print it? I made an enum called Days, and it has the name of the days from Sunday to Monday. Then the program asks the user to put in a number. How can I match that number with the value of the thing in the enum and print out the name of the day? Lol, for example:

    Code:
    #include <iostream>
    int main() {
    
    enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
    
    int x, DayOff;
    
    cout <<"What day would you like off (0-6)? ";
    cin >> x;
    DayOff = Days(x);
    
    }
    Now how would I print out the day that the number they put in represents in the enum?

    Thanks,
    CougarElite

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    you mean like 'if' statemetns or switch?

    you can do it with 'if' statements.
    Code:
    #include <iostream>
    #include <stdlib.h>
    using  namespace std;
    int main() {
    
    enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
    
    int x, DayOff;
    
    cout <<"What day would you like off (0-6)? ";
    cin >> x;
    if(x >= 0 && x <= 6)
    {
      DayOff = Days(x);
    }
    else 
    {
      cout << "PLease input a number between 0 - 6." << endl;
    }
    
    system("PAUSE");
    return 0;
    }
    EDIT:
    I dont know how to pring the date, you could make function that does that like

    Code:
    void Days(int day)
    {
      if(day == 0)
      { 
         cout << "Monday" << endl;
      }
      if(day == 1)
      {
         cout << "Tuesday" << endl;
      }
      if(day == 2)
      { 
         cout << "Wednesday" << endl;
      }
      if(day == 3)
      {
          cout << "Thursday" << endl;
       }
      if(day == 4)
      {
         cout << "Friday" << endl;
      }
      if( day == 5)
      {
         cout << "Saturday" << endl;
      }
      if(day == 6)
      {
         cout << "Sunday" << endl;
      }
    
    }
    and you would delete the enum thing an replace it with that^
    Last edited by mrafcho001; 03-21-2005 at 04:44 PM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by CougarElite
    1. Is there a function that will stop the script from running like exit(); or something? For example, if I only wanted the script to run if a user entered a number between 0 and 6 and they entered 20, I'd want the script to return a message saying "That was an invalid number. Please try again!" and stop from going on. How would I do this?
    Yes there is, and you guessed its name correctly, how 'bout that. It does however take an int parameter that functions in the same manner as the return value from main.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    to add on to what hk_mp5kpdw said

    you need to include process.h for it to work.

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Careful with the bump there, if he had trouble with that he probably would've posted back.

  6. #6
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    You may as well just put:

    Code:
    DayOff = x;
    ... as enums are evaluated as integers anyway. And you cannot obtain the string representation of an enum, in much the same way as you can't obtain the name of a #define label - it's lost at compile-time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Replies: 1
    Last Post: 06-17-2005, 07:29 AM
  3. Questions on Speed of Execution
    By wavering in forum C Programming
    Replies: 22
    Last Post: 01-20-2002, 01:04 PM
  4. questions about new and delete
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2001, 01:48 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21