在输入所有输入字符串后使用循环打印输出字符串

时间:2014-02-04 15:29:48

标签: c++ loops for-loop

我的问题是我在输入最后一个输入后需要转换所有输入。代码中有一个函数:你输入一个数字pocet_r,它允许你输入pocet_r字符串。所以我需要以某种方式改变它。

示例,它现在如何运作:

3 (`pocet_r`)
input 
output
input
output
input
output

我希望:

3 (`pocet_r`)
input 
input 
input
output
output
output

我的代码,看看突出显示的循环,那就是错误:

#include<iostream>
#include<string>
#include <ctype.h>

using namespace std;
int pocet_r;
void Convert(string input){
    string output = "";
    string flag = "";
    bool underscore = false;
    bool uppercase = false;
    if ( islower(input[0]) == false){
        cout << "Error!" <<endl;
        return;
    }

    for (int i=0; i < input.size(); i++){
        if ( (isalpha( input[i] ) || (input[i]) == '_') == false){
            cout << "Chyba!" <<endl;
            return;
        }
        if (islower(input[i])){
            if (underscore){
                underscore = false;
                output += toupper(input[i]);
            }
            else
                output += input[i];

        }
        else if (isupper(input[i])){
            if (flag == "C" || uppercase){
                cout << "Chyba!"<<endl;
                return;
            }
            flag = "Java";
            output += '_';
            output += tolower(input[i]);

        }
        else if (input[i] == '_'){
            if (flag == "Java" || underscore){
            cout << "Chyba!" <<endl;
            return;
            }
            flag = "C";
            underscore = true;
        }

    }


    for (int i=input.size()-1; i >=0; i--){
      if (input[i] == '_'){
            if (flag == "Java" || underscore){
            cout << "Chyba!" <<endl;
            return;
            }
            flag = "C";
            underscore = true;
        }
    }
    **for (int i = 0; output[i] != '\0' ; i++){

    cout << output[i] << endl; }**



} 



int main(){

    const int max = 100;
    string input;


    cin >> pocet_r;

    if(pocet_r >= 1 && pocet_r <=100)
    {
               for (int i = 0; i <pocet_r; i++)
               {
                if(input.size() > max){
                 cout << "slovo musi mat minimalne 1 a maximalne 100 znakov" << endl;
                 while(input.size() > max){

                                    **getline(cin, input);
                                    }**
                 }else{

                       Convert (input);
                       }





               }
    }else{
     cout << "Minimalne 1 a maximalne 100 uloh" << endl;
}
system("pause");
}

这段代码正在做类似的事情,当我键入数字4时,我收到四条消息说错误! ,我甚至不输入四个输入字符串......

根据LoPoBo回答

编辑
}else{


                       for(vector<string>::iterator it = inputs.begin(); it != inputs.end(); ++it){
                       inputs.push_back(input);
                       Convert(*it);

                       }

}

1 个答案:

答案 0 :(得分:1)

我没有完全理解你的代码,但是如果你想在计算输出之前读取所有输入,你可以简单地将输入存储在std :: vector中。 然后,在读取所有输入后,您可以遍历向量并进行计算,如下所示:

vector<string> inputs;
// Loop through the inputs and add them to the vector with inputs.push_back(input);
for(vector<string>::iterator it = inputs.begin(); it != inputs.end(); ++it){
Convert(*it);
}

另见http://www.cplusplus.com/reference/vector/vector/

相关问题