0

I'm new to this site but have been taking a programming class for about a semester now. I just had a question as to why my program is skipping all the cases, goes to default, and closes immediately without stopping or doing anything. It's probably just a small thing but I've been sitting here forever and can't see it. Here's the code. Thanks for the help:

//Name Game
#include <iostream>  
#include <fstream>  
#include <stdlib.h> 
#include <math.h>  
#include <iomanip>  
using namespace std; 
int main(void)
{
    char name, zzz;
    cout << "Hello, and welcome to the start of a fantastic new adventure. \nI'm your guide for the day: Sebastian.  \nMay I ask what your name is?\n";
    cin >> name;
    switch (name)
    {
    case 'alex':
    case 'Alex':
        cout << "What an absolutely beautiful name. It sends chills down my spine just thinking about it. \nYou're one lucky girl.";
        cout << "\nI've heard from my friend that you are as beautiful as your name suggests.";
        break;
    case 'Ryan':
    case 'ryan':
        cout << "Please stop using this project.  What are you even doing here?";
        break;
    default:
        cout << "That's a nice name.  You should keep it for as long as you can.";
        cin >> zzz;
        return (0);
    }
    return 0;
}
  • 1
    You're confusing `char`s for strings. – Biffen Apr 21 '16 at 18:09
  • 1
    You can not do that: Read the compiler warnings (errors) and a book. A case label must be an integral (integer) constant. –  Apr 21 '16 at 18:10
  • 7
    @DieterLücking, `'alex'` _is_ an integral constant (it's a _multicharacter literal_, which is conditionally-supported, has type `int`, and has an implementation-defined value) – Jonathan Wakely Apr 21 '16 at 18:11
  • @JonathanWakely Thanks for the clarification. –  Apr 21 '16 at 18:22

3 Answers3

6

A char is only a single character, not a whole name.

When you read a char, it will be a or A or ,, but never Alex.

You have to use strings for this task, which are written in "double" quotes, not 'single' quotes.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
0

I'm a bit surprised this compiles as the cases in the switch statement use single quotes to enclose multiple characters. Single quotes typically contain just a single character, to represent multiple characters (a 'string') you would use double quotes.

The name variable is declared as a char which can only hold a single character - I think this is why it's always selecting the default case.

I'd recommend reading up on handling strings in c++ to accomplish what you're trying to do.

grae22
  • 104
  • 11
  • Ok, thanks you guys. I'll definitely make sure to read up on the difference between the two (my teacher never taught us strings so I'm on my own there). Anyway, thanks a lot! Appreciate ya'll! – Russell Lochrie Apr 25 '16 at 14:09
0

Your problem is because you misunderstood variable types between char and string, where string is bunch (group) of char.

String name1 = "Alex";
cout<<name1;

it will print Alex (notice the double quotes), while

char name2 = 'Alex';
cout<<name2;

will print A, since variable name2 is only for one character

Because your case condition is multiple char and your switch variable is only one character, the switch-case statement goes to default.

The alternative is using string variable and change the switch-case statement to if-else statement since switch case cant handle string variable (without creating pointer or hash)

duck
  • 369
  • 3
  • 17