如何将char转换为字符串并从字符串打印到main函数

时间:2015-10-12 17:13:42

标签: c++

我是初学者! 我必须编写一个类似于caesar密码的代码,我有一个main.cpp,它写在下面,我在一个单独的ConvertString.cpp中编写了我的代码,也写在下面。

我无法使用ConvertString.cpp打印输出 main.cpp中。任何人都可以指出我的错误。非常感谢你的帮助!

以下是main.cpp:

#include <iostream>
#include <string>
using namespace std;

string ConvertString (string, int);

int main () {
    // define the modification intger variable and 
    //the new modification variable to recover the text
    int mod_int, recover_int;

// declare the input string , mod_string,  recovered string
    string input_string, mod_string, recovered_string;

    // top level loop to perform modification
    while (1) {
        cout << "Please enter modification key (or -1 to exit)" << endl;
        // read in the mod_int from user
        cin >> mod_int;

        // if the user is done (mod_int == -1), then exit
        if (mod_int == -1) break;

        // read in the text to be modified 
        cout << "Please enter text to be modified" << endl;
        cin >> input_string;

        // modify the input text
        mod_string = [ConvertString(input_string, mod_int)][1];
        // print out the modified text
        cout << endl << "The modified text is " << endl << mod_string << endl;



        // calculate a new recover_int value to recover original text
        // from the modified text -- used for testing only
        recover_int = 26 - (mod_int%26);
        // recover original text by calling ModifyString with new recover_int value
        recovered_string = ConvertString(mod_string, recover_int);

        // print out the recovered text -- should match original
        cout << endl << "The recovered text is " << endl << recovered_string << endl << endl;

    } // end while loop
} // end main function


enter code here

以下是ConvertString.cpp

    string ConvertString (string input_string, int mod_int)
    {
    // your programming code
        int x;  //integer x is declared
        x= input_string.length(); 
        for(int index=0; index < x ; index++)   

        {   

        if(isalpha(input_string[index]))    //checks the string for alphabet
            {   
        int start = 65; //if alphabets is present in the string, it is  assumed as upper case characters
                                //starting point is taken as character 'A' with ASCII value of 65

            if(islower(input_string[index]))    //if lower case character is present in string
                    start=97;                   //start is taken as 'a' with ASCII value of 97

            ConvertString(input_string, mod_int)=(((int)input_string[index]- start + mod_int)% 26) + start;

            //input_string is converted to integer and then modified by the formula
                //formula for characters in the string to cycle in the range from a-z and A-Z.
            }   //closing parenthesis for if

            else
                cout <<input_string[index]; // if character is not a-z or A-Z, print it without any modification

        }   //closing parenthesis belong to - for
  return input_string; // return the converted string
} //end ConvertString

1 个答案:

答案 0 :(得分:0)

你忘了return。变化:

ConvertString(input_string, mod_int)=(((int)input_string[index]- start + mod_int)% 26) + start;

return ConvertString(input_string, mod_int)=(((int)input_string[index]- start + mod_int)% 26) + start;
// ^^^ Missing