Character input

This is a discussion on Character input within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. I am new in programming and I wanted to ask your help with a problem. I want to ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    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, 10: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

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