Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我需要知道如何扩展程序,以便它读取和转换所有输入数字,直到标准输入文件结束_C++ - Fatal编程技术网

C++ 我需要知道如何扩展程序,以便它读取和转换所有输入数字,直到标准输入文件结束

C++ 我需要知道如何扩展程序,以便它读取和转换所有输入数字,直到标准输入文件结束,c++,C++,我写的代码,用户将输入一个罗马数字,输出将是一个常规数字。我已经完成了这一点,但是我在下一部分遇到了困难,那就是: 扩展程序,使其读取和转换所有输入数字,直到标准输入的文件结束。您可能只需在读取一行代码的代码周围添加一个适当的“读取循环”就可以做到这一点 我目前的代码是: #include<iostream> #include<string> using namespace std; int value(char r){ if(std::toupper(r

我写的代码,用户将输入一个罗马数字,输出将是一个常规数字。我已经完成了这一点,但是我在下一部分遇到了困难,那就是:

扩展程序,使其读取和转换所有输入数字,直到标准输入的文件结束。您可能只需在读取一行代码的代码周围添加一个适当的“读取循环”就可以做到这一点

我目前的代码是:

#include<iostream>

#include<string>

using namespace std;

int value(char r){

    if(std::toupper(r) == 'I')
        return 1;
    if (std::toupper(r) == 'V')
        return 5;
    if (std::toupper(r) == 'X')
        return 10;
    if (std::toupper(r) == 'L')
        return 50;
    if (std::toupper(r) == 'C')
        return 100;
    if (std::toupper(r) == 'D')
        return 500;
    if (std::toupper(r) == 'M')
        return 1000;
    return -1;
}

int romantoArabic(string &str){
    int res = 0;
    for (int i=0; i<str.length(); i++)
    {

        int s1 = value(str[i]);
        if (i+1 < str.length())
        {

            int s2 = value(str[i+1]);
            if (s1 >= s2)
            {
                res = res + s1;
            }

            else
            {

                res = res + s2 - s1;
                i++; // Value of current symbol is

            }
        }

        else
        {
            res = res + s1;
            i++;

        }
    }

    return res;

}

int main(){
    string str;
    cout<<"";
    cin>>str;
    cout << ""<< romantoArabic(str) << endl;
    return 0;
}
我的输出是:

1
何时应该:

1
2
3
4
5
6
7
8
9
while(cin>>str){

正如说明所说,不能尝试“在读取单行代码的代码周围添加适当的“阅读循环”。我相当肯定您的老师最近已经讨论了这些内容。没有。他们没有。@R\u Bowsy或他们做了,而您没有注意
1
2
3
4
5
6
7
8
9
while (cin >> str) {
    cout << ""<< romantoArabic(str) << endl;
}