行数取决于用户输入

number of rows depending on user input

本文关键字:输入 用户 取决于      更新时间:2023-10-16

我正在尝试创建可以输入的行数,具体取决于用户输入。

因此,它要求我进行多个行,Max 100,当我键入12时,我想创建12行,我想在每一个中输入其中的每一个英语字母。

之后,我需要对所有键入这些行的数据做点事

ps,它显示了我在void Line中的错误..

 #include <iostream>
void riadkov (int arg[], int dlzka_r){
      char dlzka_r[100];
      riadkov(ulohy, dlzka_r);
int main(){
using namespace std;
int ulohy;
     cout << "zadaj pocet uloh: ";
     cin >> ulohy;
     if (ulohy >= 1 && ulohy <= 100){

     cout << riadkov[ulohy] << endl; }
     }else{
     cout << "minimalne 1 uloha, maximalne 100 uloh!" << endl;
     }
system("pause");   
}

正如Alex所说,您不能在另一个内部创建功能,应该使Riadkov成为lambda函数:

auto riadkov = [](int arg[], in dkzka_r) -> void {
    // implementation
}

其次,由于您尝试动态创建许多行,您需要的是:

char **data; // but you 'll have to malloc/new yourself

如果您不需要使用char,则可以选择一个字符串的容器

编辑:

编译并运行此示例(与char ** ... rtfm)

#include <iostream>
#include <vector>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<std::string> data; // contains a sequence of strings
    std::size_t num(0); // number of rows
    do 
    {
        std::cout << "Enter number (1 to 100) of rows : " ;
        std::cin >> num;
    } while (num < 1 || num > 100); 
    for (std::size_t i(0); i < num; ++i)
    {
        data.push_back(std::string()); // add an empty string
        std::cout << "nEnter data for row " << i << " : ";
        std::cin >> data.back(); // fill the empty string with user input
        if (data.back().length() > 100) {
            std::cout << "Only 1 to 100 characters are allowed";
            data.pop_back(); // remove the last string
            --i; // the ith row will be prosessed again
        }
    }
    // now to print what you inserted in the vector
    std::cout << "Printing contents of the vectorn";
    for (std::size_t i(0), ie(data.size()); i < ie; ++i)
    {
        std::cout << i << ". :" << data[i] << std::endl;
    }
    return 0;
}