Thread: Character input

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Character input

    Hello everyone. I am new in programming and I wanted to ask your help with a problem. I want to make a programm that uses characters as input (more accurately the days of the week) and according to your decision you get output. (pex if you choose Monday you get 5, if you choose Friday you get 6 etc.). I made a char variable but it doesn;t work in if statement. Please help me because I am desperate

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Post the code you tried (that didn't work), and we can tell you why it didn't work (so you learn something and won't repeat the same mistakes again).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Here it is

    Code:
    #include <iostream>
    
    int main()
    {
        using namespace std;
        
        char day[20];
    
        cout << "Select a day of the week: ";
        cin >> day;
        
        if (day == Monday)
        {
            cout << "Monday is the worst day of the week!";
        }
        else if (day == Tuesday)
        {
            cout << "Tuesday is boring";
        }
        else if (day == Wednesday);
        {
             cout << "Wednesday is interesting";
        }
        else if (day == Thursday)
        {
              cout << "Thursday is fine";
        }
        else if (day == Friday)
        {
              cout << "Friday is great!";
        }
        else if (day == Saturday)
        {
               cout << "Night out";
        }
        else   
        {
               cout << "Relaxing";
        }
        
       return 0;
    }

    IT SAYS WARNING ON IF STATEMENT AND DOESN'T COMPILE

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need " "

    As in
    if (day == "Monday")

    Also, you're using char arrays, not std::string, so you also need
    if ( strcmp(day,"Monday") == 0 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Change the type of day to std::string. This way of reading input is unsafe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. skipping a character during input
    By sanji2011 in forum C Programming
    Replies: 2
    Last Post: 03-26-2011, 05:37 AM
  2. Another character input problem
    By walkingbeard in forum C Programming
    Replies: 3
    Last Post: 11-26-2009, 11:32 AM
  3. Do While Character Input Error
    By crackpat in forum C Programming
    Replies: 2
    Last Post: 04-13-2009, 11:22 PM
  4. Single Character Input.
    By mintsmike in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2009, 08:16 AM
  5. Multiple Character Input.
    By mintsmike in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 01:15 AM