Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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++_Arrays_String_Io_Formatting - Fatal编程技术网

C++ 带字符显示的矢量

C++ 带字符显示的矢量,c++,arrays,string,io,formatting,C++,Arrays,String,Io,Formatting,我在显示寄存器中输入的数据时遇到问题。我编写的以下程序只显示最后一个寄存器。(ziua=day,inregistrari=registers,data=date(例如,2013年2月3日)) #包括 #包括 #包括 #包括 使用名称空间std; int main() { char ziua[30],data[30],inregistrari[90]; int n,i; 库特 LI>你是用C++编程的,你应该使用 STD::String < /Cord>代替C风格字符串。 inregistrar

我在显示寄存器中输入的数据时遇到问题。我编写的以下程序只显示最后一个寄存器。(ziua=day,inregistrari=registers,data=date(例如,2013年2月3日))

#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
char ziua[30],data[30],inregistrari[90];
int n,i;
库特
<> LI>你是用C++编程的,你应该使用<代码> STD::String < /Cord>代替C风格字符串。
  • inregistrari[90]
    是一个字符数组,其大小足以容纳一个最大长度为89
    char
    s(+终止字符)的字符串,但您的循环似乎将其视为一个或多个数组(尽管在本例中
    get(inregistrari);
    继续重写相同的字符串)
  • 函数<代码>获取一般不适用,在C中应该使用<代码> FGES< /Cord>(但这是C++,因此这里的真正解决方案应该使用<代码> STD::GETLION<代码> < < /LI>)
  • 在此处使用
    std::vector
    代替C样式数组
  • 打印注册表中的
    i
    for
    循环的主体中,但此循环的每次迭代都会执行完全相同的操作(打印不以任何方式依赖于
    i
  • 在全局空间中使用namespace std;
    是一种不好的做法
  • 您不必在函数开始时声明所有变量,这在旧的ANSI C中是必需的(大约20年前)
  • 下面是一个示例,它可能看起来像:

    #include <iostream>
    #include <string>
    #include <vector>
    
    int main()
    {
        std::string day, date;
        int registerCount;
    
        std::cout << "INPUT DATA"
                  << std::endl << std::endl
                  << "Enter the day in which you want to perform the register: "
                  << std::endl;
        std::cin >> day;
        std::cout << "DATE:" << std::endl;
        std::cin >> date;
        std::cout << "Enter the number of registers you wanna perfom for the day "
                  << day << ":" << std::endl;
        std::cin >> registerCount;
    
        std::vector<std::string> registers(registerCount);
        for (int i = 0; i < registerCount; ++i)
        {
            std::cout << "Register " << i << ":" << std::endl;
            std::getline(std::cin, registers[i]);
        }
    
        std::cout << "The data for the day of " << day << " are the following: "
                  << std::endl;
    
        std::cout << "DATE: " << date << std::endl;
        for (int i = 0; i < registerCount; ++i)
            std::cout << registers[i] << std::endl;
    }
    
    #包括
    #包括
    #包括
    int main()
    {
    std::字符串日期;
    国际注册计数;
    标准::cout
    <> LI>你是用C++编程的,你应该使用<代码> STD::String < /Cord>代替C风格字符串。
    
  • inregistrari[90]
    是一个字符数组,其大小足以容纳一个最大长度为89
    char
    s(+终止字符)的字符串,但您的循环似乎将其视为一个或多个数组(尽管在本例中
    get(inregistrari);
    继续重写相同的字符串)
  • 函数<代码>获取一般不适用,在C中应该使用<代码> FGES< /Cord>(但这是C++,因此这里的真正解决方案应该使用<代码> STD::GETLION<代码> < < /LI>)
  • 在此处使用
    std::vector
    代替C样式数组
  • 打印注册表中的
    i
    for
    循环的主体中,但此循环的每次迭代都会执行完全相同的操作(打印不以任何方式依赖于
    i
  • 在全局空间中使用namespace std;
    是一种不好的做法
  • 您不必在函数开始时声明所有变量,这在旧的ANSI C中是必需的(大约20年前)
  • 下面是一个示例,它可能看起来像:

    #include <iostream>
    #include <string>
    #include <vector>
    
    int main()
    {
        std::string day, date;
        int registerCount;
    
        std::cout << "INPUT DATA"
                  << std::endl << std::endl
                  << "Enter the day in which you want to perform the register: "
                  << std::endl;
        std::cin >> day;
        std::cout << "DATE:" << std::endl;
        std::cin >> date;
        std::cout << "Enter the number of registers you wanna perfom for the day "
                  << day << ":" << std::endl;
        std::cin >> registerCount;
    
        std::vector<std::string> registers(registerCount);
        for (int i = 0; i < registerCount; ++i)
        {
            std::cout << "Register " << i << ":" << std::endl;
            std::getline(std::cin, registers[i]);
        }
    
        std::cout << "The data for the day of " << day << " are the following: "
                  << std::endl;
    
        std::cout << "DATE: " << date << std::endl;
        for (int i = 0; i < registerCount; ++i)
            std::cout << registers[i] << std::endl;
    }
    
    #包括
    #包括
    #包括
    int main()
    {
    std::字符串日期;
    国际注册计数;
    
    std::那么我应该怎么做才能在一个单独的数组中写入每个寄存器?@CokeThe:请稍等,我会为您编写:)如果我不声明,我的编译器就无法识别变量them@CokeThe:出于好奇:您使用的编译器到底是什么?顺便检查一下我的答案,我还添加了一些关于 STD::GETLINE < /C> >我将规则7应用到向量:/我将考虑将<代码> int >代码> <代码> SigZiT并在“代码> STD::CIN < /Calp>中添加一些流状态检查。公平地说,超出了这个问题的范围。那么我应该怎么做才能登记在每个寄存器的单独数组中?”为您编写:)如果我不声明,我的编译器将无法识别变量them@CokeThe出于纯粹的好奇心:你使用的编译器到底是什么?BTW现在检查我的答案,我还添加了一些关于“代码> STD::GETLINE ”的附加信息,我将规则7应用到向量中:>大小\u t
    并在
    std::cin
    上添加一些流状态检查。公平地说,这超出了这个问题的范围