This question already has answers here:
Why the switch statement cannot be applied on strings?
(19个回答)
Single quotes vs. double quotes in C or C++
(12个答案)
3年前关闭。
我是这个网站的新手,但是现在大约一个学期就开始学习编程课程。我只是有一个问题,为什么我的程序会跳过所有情况,变为默认状态,并立即关闭而不停止或执行任何操作。这可能只是一件小事,但我一直坐在这里,看不到它。这是代码。谢谢您的帮助:
(19个回答)
Single quotes vs. double quotes in C or C++
(12个答案)
3年前关闭。
我是这个网站的新手,但是现在大约一个学期就开始学习编程课程。我只是有一个问题,为什么我的程序会跳过所有情况,变为默认状态,并立即关闭而不停止或执行任何操作。这可能只是一件小事,但我一直坐在这里,看不到它。这是代码。谢谢您的帮助:
//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;
}
最佳答案
char
只是一个字符,而不是全名。
当您阅读char
时,它将是a
或A
或,
,但不会是Alex
。
您必须为此任务使用字符串,这些字符串以"double"
引号而不是'single'
引号书写。
关于c++ - 使用C++进行切换会跳过所有情况,并且会连续t ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36777252/
10-10 22:15