char变量不能正常使用c ++

时间:2018-01-21 18:18:18

标签: c++ visual-c++ char switch-statement

我是c ++和编码的新手,在尝试编写基于switch-case指令的简单程序时遇到了问题。我想让用户为char变量设置值,然后使用switch-case指令比较它们,但问题是当我输入我需要的值时,指令不识别它们并运行默认的switch case。我试图解决这个问题很多,但仍然不知道该怎么做以及问题是什么。我肯定错过了一些非常简单的事情,如果有人向我解释这件事,我会非常感激。

#include <iostream> 
#include <stdio.h>

using namespace std;

void main(){
char original_currency_type[4], nedeed_currency_type[4];
float money_amount;
cout << "Enter your currency type (choose between USD,EUR and RUB) \n";
cin >> original_currency_type;
cout << "Ok, so now enter the currency type you need to convert your money to (choose between USD,EUR and RUB, again) \n";
cin >> nedeed_currency_type;
cout << "Enter your money amount \n";
cin >> money_amount;
switch (original_currency_type[4]){
case ('RUB') :{
        if (nedeed_currency_type[4] = 'USD')
            cout << money_amount << " RUB is " << money_amount*60 << " USD";
        else
            cout << money_amount << " RUB is " << money_amount*100 << " EUR";
        break;
    }
case'USD':{
    if (nedeed_currency_type[4] = 'RUB')
        cout << money_amount << " USD is " << money_amount/60 << " RUB";
    else 
        cout << money_amount << " USD is " << money_amount*1.2 << " EUR";
            break;
            }
case 'EUR':{
    if (nedeed_currency_type[4] = 'RUB')
        cout << money_amount << " EUR is " << money_amount/100 << " RUB";
    else 
        cout << money_amount << " EUR is " << money_amount/1,2 << ' USD';

    break;
            }
default :{
    cout << " You haven't been able to choose one of three options right \n";
    break;
              }
    }
}

1 个答案:

答案 0 :(得分:1)

首先在您的交换机行中,而不是比较您正在访问数组original_currency_type的索引4处的字符的整个字符数组。这将导致“Index out of bounds”异常。

其次,请注意,在您的案例陈述中,您并非比较字符,您分配

第三,你在switch语句中所做的比较不是一个字符,它是一个字符串。字符串不能在switch语句中,因为它不是基本类型(如int,double,float,char)。在c ++中,它通常用于拥有字符串对象而不是字符数组,因为有一个字符串对象可以访问一些方便的函数来处理这些类型的任务。

此外,在创建输出行时

cout << money_amount << " RUB is " << money_amount*60 << " USD";

你不能添加一个整数(money_amount)一个字符串(“RUB is”)。 幸运的是,这个问题有一个简单的解决方案。在money_amount之前使用字符串库提供的to_string函数:

cout << to_string(money_amount) << " RUB is " << to_string(money_amount*60) << " USD

解决这个问题的方法可能是使用if和else if语句。 请记住,当您使用字符串对象时,您需要包含字符串库

#include <iostream> 
#include <stdio.h>

//Use this library whenever you want to use strings
#include <string>


using namespace std;

void main(){

    //Use the string object instead of a char array in c++
    string original_currency_type, nedeed_currency_type;
    float money_amount;
    cout << "Enter your currency type (choose between USD,EUR and RUB) \n";
    cin >> original_currency_type;
    cout << "Ok, so now enter the currency type you need to convert your money to (choose between USD,EUR and RUB, again) \n";
    cin >> nedeed_currency_type;
    cout << "Enter your money amount \n";
    cin >> money_amount;

    //When comparing between strings use double quotes (") instead of single quotes (')
    if (original_currency_type == "RUB"){
        if (nedeed_currency_type == "USD")
            cout << to_string(money_amount) << " RUB is " << to_string(money_amount*60) << " USD";
        else
            cout << to_string(money_amount) << " RUB is " << to_string(money_amount*100) << " EUR";
    }
    else if(original_currency_type == "USD") {
        if (nedeed_currency_type == "RUB")
            cout << to_string(money_amount) << " USD is " << to_string(money_amount/60) << " RUB";
        else 
            cout << to_string(money_amount) << " USD is " << to_string(money_amount*1.2) << " EUR";
    }
    else if (original_currency_type == "EUR") {
        if (nedeed_currency_type == "RUB")
            cout << to_string(money_amount) << " EUR is " << to_string(money_amount/100) << " RUB";
        else 
            cout << to_string(money_amount) << " EUR is " << to_string(money_amount/1.2) << " USD";
    }

    else {
        cout << " You haven't been able to choose one of three options right \n";
    }
}
相关问题